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

co.paralleluniverse.common.util.ConcurrentSet Maven / Gradle / Ivy

Go to download

The core library for Fibers on Java, compatible with Java 11-16. Forked from puniverse/quasar

There is a newer version: 10.0.6
Show newest version
/*
 * Copyright (c) 2013-2014, Parallel Universe Software Co. All rights reserved.
 * 
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *  
 *   or (per the licensee's choosing)
 *  
 * under the terms of the GNU Lesser General Public License version 3.0
 * as published by the Free Software Foundation.
 */
package co.paralleluniverse.common.util;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

/**
 *
 * @author pron
 */
public class ConcurrentSet extends AbstractSet implements Set {
    public static  Set make(ConcurrentMap map) {
        return new ConcurrentSet(map);
    }
    
    private final ConcurrentMap map;
    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();

    public ConcurrentSet(ConcurrentMap map) {
        this.map = map;
    }

    public ConcurrentSet(ConcurrentMap map, Collection elements) {
        for(E elem : elements)
            map.put(elem, PRESENT);
        this.map = map;
    }

    @Override
    public boolean add(E e) {
        return map.putIfAbsent(e, PRESENT) != PRESENT;
    }

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

    @Override
    public boolean remove(Object o) {
        return map.remove(o) == PRESENT;
    }

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

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

    public Iterator iterator() {
        return map.keySet().iterator();
    }

    public int size() {
        return map.size();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy