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