nofs /Java.NET/JavApi Commons collections (Apache Port)/org.apache.commons.collections.list.SetUniqueList.cs

Language C# Lines 378
MD5 Hash 1d862fc7c35110be51fdfe7e74ee5c36 Estimated Cost $4,881 (why?)
Repository git://github.com/gadfly/nofs.git View Raw File View Project SPDX
  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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 *  Licensed 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.
 *  
 */

using System;
using java = biz.ritter.javapi;
using org.apache.commons.collections.iterators;
using org.apache.commons.collections.set;

namespace org.apache.commons.collections.list
{

    /**
     * Decorates a <code>List</code> to ensure that no duplicates are present
     * much like a <code>Set</code>.
     * <p>
     * The <code>List</code> interface makes certain assumptions/requirements.
     * This implementation breaks these in certain ways, but this is merely the
     * result of rejecting duplicates.
     * Each violation is explained in the method, but it should not affect you.
     * Bear in mind that Sets require immutable objects to function correctly.
     * <p>
     * The {@link org.apache.commons.collections.set.ListOrderedSet ListOrderedSet}
     * class provides an alternative approach, by wrapping an existing Set and
     * retaining insertion order in the iterator.
     * <p>
     * This class is java.io.Serializable from Commons Collections 3.1.
     *
     * @since Commons Collections 3.0
     * @version $Revision$ $Date$
     * 
     * @author Matthew Hawthorne
     * @author Stephen Colebourne
     * @author Tom Dunham
     */
    [Serializable]
    public class SetUniqueList : AbstractSerializableListDecorator
    {

        /** Serialization version */
        private static readonly long serialVersionUID = 7196982186153478694L;

        /**
         * Internal Set to maintain uniqueness.
         */
        protected readonly java.util.Set<Object> setJ;

        /**
         * Factory method to create a SetList using the supplied list to retain order.
         * <p>
         * If the list contains duplicates, these are removed (first indexed one kept).
         * A <code>HashSet</code> is used for the set behaviour.
         * 
         * @param list  the list to decorate, must not be null
         * @throws IllegalArgumentException if list is null
         */
        public static SetUniqueList decorate(java.util.List<Object> list)
        {
            if (list == null)
            {
                throw new java.lang.IllegalArgumentException("List must not be null");
            }
            if (list.isEmpty())
            {
                return new SetUniqueList(list, new java.util.HashSet<Object>());
            }
            else
            {
                java.util.List<Object> temp = new java.util.ArrayList<Object>(list);
                list.clear();
                SetUniqueList sl = new SetUniqueList(list, new java.util.HashSet<Object>());
                sl.addAll(temp);
                return sl;
            }
        }

        //-----------------------------------------------------------------------
        /**
         * Constructor that wraps (not copies) the List and specifies the set to use.
         * <p>
         * The set and list must both be correctly initialised to the same elements.
         * 
         * @param set  the set to decorate, must not be null
         * @param list  the list to decorate, must not be null
         * @throws IllegalArgumentException if set or list is null
         */
        protected internal SetUniqueList(java.util.List<Object> list, java.util.Set<Object> set)
            : base(list)
        {
            if (set == null)
            {
                throw new java.lang.IllegalArgumentException("Set must not be null");
            }
            this.setJ = set;
        }

        //-----------------------------------------------------------------------
        /**
         * Gets an unmodifiable view as a Set.
         * 
         * @return an unmodifiable set view
         */
        public virtual java.util.Set<Object> asSet()
        {
            return UnmodifiableSet.decorate(setJ);
        }

        //-----------------------------------------------------------------------
        /**
         * Adds an element to the list if it is not already present.
         * <p>
         * <i>(Violation)</i>
         * The <code>List</code> interface requires that this method returns
         * <code>true</code> always. However this class may return <code>false</code>
         * because of the <code>Set</code> behaviour.
         * 
         * @param object the object to add
         * @return true if object was added
         */
        public override bool add(Object obj)
        {
            // gets initial size
            int sizeBefore = size();

            // adds element if unique
            add(size(), obj);

            // compares sizes to detect if collection changed
            return (sizeBefore != size());
        }

        /**
         * Adds an element to a specific index in the list if it is not already present.
         * <p>
         * <i>(Violation)</i>
         * The <code>List</code> interface makes the assumption that the element is
         * always inserted. This may not happen with this implementation.
         * 
         * @param index  the index to insert at
         * @param object  the object to add
         */
        public override void add(int index, Object obj)
        {
            // adds element if it is not contained already
            if (setJ.contains(obj) == false)
            {
                base.add(index, obj);
                setJ.add(obj);
            }
        }

        /**
         * Adds an element to the end of the list if it is not already present.
         * <p>
         * <i>(Violation)</i>
         * The <code>List</code> interface makes the assumption that the element is
         * always inserted. This may not happen with this implementation.
         * 
         * @param coll  the collection to add
         */
        public override bool addAll(java.util.Collection<Object> coll)
        {
            return addAll(size(), coll);
        }

        /**
         * Adds a collection of objects to the end of the list avoiding duplicates.
         * <p>
         * Only elements that are not already in this list will be added, and
         * duplicates from the specified collection will be ignored.
         * <p>
         * <i>(Violation)</i>
         * The <code>List</code> interface makes the assumption that the elements
         * are always inserted. This may not happen with this implementation.
         * 
         * @param index  the index to insert at
         * @param coll  the collection to add in iterator order
         * @return true if this collection changed
         */
        public override bool addAll(int index, java.util.Collection<Object> coll)
        {
            // gets initial size
            int sizeBefore = size();

            // adds all elements
            for (java.util.Iterator<Object> it = coll.iterator(); it.hasNext(); )
            {
                add(it.next());
            }

            // compares sizes to detect if collection changed
            return sizeBefore != size();
        }

        //-----------------------------------------------------------------------
        /**
         * Sets the value at the specified index avoiding duplicates.
         * <p>
         * The object is set into the specified index.
         * Afterwards, any previous duplicate is removed
         * If the object is not already in the list then a normal set occurs.
         * If it is present, then the old version is removed.
         * 
         * @param index  the index to insert at
         * @param object  the object to set
         * @return the previous object
         */
        public override Object set(int index, Object obj)
        {
            int pos = indexOf(obj);
            Object removed = base.set(index, obj);
            if (pos == -1 || pos == index)
            {
                return removed;
            }

            // the object is already in the uniq list
            // (and it hasn't been swapped with itself)
            base.remove(pos);  // remove the duplicate by index
            setJ.remove(removed);  // remove the item deleted by the set
            return removed;  // return the item deleted by the set
        }

        public override bool remove(Object obj)
        {
            bool result = base.remove(obj);
            setJ.remove(obj);
            return result;
        }

        public override Object remove(int index)
        {
            Object result = base.remove(index);
            setJ.remove(result);
            return result;
        }

        public override bool removeAll(java.util.Collection<Object> coll)
        {
            bool result = base.removeAll(coll);
            setJ.removeAll(coll);
            return result;
        }

        public override bool retainAll(java.util.Collection<Object> coll)
        {
            bool result = base.retainAll(coll);
            setJ.retainAll(coll);
            return result;
        }

        public override void clear()
        {
            base.clear();
            setJ.clear();
        }

        public override bool contains(Object obj)
        {
            return setJ.contains(obj);
        }

        public override bool containsAll(java.util.Collection<Object> coll)
        {
            return setJ.containsAll(coll);
        }

        public override java.util.Iterator<Object> iterator()
        {
            return new SetListIterator(base.iterator(), setJ);
        }

        public override java.util.ListIterator<Object> listIterator()
        {
            return new SetListListIterator(base.listIterator(), setJ);
        }

        public override java.util.ListIterator<Object> listIterator(int index)
        {
            return new SetListListIterator(base.listIterator(index), setJ);
        }

        public override java.util.List<Object> subList(int fromIndex, int toIndex)
        {
            return new SetUniqueList(base.subList(fromIndex, toIndex), setJ);
        }

        //-----------------------------------------------------------------------
        /**
         * Inner class iterator.
         */
        internal class SetListIterator : AbstractIteratorDecorator
        {

            protected java.util.Set<Object> set;
            protected Object last = null;

            protected internal SetListIterator(java.util.Iterator<Object> it, java.util.Set<Object> set)
                : base(it)
            {
                this.set = set;
            }

            public override Object next()
            {
                last = base.next();
                return last;
            }

            public override void remove()
            {
                base.remove();
                set.remove(last);
                last = null;
            }
        }

        /**
         * Inner class iterator.
         */
        internal class SetListListIterator : AbstractListIteratorDecorator
        {

            protected java.util.Set<Object> setJ;
            protected Object last = null;

            protected internal SetListListIterator(java.util.ListIterator<Object> it, java.util.Set<Object> set)
                : base(it)
            {
                this.setJ = set;
            }

            public override Object next()
            {
                last = base.next();
                return last;
            }

            public override Object previous()
            {
                last = base.previous();
                return last;
            }

            public override void remove()
            {
                base.remove();
                setJ.remove(last);
                last = null;
            }

            public override void add(Object obj)
            {
                if (setJ.contains(obj) == false)
                {
                    base.add(obj);
                    setJ.add(obj);
                }
            }

            public override void set(Object obj)
            {
                throw new java.lang.UnsupportedOperationException("ListIterator does not support set");
            }
        }
    }
}
Back to Top