/* * This file is part of NucleusFramework for Bukkit, licensed under the MIT License (MIT). * * Copyright (c) JCThePants (www.jcwhatever.com) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.jcwhatever.nucleus.collections; import com.jcwhatever.nucleus.collections.wrap.SetWrapper; import java.util.Collection; import java.util.Collections; import java.util.Set; import java.util.WeakHashMap; /** * A hash {@link java.util.Set} with weak referenced values. */ public class WeakHashSet<E> extends SetWrapper<E> { private final transient Set<E> _set; /** * Constructor. */ public WeakHashSet() { this(10); } /** * Constructor. * * @param size The initial capacity. */ public WeakHashSet(int size) { _set = Collections.newSetFromMap(new WeakHashMap<E, Boolean>(size)); } /** * Constructor. * * @param collection The initial collection. */ public WeakHashSet(Collection<E> collection) { _set = Collections.newSetFromMap(new WeakHashMap<E, Boolean>(collection.size() + 5)); _set.addAll(collection); } @Override protected Set<E> set() { return _set; } }