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.lib.stream.computation;
020
021import java.util.Collections;
022import java.util.Objects;
023import java.util.Set;
024
025/**
026 * The metadata defining a computation.
027 *
028 * @since 9.3
029 */
030public class ComputationMetadata {
031    protected final String name;
032
033    protected final Set<String> inputStreams;
034
035    protected final Set<String> outputStreams;
036
037    public ComputationMetadata(String name, Set<String> inputStreams, Set<String> outputStreams) {
038        this.name = Objects.requireNonNull(name);
039
040        if (inputStreams == null) {
041            this.inputStreams = Collections.emptySet();
042        } else {
043            this.inputStreams = inputStreams;
044        }
045        if (outputStreams == null) {
046            this.outputStreams = Collections.emptySet();
047        } else {
048            this.outputStreams = outputStreams;
049        }
050
051        if (this.inputStreams.isEmpty() && this.outputStreams.isEmpty()) {
052            throw new IllegalArgumentException("Both input and output streams are empty");
053        }
054    }
055
056    /**
057     * Globally unique identifier of the computation.
058     */
059    public String name() {
060        return name;
061    }
062
063    /**
064     * List of streams to subscribe this computation to.
065     */
066    public Set<String> inputStreams() {
067        return inputStreams;
068    }
069
070    /**
071     * List of streams this computation may produce on.
072     */
073    public Set<String> outputStreams() {
074        return outputStreams;
075    }
076
077    @Override
078    public boolean equals(Object o) {
079        if (this == o)
080            return true;
081        if (o == null || getClass() != o.getClass())
082            return false;
083
084        ComputationMetadata metadata = (ComputationMetadata) o;
085
086        return name.equals(metadata.name) && inputStreams.equals(metadata.inputStreams)
087                && outputStreams.equals(metadata.outputStreams);
088    }
089
090    @Override
091    public int hashCode() {
092        int result = name.hashCode();
093        result = 31 * result + inputStreams.hashCode();
094        result = 31 * result + outputStreams.hashCode();
095        return result;
096    }
097
098    @Override
099    public String toString() {
100        return "ComputationMetadata{" + "name='" + name + '\'' + ", inputStreams=" + String.join(",", inputStreams)
101                + ", outputStreams=" + String.join(",", outputStreams) + '}';
102    }
103}