summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/util/VectorSet.java
blob: f1665c74d25b19c8ca151661af91674035da9ab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.tools.ant.util;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.Vector;

/**
 * Subclass of Vector that won't store duplicate entries and shows
 * HashSet's constant time performance characteristics for the
 * contains method.
 *
 * <p>This is not a general purpose class but has been written because
 * the protected members of {@link
 * org.apache.tools.ant.DirectoryScanner DirectoryScanner} prohibited
 * later revisions from using a more efficient collection.</p>
 *
 * <p>Methods are synchronized to keep Vector's contract.</p>
 *
 * @since Ant 1.8.0
 */
public final class VectorSet<E> extends Vector<E> {
    private static final long serialVersionUID = 1L;

    private final HashSet<E> set = new HashSet<E>();

    public VectorSet() {
        super();
    }

    public VectorSet(int initialCapacity) {
        super(initialCapacity);
    }

    public VectorSet(int initialCapacity, int capacityIncrement) {
        super(initialCapacity, capacityIncrement);
    }

    public VectorSet(Collection<? extends E> c) {
        if (c != null) {
            c.forEach(this::add);
        }
    }

    @Override
    public synchronized boolean add(E o) {
        if (!set.contains(o)) {
            doAdd(size(), o);
            return true;
        }
        return false;
    }

    /**
     * This implementation may not add the element at the given index
     * if it is already contained in the collection.
     */
    @Override
    public void add(int index, E o) {
        doAdd(index, o);
    }

    private synchronized void doAdd(int index, E o) {
        // Vector.add seems to delegate to insertElementAt, but this
        // is not documented so we may better implement it ourselves
        if (set.add(o)) {
            int count = size();
            ensureCapacity(count + 1);
            if (index != count) {
                System.arraycopy(elementData, index, elementData, index + 1,
                                 count - index);
            }
            elementData[index] = o;
            elementCount++;
        }
    }

    @Override
    public synchronized void addElement(E o) {
        doAdd(size(), o);
    }

    @Override
    public synchronized boolean addAll(Collection<? extends E> c) {
        boolean changed = false;
        for (E e : c) {
            changed |= add(e);
        }
        return changed;
    }

    /**
     * This implementation may not add all elements at the given index
     * if any of them are already contained in the collection.
     */
    @Override
    public synchronized boolean addAll(int index, Collection<? extends E> c) {
        LinkedList<E> toAdd = new LinkedList<>();
        for (E e : c) {
            if (set.add(e)) {
                toAdd.add(e);
            }
        }
        if (toAdd.isEmpty()) {
            return false;
        }
        int count = size();
        ensureCapacity(count + toAdd.size());
        if (index != count) {
            System.arraycopy(elementData, index, elementData, index + toAdd.size(),
                             count - index);
        }
        for (Object o : toAdd) {
            elementData[index++] = o;
        }
        elementCount += toAdd.size();
        return true;
    }

    @Override
    public synchronized void clear() {
        super.clear();
        set.clear();
    }

    @Override
    public VectorSet<E> clone() {
        @SuppressWarnings("unchecked")
        final VectorSet<E> vs = (VectorSet<E>) super.clone();
        vs.set.addAll(set);
        return vs;
    }

    @Override
    public synchronized boolean contains(Object o) {
        return set.contains(o);
    }

    @Override
    public synchronized boolean containsAll(Collection<?> c) {
        return set.containsAll(c);
    }

    @Override
    public void insertElementAt(E o, int index) {
        doAdd(index, o);
    }

    @Override
    public synchronized E remove(int index) {
        E o = get(index);
        remove(o);
        return o;
    }

    @Override
    public boolean remove(Object o) {
        return doRemove(o);
    }

    private synchronized boolean doRemove(Object o) {
        // again, remove seems to delegate to removeElement, but we
        // shouldn't trust it
        if (set.remove(o)) {
            int index = indexOf(o);
            if (index < elementData.length - 1) {
                System.arraycopy(elementData, index + 1, elementData, index,
                                 elementData.length - index - 1);
            }
            elementCount--;
            return true;
        }
        return false;
    }

    @Override
    public synchronized boolean removeAll(Collection<?> c) {
        boolean changed = false;
        for (Object o : c) {
            changed |= remove(o);
        }
        return changed;
    }

    @Override
    public synchronized void removeAllElements() {
        set.clear();
        super.removeAllElements();
    }

    @Override
    public boolean removeElement(Object o) {
        return doRemove(o);
    }

    @Override
    public synchronized void removeElementAt(int index) {
        remove(get(index));
    }

    @Override
    public synchronized void removeRange(final int fromIndex, int toIndex) {
        while (toIndex > fromIndex) {
            remove(--toIndex);
        }
    }

    @Override
    public synchronized boolean retainAll(Collection<?> c) {
        if (!(c instanceof Set)) {
            c = new HashSet<>(c);
        }
        LinkedList<E> l = new LinkedList<>();
        for (E o : this) {
            if (!c.contains(o)) {
                l.addLast(o);
            }
        }
        if (!l.isEmpty()) {
            removeAll(l);
            return true;
        }
        return false;
    }

    @Override
    public synchronized E set(int index, E o) {
        E orig = get(index);
        if (set.add(o)) {
            elementData[index] = o;
            set.remove(orig);
        } else {
            int oldIndexOfO = indexOf(o);
            remove(o);
            remove(orig);
            add(oldIndexOfO > index ? index : index - 1, o);
        }
        return orig;
    }

    @Override
    public void setElementAt(E o, int index) {
        set(index, o);
    }

}