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

com.force.i18n.commons.util.collection.IntHashSet Maven / Gradle / Ivy

There is a newer version: 1.2.30
Show newest version
/* 
 * Copyright (c) 2017, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license. 
 * For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

package com.force.i18n.commons.util.collection;

import java.io.Serializable;

import com.google.common.annotations.Beta;

/**
 * This class implements the {@code IntSet} interface, backed by a hash table
 * (actually a {@code IntHashMap} instance).  It makes no guarantees as to the
 * iteration order of the set; in particular, it does not guarantee that the
 * order will remain constant over time. 

* * This class offers constant time performance for the basic operations * ({@code add}, {@code remove}, {@code contains} and {@code size}), * assuming the hash function disperses the elements properly among the * buckets. Iterating over this set requires time proportional to the sum of * the {@code IntHashSet} instance's size (the number of elements) plus the * "capacity" of the backing {@code IntHashMap} instance (the number of * buckets). Thus, it's very important not to set the intial capacity too * high (or the load factor too low) if iteration performance is important.

* * Note that this implementation is not synchronized. If multiple * threads access a set concurrently, and at least one of the threads modifies * the set, it must be synchronized externally. * * Beta class. Classes under com.force.i18n.commons package will be moved into a dedicated project. * * @author Based on Sun's java.util.HashSet (modified by koliver) * @see IntSet * @see IntHashMap * @see IntMap * Note: use {@link java.util.HashSet} or {@link java.util.EnumSet} instead. */ @Beta public class IntHashSet extends AbstractIntSet implements Serializable { private static final long serialVersionUID = 7500368467424833369L; IntHashMap map; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); /** * Constructs a new, empty set; the backing {@code HashMap} instance has * default capacity and load factor, which is {@code 0.75}. */ public IntHashSet() { this.map = new IntHashMap(); } /** * Constructs a new, empty set; the backing {@code HashMap} instance has * the specified initial capacity and the specified load factor. * * @param initialCapacity the initial capacity of the hash map. * @param loadFactor the load factor of the hash map. * @throws IllegalArgumentException if the initial capacity is less * than zero, or if the load factor is nonpositive. */ public IntHashSet(int initialCapacity, float loadFactor) { this.map = new IntHashMap(initialCapacity, loadFactor); } /** * Constructs a new, empty set; the backing {@code HashMap} instance has * the specified initial capacity and default load factor, which is * {@code 0.75}. * * @param initialCapacity the initial capacity of the hash table. * @throws IllegalArgumentException if the initial capacity is less * than zero. */ public IntHashSet(int initialCapacity) { this.map = new IntHashMap(initialCapacity); } @Override public IntSet makeEmpty() { return new IntHashSet(this.map.capacity(), this.map.loadFactor()); } @Override public IntIterator iterator() { return this.map.keySet().iterator(); } /** * Returns the number of elements in this set (its cardinality). * * @return the number of elements in this set (its cardinality). */ @Override public int size() { return this.map.size(); } /** * Returns {@code true} if this set contains the specified element. * * @param o element whose presence in this set is to be tested. * @return {@code true} if this set contains the specified element. */ @Override public boolean contains(int o) { return this.map.containsKey(o); } /** * Adds the specified element to this set if it is not already * present. * * @param o element to be added to this set. * @return {@code true} if the set did not already contain the specified * element. */ @Override public boolean add(int o) { return this.map.put(o, PRESENT) == null; } /** * Removes the given element from this set if it is present. * * @param o object to be removed from this set, if present. * @return {@code true} if the set contained the specified element. */ @Override public boolean remove(int o) { return this.map.remove(o) == PRESENT; } /** * Removes all of the elements from this set. */ @Override public void clear() { this.map.clear(); } }