OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 182
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils.cc;
14
 package org.openconcerto.utils.cc;
15
 
15
 
16
import java.util.AbstractSet;
-
 
17
import java.util.Collection;
16
import java.util.Collection;
18
import java.util.Collections;
-
 
19
import java.util.Iterator;
-
 
20
import java.util.LinkedList;
17
import java.util.LinkedList;
21
import java.util.List;
-
 
22
 
18
 
23
/**
19
/**
24
 * An IdentitySet maintaining insertion order.
20
 * An IdentitySet maintaining insertion order.
25
 * 
21
 * 
26
 * @param <E> the type of elements maintained by this set
22
 * @param <E> the type of elements maintained by this set
27
 * @see IdentityHashSet
23
 * @see IdentityHashSet
28
 */
24
 */
29
public final class LinkedIdentitySet<E> extends AbstractSet<E> implements IdentitySet<E>, Cloneable {
25
public final class LinkedIdentitySet<E> extends ListIdentitySet<E> implements Cloneable {
30
 
-
 
31
    private final LinkedList<E> list;
-
 
32
 
26
 
33
    public LinkedIdentitySet() {
27
    public LinkedIdentitySet() {
34
        this.list = new LinkedList<E>();
-
 
35
    }
-
 
36
 
-
 
37
    public LinkedIdentitySet(Collection<? extends E> c) {
-
 
38
        this.list = new LinkedList<E>();
-
 
39
        this.addAll(c);
-
 
40
    }
-
 
41
 
-
 
42
    public final List<E> getList() {
-
 
43
        return Collections.unmodifiableList(this.list);
-
 
44
    }
-
 
45
 
-
 
46
    @Override
-
 
47
    public boolean add(E e) {
-
 
48
        if (this.contains(e))
-
 
49
            return false;
-
 
50
        else {
28
        super();
51
            this.list.add(e);
-
 
52
            return true;
-
 
53
        }
-
 
54
    }
-
 
55
 
-
 
56
    @Override
-
 
57
    public boolean addAll(Collection<? extends E> c) {
-
 
58
        // let the super which calls add() since we don't want to build a map to use Map.addAll()
-
 
59
        return super.addAll(c);
-
 
60
    }
-
 
61
 
-
 
62
    @Override
-
 
63
    public void clear() {
-
 
64
        this.list.clear();
-
 
65
    }
-
 
66
 
-
 
67
    @Override
-
 
68
    public int size() {
-
 
69
        return this.list.size();
-
 
70
    }
-
 
71
 
-
 
72
    @Override
-
 
73
    public boolean isEmpty() {
-
 
74
        return this.list.isEmpty();
-
 
75
    }
-
 
76
 
-
 
77
    @Override
-
 
78
    public Iterator<E> iterator() {
-
 
79
        return this.list.iterator();
-
 
80
    }
-
 
81
 
-
 
82
    @Override
-
 
83
    public boolean contains(Object o) {
-
 
84
        Iterator<E> e = iterator();
-
 
85
        while (e.hasNext())
-
 
86
            if (o == e.next())
-
 
87
                return true;
-
 
88
        return false;
-
 
89
    }
-
 
90
 
-
 
91
    @Override
-
 
92
    public boolean containsAll(Collection<?> c) {
-
 
93
        // let the super which calls contains()
-
 
94
        return super.containsAll(c);
-
 
95
    }
29
    }
96
 
30
 
97
    @Override
-
 
98
    public boolean remove(Object o) {
31
    public LinkedIdentitySet(int expectedSize) {
99
        Iterator<E> e = iterator();
-
 
100
        while (e.hasNext()) {
-
 
101
            if (o == e.next()) {
-
 
102
                e.remove();
32
        super(expectedSize);
103
                return true;
-
 
104
            }
-
 
105
        }
-
 
106
        return false;
-
 
107
    }
33
    }
108
 
34
 
109
    /*
-
 
110
     * (From IdentityHashMap) Must revert from AbstractSet's impl to AbstractCollection's, as the
-
 
111
     * former contains an optimization that results in incorrect behavior when c is a smaller
-
 
112
     * "normal" (non-identity-based) Set.
-
 
113
     */
-
 
114
    @Override
-
 
115
    public boolean removeAll(Collection<?> c) {
35
    public LinkedIdentitySet(Collection<? extends E> c) {
116
        boolean modified = false;
-
 
117
        for (Iterator<E> i = iterator(); i.hasNext();) {
-
 
118
            if (c.contains(i.next())) {
-
 
119
                i.remove();
-
 
120
                modified = true;
-
 
121
            }
36
        super(c);
122
        }
-
 
123
        return modified;
-
 
124
    }
-
 
125
 
-
 
126
    @Override
-
 
127
    public boolean retainAll(Collection<?> c) {
-
 
128
        // let the super which calls remove()
-
 
129
        return super.retainAll(c);
-
 
130
    }
-
 
131
 
-
 
132
    @Override
-
 
133
    public Object[] toArray() {
-
 
134
        return this.list.toArray();
-
 
135
    }
37
    }
136
 
38
 
137
    @Override
39
    @Override
138
    public <T> T[] toArray(T[] a) {
40
    protected LinkedList<E> newList(int expectedSize) {
139
        return this.list.toArray(a);
41
        return new LinkedList<>();
140
    }
42
    }
141
 
43
 
142
    /**
44
    /**
143
     * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements themselves are not
45
     * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements themselves are not
144
     * cloned.
46
     * cloned.
145
     * 
47
     * 
146
     * @return a shallow copy of this set
48
     * @return a shallow copy of this set
147
     */
49
     */
-
 
50
    @Override
148
    public Object clone() {
51
    public Object clone() {
149
        return new LinkedIdentitySet<E>(this);
52
        return new LinkedIdentitySet<E>(this);
150
    }
53
    }
151
 
54
 
152
}
55
}