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

org.pcollections.MapPSet Maven / Gradle / Ivy

There is a newer version: 3.3.8
Show newest version
/*
 * Copyright (c) 2008 Harold Cooper. All rights reserved.
 * Licensed under the MIT License.
 * See LICENSE file in the project root for full license information.
 */

package org.pcollections;

import java.io.Serializable;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;

/**
 * A map-backed persistent set.
 *
 * 

If the backing map is thread-safe, then this implementation is thread-safe (assuming Java's * AbstractSet is thread-safe), although its iterators may not be. * * @author harold * @param */ public final class MapPSet extends AbstractSet implements PSet, Serializable { private static final long serialVersionUID = 1L; //// STATIC FACTORY METHODS //// /** * @param * @param map * @return a PSet with the elements of map.keySet(), backed by map */ @SuppressWarnings("unchecked") public static MapPSet from(final PMap map) { return new MapPSet((PMap) map); } /** * @param * @param map * @param e * @return from(map).plus(e) */ public static MapPSet from(final PMap map, E e) { return from(map).plus(e); } /** * @param * @param map * @param list * @return from(map).plusAll(list) */ public static MapPSet from(final PMap map, final Collection list) { return from(map).plusAll(list); } //// PRIVATE CONSTRUCTORS //// private final PMap map; // not instantiable (or subclassable): private MapPSet(final PMap map) { this.map = map; } //// REQUIRED METHODS FROM AbstractSet //// @Override public Iterator iterator() { return map.keySet().iterator(); } @Override public int size() { return map.size(); } //// OVERRIDDEN METHODS OF AbstractSet //// @Override public boolean contains(final Object e) { return map.containsKey(e); } //// IMPLEMENTED METHODS OF PSet //// private static enum In { IN } public MapPSet plus(final E e) { if (contains(e)) return this; return new MapPSet(map.plus(e, In.IN)); } public MapPSet minus(final Object e) { if (!contains(e)) return this; return new MapPSet(map.minus(e)); } public MapPSet plusAll(final Collection list) { PMap map = this.map; for (E e : list) map = map.plus(e, In.IN); return from(map); } public MapPSet minusAll(final Collection list) { PMap map = this.map.minusAll(list); return from(map); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy