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

org.elasticsearch.common.collect.CopyOnWriteHashSet Maven / Gradle / Ivy

There is a newer version: 8.14.1
Show newest version
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.common.collect;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Maps;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * {@link Set} implementation based on {@link CopyOnWriteHashMap}.
 * Null values are not supported.
 */
public class CopyOnWriteHashSet extends ForwardingSet {

    /**
     * Return a copy of the provided set.
     */
    public static  CopyOnWriteHashSet copyOf(Collection set) {
        if (set instanceof CopyOnWriteHashSet) {
            // no need to copy in that case
            @SuppressWarnings("unchecked")
            final CopyOnWriteHashSet cowSet = (CopyOnWriteHashSet) set;
            return cowSet;
        } else {
            return new CopyOnWriteHashSet().copyAndAddAll(set);
        }
    }

    private final CopyOnWriteHashMap map;

    /** Create a new empty set. */
    public CopyOnWriteHashSet() {
        this(new CopyOnWriteHashMap());
    }

    private CopyOnWriteHashSet(CopyOnWriteHashMap map) {
        this.map = map;
    }

    @Override
    protected Set delegate() {
        return map.keySet();
    }

    /**
     * Copy the current set and return a copy that contains or replaces entry.
     */
    public CopyOnWriteHashSet copyAndAdd(T entry) {
        return new CopyOnWriteHashSet<>(map.copyAndPut(entry, true));
    }

    /**
     * Copy the current set and return a copy that is the union of the current
     * set and entries, potentially replacing existing entries in
     * case of equality.
     */
    public CopyOnWriteHashSet copyAndAddAll(Collection entries) {
        final Collection> asMapEntries = Collections2.transform(entries,new Function>() {
            @Override
            public Entry apply(T input) {
                return Maps.immutableEntry(input, true);
            }
        });
        CopyOnWriteHashMap updated = this.map.copyAndPutAll(asMapEntries);
        return new CopyOnWriteHashSet<>(updated);
    }

    /**
     * Copy the current set and return a copy that removes entry
     * if it exists.
     */
    public CopyOnWriteHashSet copyAndRemove(Object entry) {
        final CopyOnWriteHashMap updated = map.copyAndRemove(entry);
        if (updated == map) {
            return this;
        } else {
            return new CopyOnWriteHashSet<>(updated);
        }
    }

    /**
     * Copy the current set and return a copy that is the difference of the current
     * set and entries.
     */
    public CopyOnWriteHashSet copyAndRemoveAll(Collection entries) {
        CopyOnWriteHashMap updated = this.map.copyAndRemoveAll(entries);
        if (updated == map) {
            return this;
        } else {
            return new CopyOnWriteHashSet<>(updated);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy