OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.cc;
15
 
16
import java.util.AbstractSet;
17
import java.util.Collection;
18
import java.util.IdentityHashMap;
19
import java.util.Iterator;
20
 
21
/**
22
 * This class implements the <tt>Set</tt> interface with a hash table, using reference-equality in
23
 * place of object-equality when comparing items. In other words, in an <tt>IdentityHashSet</tt>,
24
 * two keys <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if <tt>(k1==k2)</tt>. (In
25
 * normal <tt>Set</tt> implementations (like <tt>HashSet</tt>) two keys <tt>k1</tt> and <tt>k2</tt>
26
 * are considered equal if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
27
 *
28
 * @param <E> the type of elements maintained by this set
29
 */
30
public final class IdentityHashSet<E> extends AbstractSet<E> implements IdentitySet<E>, Cloneable {
31
 
182 ilm 32
    static private final Object VALUE = new Object();
33
 
17 ilm 34
    private final IdentityHashMap<E, Object> map;
35
 
36
    public IdentityHashSet() {
37
        this.map = new IdentityHashMap<E, Object>();
38
    }
39
 
40
    public IdentityHashSet(int expectedMaxSize) {
41
        this.map = new IdentityHashMap<E, Object>(expectedMaxSize);
42
    }
43
 
44
    public IdentityHashSet(Collection<? extends E> c) {
45
        this.map = new IdentityHashMap<E, Object>(Math.max((int) (c.size() / .75f) + 1, 16));
46
        this.addAll(c);
47
    }
48
 
49
    @Override
50
    public boolean add(E e) {
182 ilm 51
        return this.map.put(e, VALUE) != VALUE;
17 ilm 52
    }
53
 
54
    @Override
55
    public boolean addAll(Collection<? extends E> c) {
56
        // let the super which calls add() since we don't want to build a map to use Map.addAll()
57
        return super.addAll(c);
58
    }
59
 
60
    @Override
61
    public void clear() {
62
        this.map.clear();
63
    }
64
 
65
    @Override
66
    public int size() {
67
        return this.map.size();
68
    }
69
 
70
    @Override
71
    public boolean isEmpty() {
72
        return this.map.isEmpty();
73
    }
74
 
75
    @Override
76
    public Iterator<E> iterator() {
77
        return this.map.keySet().iterator();
78
    }
79
 
80
    @Override
81
    public boolean contains(Object o) {
182 ilm 82
        return this.map.containsKey(o);
17 ilm 83
    }
84
 
85
    @Override
86
    public boolean containsAll(Collection<?> c) {
87
        return this.map.keySet().containsAll(c);
88
    }
89
 
90
    @Override
91
    public boolean remove(Object o) {
182 ilm 92
        return this.map.remove(o) == VALUE;
17 ilm 93
    }
94
 
95
    @Override
96
    public boolean removeAll(Collection<?> c) {
97
        return this.map.keySet().removeAll(c);
98
    }
99
 
100
    @Override
101
    public boolean retainAll(Collection<?> c) {
102
        return this.map.keySet().retainAll(c);
103
    }
104
 
105
    @Override
106
    public Object[] toArray() {
107
        return this.map.keySet().toArray();
108
    }
109
 
110
    @Override
111
    public <T> T[] toArray(T[] a) {
112
        return this.map.keySet().toArray(a);
113
    }
114
 
182 ilm 115
    // use System.identityHashCode()
116
    @Override
117
    public int hashCode() {
118
        return this.map.keySet().hashCode();
119
    }
120
 
121
    // equals() uses containsAll which is correct
122
 
17 ilm 123
    /**
124
     * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements themselves are not
125
     * cloned.
126
     *
127
     * @return a shallow copy of this set
128
     */
129
    public Object clone() {
130
        return new IdentityHashSet<E>(this);
131
    }
132
 
133
}