Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8ae69973e0e45eb442d67ceb56f60f5b62e17a2d (plain) (blame)
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
/*
 * 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.felix.resolver.util;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

public class CopyOnWriteSet<E> implements Set<E>, Cloneable {

    Object[] data;

    public CopyOnWriteSet() {
        data = new Object[0];
    }

    public CopyOnWriteSet(CopyOnWriteSet<? extends E> col) {
        data = col.data;
    }

    public CopyOnWriteSet(Collection<? extends E> col) {
        data = col.toArray(new Object[col.size()]);
    }

    public Iterator<E> iterator() {
        return new Iterator<E>() {
            int idx = 0;
            public boolean hasNext() {
                return idx < data.length;
            }
            @SuppressWarnings("unchecked")
            public E next() {
                return (E) data[idx++];
            }
            public void remove() {
                CopyOnWriteSet.this.remove(--idx);
            }
        };
    }

    public int size() {
        return data.length;
    }

    public boolean add(E e) {
        Object[] d = data;
        if (d.length == 0) {
            data = new Object[] {e};
        } else {
            for (Object o : d) {
                if (o == null ? e == null : o.equals(e)) {
                    return false;
                }
            }
            Object[] a = new Object[d.length + 1];
            System.arraycopy(d, 0, a, 0, d.length);
            a[d.length] = e;
            data = a;
        }
        return true;
    }

    private void remove(int index) {
        Object[] d = data;
        int len = d.length;
        Object[] a = new Object[len - 1];
        int numMoved = len - index - 1;
        if (index > 0) {
            System.arraycopy(d, 0, a, 0, index);
        }
        if (numMoved > 0) {
            System.arraycopy(d, index + 1, a, index, numMoved);
        }
        data = a;
    }

    public Object[] toArray() {
        return data.clone();
    }

    @SuppressWarnings("unchecked")
    public <T> T[] toArray(T[] a) {
        int size = data.length;
        if (a.length < size)
            // Make a new array of a's runtime type, but my contents:
            return (T[]) copyOf(data, size, a.getClass());
        System.arraycopy(data, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof CopyOnWriteSet)) {
            return false;
        }
        Object[] o1 = data;
        @SuppressWarnings("rawtypes")
        Object[] o2 = ((CopyOnWriteSet) o).data;
        if (o1 == o2) {
            return true;
        }
        int l = o1.length;
        if (l != o2.length) {
            return false;
        }
        loop:
        for (int i = l; i-- > 0;) {
            Object v1 = o1[i];
            for (int j = l; j-- > 0;) {
                Object v2 = o2[j];
                if (v1 == v2)
                    continue loop;
                if (v1 != null && v1.equals(v2))
                    continue loop;
            }
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(data);
    }

    /**
     * Clone this object
     *
     * @return a cloned object.
     */
    @Override
    @SuppressWarnings("unchecked")
    public CopyOnWriteSet<E> clone() {
        try {
            return (CopyOnWriteSet<E>) super.clone();
        } catch (CloneNotSupportedException exc) {
            InternalError e = new InternalError();
            e.initCause(exc);
            throw e; //should never happen since we are cloneable
        }
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public boolean contains(Object o) {
        throw new UnsupportedOperationException();
    }

    public boolean remove(Object o) {
        int index;
        if ((index = indexOf(o, data, data.length)) >= 0) {
            remove(index);
            return true;
        }
        return false;
    }

    private static int indexOf(Object o, Object[] d, int len) {
        if (o == null) {
            for (int i = len; i-- > 0;) {
                if (d[i] == null)
                    return i;
            }
        } else {
            for (int i = len; i-- > 0;) {
                if (o.equals(d[i]))
                    return i;
            }
        }
        return -1;
    }

    public boolean containsAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    public boolean addAll(Collection<? extends E> c) {
        Object[] cs = c.toArray();
        if (cs.length == 0)
            return false;
        Object[] elements = data;
        int len = elements.length;
        int added = 0;
        // uniquify and compact elements in cs
        for (int i = 0; i < cs.length; ++i) {
            Object e = cs[i];
            if (indexOf(e, elements, len) < 0 &&
                    indexOf(e, cs, added) < 0)
                cs[added++] = e;
        }
        if (added > 0) {
            Object[] newElements = copyOf(elements, len + added);
            System.arraycopy(cs, 0, newElements, len, added);
            data = newElements;
            return true;
        }
        return false;
    }

    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    public void clear() {
        throw new UnsupportedOperationException();
    }

    @SuppressWarnings("unchecked")
    public static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    }

    @SuppressWarnings("unchecked")
    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        T[] copy;
        if ((Object) newType == Object[].class) {
            copy = (T[]) new Object[newLength];
        } else {
            copy = (T[]) Array.newInstance(newType.getComponentType(), newLength);
        }
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }
}

Back to the top