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

org.apache.logging.log4j.internal.CopyOnWriteNavigableSet Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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 org.apache.logging.log4j.internal;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.Spliterator;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class CopyOnWriteNavigableSet extends TreeSet {
    private static final long serialVersionUID = 1L;

    private volatile NavigableSet set;
    private final Comparator comparator;

    public CopyOnWriteNavigableSet(final Comparator comparator) {
        set = new TreeSet<>(comparator);
        this.comparator = comparator;
    }

    private CopyOnWriteNavigableSet(final CopyOnWriteNavigableSet copyOnWriteSet) {
        this.set = new TreeSet<>(copyOnWriteSet.set);
        this.comparator = copyOnWriteSet.comparator;
    }

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

    @Override
    public Iterator descendingIterator() {
        return set.descendingIterator();
    }

    @Override
    public NavigableSet descendingSet() {
        return set.descendingSet();
    }

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

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

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

    @Override
    public boolean add(final E e) {
        final NavigableSet newSet = new TreeSet(set);
        final boolean result = newSet.add(e);
        if (result) {
            set = newSet;
        }
        return result;
    }

    @Override
    public boolean remove(final Object o) {
        final NavigableSet newSet = new TreeSet(set);
        final boolean result = newSet.remove(o);
        if (result) {
            set = newSet;
        }
        return result;
    }

    @Override
    public void clear() {
        final NavigableSet newSet = new TreeSet(set);
        newSet.clear();
        set = newSet;
    }

    @Override
    public boolean addAll(final Collection c) {
        final NavigableSet newSet = new TreeSet(set);
        final boolean result = newSet.addAll(c);
        if (result) {
            set = newSet;
        }
        return result;
    }

    @Override
    public NavigableSet subSet(
            final E fromElement, final boolean fromInclusive, final E toElement, final boolean toInclusive) {
        return set.subSet(fromElement, fromInclusive, toElement, toInclusive);
    }

    @Override
    public NavigableSet headSet(final E toElement, final boolean inclusive) {
        return set.headSet(toElement, inclusive);
    }

    @Override
    public NavigableSet tailSet(final E fromElement, final boolean inclusive) {
        return set.tailSet(fromElement, inclusive);
    }

    @Override
    public SortedSet subSet(final E fromElement, final E toElement) {
        return set.subSet(fromElement, toElement);
    }

    @Override
    public SortedSet headSet(final E toElement) {
        return set.headSet(toElement);
    }

    @Override
    public SortedSet tailSet(final E fromElement) {
        return set.tailSet(fromElement);
    }

    @Override
    public Comparator comparator() {
        return comparator;
    }

    @Override
    public E first() {
        return set.first();
    }

    @Override
    public E last() {
        return set.last();
    }

    @Override
    public E lower(final E e) {
        return set.lower(e);
    }

    @Override
    public E floor(final E e) {
        return set.floor(e);
    }

    @Override
    public E ceiling(final E e) {
        return set.ceiling(e);
    }

    @Override
    public E higher(final E e) {
        return set.higher(e);
    }

    @Override
    public E pollFirst() {
        return set.pollFirst();
    }

    @Override
    public E pollLast() {
        return set.pollLast();
    }

    @Override
    public Object clone() {
        return new CopyOnWriteNavigableSet(this);
    }

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

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

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

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

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

    @Override
    public  T[] toArray(final T[] a) {
        return set.toArray(a);
    }

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

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

    @Override
    public String toString() {
        return set.toString();
    }

    @Override
    public  T[] toArray(final IntFunction generator) {
        return set.toArray(generator);
    }

    @Override
    public boolean removeIf(final Predicate filter) {
        final NavigableSet newSet = new TreeSet(set);
        final boolean result = newSet.removeIf(filter);
        if (result) {
            this.set = newSet;
        }
        return result;
    }

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy