001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Taken from https://github.com/concord/concord-jvm
018 */
019package org.nuxeo.ecm.platform.importer.mqueues.computation;
020
021import java.util.Collections;
022import java.util.Objects;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026public class ComputationMetadata {
027    /** Globally unique identifier of the computation */
028    protected final String name;
029
030    /** List of streams to subscribe this computation to. */
031    protected final Set<String> istreams;
032
033    /** List of streams this computation may produce on */
034    protected final Set<String> ostreams;
035
036    public String name() {
037        return name;
038    }
039
040    public Set<String> inputStreams() {
041        return istreams;
042    }
043
044    public Set<String> outputStreams() {
045        return ostreams;
046    }
047
048    public ComputationMetadata(String name, Set<String> inputStreams, Set<String> outputStreams) {
049        this.name = Objects.requireNonNull(name);
050
051        if (inputStreams == null) {
052            this.istreams = Collections.emptySet();
053        } else {
054            this.istreams = inputStreams;
055        }
056        if (outputStreams == null) {
057            this.ostreams = Collections.emptySet();
058        } else {
059            this.ostreams = outputStreams;
060        }
061
062        if (this.istreams.isEmpty() && this.ostreams.isEmpty()) {
063            throw new RuntimeException("Both input and output streams are empty");
064        }
065    }
066
067    @Override
068    public boolean equals(Object o) {
069        if (this == o) return true;
070        if (o == null || getClass() != o.getClass()) return false;
071
072        ComputationMetadata metadata = (ComputationMetadata) o;
073
074        if (!name.equals(metadata.name)) return false;
075        if (!istreams.equals(metadata.istreams)) return false;
076        return ostreams.equals(metadata.ostreams);
077
078    }
079
080    @Override
081    public int hashCode() {
082        int result = name.hashCode();
083        result = 31 * result + istreams.hashCode();
084        result = 31 * result + ostreams.hashCode();
085        return result;
086    }
087
088    @Override
089    public String toString() {
090        return "ComputationMetadata{" +
091                "name='" + name + '\'' +
092                ", istreams=" + istreams.stream().collect(Collectors.joining(",")) +
093                ", ostreams=" + ostreams.stream().collect(Collectors.joining(",")) +
094                '}';
095    }
096}