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

org.gridgain.grid.util.GridBoundedLinkedHashSet Maven / Gradle / Ivy

/* 
 Copyright (C) GridGain Systems. All Rights Reserved.
 
 Licensed 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.gridgain.grid.util;

import java.util.*;

/**
 * Hash table and linked list implementation of the {@code Set} interface,
 * with predictable iteration order. This implementation differs from
 * {@code HashSet} in that it maintains a doubly-linked list running through
 * all of its entries. This linked list defines the iteration ordering,
 * which is the order in which elements were inserted into the set
 * (insertion-order). Note that insertion order is not affected
 * if an element is re-inserted into the set. (An element {@code e}
 * is reinserted into a set {@code s} if {@code s.add(e)} is invoked when
 * {@code s.contains(e)} would return {@code true} immediately prior to
 * the invocation.)
 * 

* HashSet is also has maximum capacity. When it is reached the * newest elements supersede eldest ones. * * @param Set element. * */ public class GridBoundedLinkedHashSet extends GridSerializableSet implements Cloneable { /** */ private static final Object FAKE = Boolean.TRUE; /** */ @SuppressWarnings({"TransientFieldNotInitialized", "CollectionDeclaredAsConcreteClass"}) private transient HashMap map; /** * Constructs a new, empty set; the backing {@code LinkedHashMap} * instance has default initial capacity (16) and load factor (0.75). * * @param maxCap Maximum set capacity. */ public GridBoundedLinkedHashSet(int maxCap) { assert maxCap > 0; map = new GridBoundedLinkedHashMap<>(maxCap); } /** * Constructs a new set containing the elements in the specified collection. * The {@code LinkedHashMap} is created with default load factor (0.75) * and an initial capacity sufficient to contain the elements in the specified * collection. * * @param c The collection whose elements are to be placed into this set. * @param maxCap Maximum set capacity. */ public GridBoundedLinkedHashSet(Collection c, int maxCap) { assert maxCap > 0; map = new GridBoundedLinkedHashMap<>(Math.max((int) (c.size() / 0.75f) + 1, 16), maxCap); addAll(c); } /** * Constructs a new, empty set; the backing {@code LinkedHashMap} * instance has the specified initial capacity and the specified load factor. * * @param initCap The initial capacity of the hash map. * @param maxCap Maximum set capacity. * @param loadFactor the Load factor of the hash map. */ public GridBoundedLinkedHashSet(int initCap, int maxCap, float loadFactor) { assert maxCap > 0; map = new GridBoundedLinkedHashMap<>(initCap, maxCap, loadFactor); } /** * Constructs a new, empty set; the backing {@code LinkedHashMap} * instance has the specified initial capacity and the specified load factor. * * @param initCap The initial capacity of the hash map. * @param maxCap Maximum set capacity. * @param loadFactor the Load factor of the hash map. * @param accessOrder {@code True} for access order, {@code false} for insertion order. */ public GridBoundedLinkedHashSet(int initCap, int maxCap, float loadFactor, boolean accessOrder) { assert maxCap > 0; map = new GridBoundedLinkedHashMap<>(initCap, maxCap, loadFactor, accessOrder); } /** * Constructs a new, empty set; the backing {@code LinkedHashHashMap} * instance has the specified initial capacity and default load factor, which * is {@code 0.75}. * * @param initCap The initial capacity of the hash table. * @param maxCap Maximum capacity. */ public GridBoundedLinkedHashSet(int initCap, int maxCap) { assert maxCap > 0; map = new GridBoundedLinkedHashMap<>(initCap, maxCap); } /** * Returns an iterator over the elements in this set. The elements are * returned in no particular order. * * @return An iterator over the elements in this set. * @see ConcurrentModificationException */ @Override public Iterator iterator() { return 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 map.size(); } /** * Returns {@code true} if this set contains no elements. * * @return {@code True} if this set contains no elements. */ @Override public boolean isEmpty() { return map.isEmpty(); } /** * 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. */ @SuppressWarnings({"SuspiciousMethodCalls"}) @Override public boolean contains(Object o) { return 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(E o) { return map.put(o, FAKE) == null; } /** * Removes the specified 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. */ @SuppressWarnings({"ObjectEquality"}) @Override public boolean remove(Object o) { return map.remove(o) == FAKE; } /** * Removes all of the elements from this set. */ @Override public void clear() { map.clear(); } /** * Returns a shallow copy of this {@code GridBoundedLinkedHashSet} * instance: the elements themselves are not cloned. * * @return a shallow copy of this set. * @throws CloneNotSupportedException Thrown if cloning is not supported. */ @SuppressWarnings("unchecked") @Override public Object clone() throws CloneNotSupportedException { GridBoundedLinkedHashSet newSet = (GridBoundedLinkedHashSet)super.clone(); newSet.map = (HashMap)map.clone(); return newSet; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy