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

io.gravitee.common.util.ObservableCollection Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.common.util;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
public class ObservableCollection implements Collection {

    private final Collection wrapped;
    private final List> listeners = new ArrayList<>();

    public ObservableCollection(final Collection wrapped) {
        Objects.requireNonNull(wrapped, "Observed collection can not be null");

        this.wrapped = wrapped;
    }

    public void addListener(ChangeListener listener) {
        listeners.add(listener);
    }

    public void removeListener(ChangeListener listener) {
        listeners.remove(listener);
    }

    @Override
    public int size() {
        return wrapped.size();
    }

    @Override
    public boolean isEmpty() {
        return wrapped.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return wrapped.contains(o);
    }

    @Override
    public Iterator iterator() {
        return wrapped.iterator();
    }

    @Override
    public Object[] toArray() {
        return wrapped.toArray();
    }

    @Override
    public  T1[] toArray(T1[] a) {
        return wrapped.toArray(a);
    }

    @Override
    public boolean add(T t) {
        listeners.forEach(listener -> listener.preAdd(t));
        boolean added = wrapped.add(t);
        if (added) {
            listeners.forEach(listener -> listener.postAdd(t));
        }

        return added;
    }

    @Override
    public boolean remove(Object o) {
        listeners.forEach(listener -> listener.preRemove((T) o));
        boolean removed = wrapped.remove(o);
        if (removed) {
            listeners.forEach(listener -> listener.postRemove((T) o));
        }

        return removed;
    }

    @Override
    public boolean containsAll(Collection c) {
        return wrapped.containsAll(c);
    }

    @Override
    public boolean addAll(Collection c) {
        return wrapped.addAll(c);
    }

    @Override
    public boolean removeAll(Collection c) {
        return wrapped.removeAll(c);
    }

    @Override
    public boolean removeIf(Predicate filter) {
        return wrapped.removeIf(filter);
    }

    @Override
    public boolean retainAll(Collection c) {
        return wrapped.retainAll(c);
    }

    @Override
    public void clear() {
        wrapped.clear();
    }

    @Override
    public boolean equals(Object o) {
        return wrapped.equals(o);
    }

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

    @Override
    public Spliterator spliterator() {
        return wrapped.spliterator();
    }

    @Override
    public Stream stream() {
        return wrapped.stream();
    }

    @Override
    public Stream parallelStream() {
        return wrapped.parallelStream();
    }

    @Override
    public void forEach(Consumer action) {
        wrapped.forEach(action);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy