All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.eureka2.interests.MultipleInterests Maven / Gradle / Ivy

There is a newer version: 2.0.0-DP4
Show newest version
/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.eureka2.interests;

import java.util.HashSet;
import java.util.Set;

/**
 * Eureka client can subscribe to multiple interests at the same time. This class
 * describes such multiple interests subscription.
 *
 * @author Tomasz Bak
 */
public class MultipleInterests extends Interest {

    private final Set> interests;

    @SafeVarargs
    public MultipleInterests(Interest... interests) {
        this.interests = new HashSet<>();
        for (Interest interest : interests) {
            append(interest, this.interests); // Unwraps if the interest itself is a MultipleInterest
        }
    }

    public MultipleInterests(Iterable> interests) {
        this.interests = new HashSet<>();
        for (Interest interest : interests) {
            append(interest, this.interests); // Unwraps if the interest itself is a MultipleInterest
        }
    }

    public Set> getInterests() {
        return interests;
    }

    @Override
    public boolean matches(T data) {
        for (Interest interest : interests) {
            if (interest.matches(data)) {
                return true;
            }
        }
        return false;
    }

    public Set> flatten() {
        return new HashSet<>(interests); // Copy to restrict mutations to the underlying set.
    }

    public MultipleInterests copyAndAppend(Interest toAppend) {
        Set> newInterests = flatten(); // flatten does the copy of the underlying set
        append(toAppend, newInterests);
        return new MultipleInterests<>(newInterests);
    }

    public MultipleInterests copyAndRemove(Interest toAppend) {
        Set> newInterests = flatten(); // flatten does the copy of the underlying set
        remove(toAppend, newInterests);
        return new MultipleInterests<>(newInterests);
    }

    private static  void append(Interest interest, Set> collector) {
        if (interest instanceof MultipleInterests) {
            for (Interest i : ((MultipleInterests) interest).getInterests()) {
                append(i, collector);
            }
        } else {
            collector.add(interest);
        }
    }

    private static  void remove(Interest interest, Set> collector) {
        if (interest instanceof MultipleInterests) {
            for (Interest i : ((MultipleInterests) interest).getInterests()) {
                remove(i, collector);
            }
        } else {
            collector.remove(interest);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof MultipleInterests)) {
            return false;
        }

        MultipleInterests that = (MultipleInterests) o;

        return interests.equals(that.interests);

    }

    @Override
    public int hashCode() {
        return interests.hashCode();
    }

    @Override
    public String toString() {
        return "MultipleInterests{" + "interests=" + interests + '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy