Subversion Repository Public Repository

litesoft

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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.util;

import java.util.*;

/**
 * In implementing classes, any bulk operations should be synchronized and probably call LLincModificationVersion!
 */
public abstract class NoEqualsCollection<E, C extends Comparable<C>> extends NoEqualsBase<E, C> implements Collection<E>
{
    private int mModificationVersion = 0;
    private Wrapper<E, C>[] mCachedWrappedEntries = null;

    protected NoEqualsCollection( NoEqualsHelper<E, C> pHelper )
    {
        super( pHelper );
    }

    abstract protected Collection<Wrapper<E, C>> LLgetCollection();

    protected final synchronized Wrapper<E, C>[] getWrappedEntries()
    {
        if ( mCachedWrappedEntries == null )
        {
            mCachedWrappedEntries = LLgetCollection().toArray( array( size() ) );
        }
        return mCachedWrappedEntries;
    }

    protected final synchronized void LLincModificationVersion()
    {
        mCachedWrappedEntries = null;
        mModificationVersion++;
    }

    protected final synchronized int LLgetModificationVersion()
    {
        return mModificationVersion;
    }

    /**
     * @param pWrap !null
     */
    protected final boolean LLadd( Wrapper<E, C> pWrap )
    {
        return LLgetCollection().add( pWrap );
    }

    /**
     * @param pWrap !null
     */
    protected final boolean LLremove( Wrapper<E, C> pWrap )
    {
        return LLgetCollection().remove( pWrap );
    }

    protected final int LLsize()
    {
        return LLgetCollection().size();
    }

    protected final void LLclear()
    {
        LLgetCollection().clear();
    }

    private boolean LLcontains( Wrapper<E, C>[] pWrappers, Object o )
    {
        E zObject = castNotNull( o );
        return LLobjectIsIn( zObject, pWrappers );
    }

    protected final boolean LLobjectIsIn( E pNotNullObject, Wrapper<E, C>[] pWrappers )
    {
        C toFind = mHelper.getProxyValue( pNotNullObject );
        for ( Wrapper<E, C> zWrapper : pWrappers )
        {
            if ( (pNotNullObject == zWrapper.getObject()) || areEqual( toFind, zWrapper.getProxyValue() ) )
            {
                return true;
            }
        }
        return false;
    }

    @SuppressWarnings({"ConstantConditions"})
    protected final E[] toTypedArray( Wrapper<E, C>[] pWrappers )
    {
        int zLength = (pWrappers != null) ? pWrappers.length : 0;
        E[] zEntries = mHelper.createArray( zLength );
        for ( int i = 0; i < zLength; i++ )
        {
            zEntries[i] = pWrappers[i].getObject();
        }
        return zEntries;
    }

    protected final boolean LLmodified( boolean pModified )
    {
        if ( pModified )
        {
            LLincModificationVersion();
        }
        return pModified;
    }

    /**
     * Returns the number of elements in this collection.  If this collection
     * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
     * <tt>Integer.MAX_VALUE</tt>.
     *
     * @return the number of elements in this collection
     */
    @Override
    public final synchronized int size()
    {
        return LLsize();
    }

    /**
     * Removes all of the elements from this collection (optional operation).
     * The collection will be empty after this method returns.
     *
     * @throws UnsupportedOperationException if the <tt>clear</tt> operation
     *                                       is not supported by this collection
     */
    @Override
    public final synchronized void clear()
    {
        if ( 0 != LLsize() )
        {
            LLclear();
            LLincModificationVersion();
        }
    }

    /**
     * Returns <tt>true</tt> if this collection contains no elements.
     *
     * @return <tt>true</tt> if this collection contains no elements
     */
    @Override
    public final boolean isEmpty()
    {
        return size() == 0;
    }

    /**
     * Returns <tt>true</tt> if this collection contains the specified element.
     * More formally, returns <tt>true</tt> if and only if this collection
     * contains at least one element <tt>e</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
     *
     * @param o element whose presence in this collection is to be tested
     *
     * @return <tt>true</tt> if this collection contains the specified
     *         element
     *
     * @throws ClassCastException   if the type of the specified element
     *                              is incompatible with this collection (optional)
     * @throws NullPointerException if the specified element is null and this
     *                              collection does not permit null elements (optional)
     */
    @Override
    public final synchronized boolean contains( Object o )
    {
        return LLcontains( getWrappedEntries(), o );
    }

    /**
     * Returns an array containing all of the elements in this collection.
     * If this collection makes any guarantees as to what order its elements
     * are returned by its iterator, this method must return the elements in
     * the same order.
     * <p/>
     * <p>The returned array will be "safe" in that no references to it are
     * maintained by this collection.  (In other words, this method must
     * allocate a new array even if this collection is backed by an array).
     * The caller is thus free to modify the returned array.
     * <p/>
     * <p>This method acts as bridge between array-based and collection-based
     * APIs.
     *
     * @return an array containing all of the elements in this collection
     */
    @Override
    public final Object[] toArray()
    {
        return toTypedArray( getWrappedEntries() );
    }

    /**
     * Returns an array containing all of the elements in this collection;
     * the runtime type of the returned array is that of the specified array.
     * If the collection fits in the specified array, it is returned therein.
     * Otherwise, a new array is allocated with the runtime type of the
     * specified array and the size of this collection.
     * <p/>
     * <p>If this collection fits in the specified array with room to spare
     * (i.e., the array has more elements than this collection), the element
     * in the array immediately following the end of the collection is set to
     * <tt>null</tt>.  (This is useful in determining the length of this
     * collection <i>only</i> if the caller knows that this collection does
     * not contain any <tt>null</tt> elements.)
     * <p/>
     * <p>If this collection makes any guarantees as to what order its elements
     * are returned by its iterator, this method must return the elements in
     * the same order.
     * <p/>
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
     * array-based and collection-based APIs.  Further, this method allows
     * precise control over the runtime type of the output array, and may,
     * under certain circumstances, be used to save allocation costs.
     * <p/>
     * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
     * The following code can be used to dump the collection into a newly
     * allocated array of <tt>String</tt>:
     * <p/>
     * <pre>
     *     String[] y = x.toArray(new String[0]);</pre>
     *
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
     * <tt>toArray()</tt>.
     *
     * @param a the array into which the elements of this collection are to be
     *          stored, if it is big enough; otherwise, a new array of the same
     *          runtime type is allocated for this purpose.
     *
     * @return an array containing all of the elements in this collection
     *
     * @throws ArrayStoreException  if the runtime type of the specified array
     *                              is not a supertype of the runtime type of every element in
     *                              this collection
     * @throws NullPointerException if the specified array is null
     */
    @Override
    public final <T> T[] toArray( T[] a )
    {
        Wrapper<E, C>[] zWrappers = getWrappedEntries();
        List<Object> collector = new ArrayList<Object>( zWrappers.length );
        for ( Wrapper<E, C> zWrapper : zWrappers )
        {
            collector.add( zWrapper.getObject() );
        }
        //noinspection SuspiciousToArrayCall
        return collector.toArray( a );
    }

    /**
     * Ensures that this collection contains the specified element (optional
     * operation).  Returns <tt>true</tt> if this collection changed as a
     * result of the call.  (Returns <tt>false</tt> if this collection does
     * not permit duplicates and already contains the specified element.)<p>
     * <p/>
     * Collections that support this operation may place limitations on what
     * elements may be added to this collection.  In particular, some
     * collections will refuse to add <tt>null</tt> elements, and others will
     * impose restrictions on the type of elements that may be added.
     * Collection classes should clearly specify in their documentation any
     * restrictions on what elements may be added.<p>
     * <p/>
     * If a collection refuses to add a particular element for any reason
     * other than that it already contains the element, it <i>must</i> throw
     * an exception (rather than returning <tt>false</tt>).  This preserves
     * the invariant that a collection always contains the specified element
     * after this call returns.
     *
     * @param e element whose presence in this collection is to be ensured
     *
     * @return <tt>true</tt> if this collection changed as a result of the
     *         call
     *
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *                                       is not supported by this collection
     * @throws ClassCastException            if the class of the specified element
     *                                       prevents it from being added to this collection
     * @throws NullPointerException          if the specified element is null and this
     *                                       collection does not permit null elements
     * @throws IllegalArgumentException      if some property of the element
     *                                       prevents it from being added to this collection
     * @throws IllegalStateException         if the element cannot be added at this
     *                                       time due to insertion restrictions
     */
    @Override
    public final synchronized boolean add( E e )
    {
        return LLmodified( LLadd( wrapNotNull( e ) ) );
    }

    /**
     * Removes a single instance of the specified element from this
     * collection, if it is present (optional operation).  More formally,
     * removes an element <tt>e</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
     * this collection contains one or more such elements.  Returns
     * <tt>true</tt> if this collection contained the specified element (or
     * equivalently, if this collection changed as a result of the call).
     *
     * @param o element to be removed from this collection, if present
     *
     * @return <tt>true</tt> if an element was removed as a result of this call
     *
     * @throws ClassCastException            if the type of the specified element
     *                                       is incompatible with this collection (optional)
     * @throws NullPointerException          if the specified element is null and this
     *                                       collection does not permit null elements (optional)
     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
     *                                       is not supported by this collection
     */
    @Override
    public final synchronized boolean remove( Object o )
    {
        return LLmodified( LLremove( wrap( castNotNull( o ) ) ) );
    }

    /**
     * Returns <tt>true</tt> if this collection contains all of the elements
     * in the specified collection.
     *
     * @param c collection to be checked for containment in this collection
     *
     * @return <tt>true</tt> if this collection contains all of the elements
     *         in the specified collection
     *
     * @throws ClassCastException   if the types of one or more elements
     *                              in the specified collection are incompatible with this
     *                              collection (optional)
     * @throws NullPointerException if the specified collection contains one
     *                              or more null elements and this collection does not permit null
     *                              elements (optional), or if the specified collection is null
     * @see #contains(Object)
     */
    @Override
    public final boolean containsAll( Collection<?> c )
    {
        Object[] zObjects = c.toArray();
        synchronized ( this )
        {
            Wrapper<E, C>[] zWrappers = getWrappedEntries();
            for ( Object zEntry : zObjects )
            {
                if ( !LLcontains( zWrappers, zEntry ) )
                {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Adds all of the elements in the specified collection to this collection
     * (optional operation).  The behavior of this operation is undefined if
     * the specified collection is modified while the operation is in progress.
     * (This implies that the behavior of this call is undefined if the
     * specified collection is this collection, and this collection is
     * nonempty.)
     *
     * @param c collection containing elements to be added to this collection
     *
     * @return <tt>true</tt> if this collection changed as a result of the call
     *
     * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
     *                                       is not supported by this collection
     * @throws ClassCastException            if the class of an element of the specified
     *                                       collection prevents it from being added to this collection
     * @throws NullPointerException          if the specified collection contains a
     *                                       null element and this collection does not permit null elements,
     *                                       or if the specified collection is null
     * @throws IllegalArgumentException      if some property of an element of the
     *                                       specified collection prevents it from being added to this
     *                                       collection
     * @throws IllegalStateException         if not all the elements can be added at
     *                                       this time due to insertion restrictions
     * @see #add(Object)
     */
    @Override
    public final boolean addAll( Collection<? extends E> c )
    {
        E[] zEntries = c.toArray( mHelper.createArray( 0 ) );
        synchronized ( this )
        {
            boolean modified = false;
            for ( E zEntry : zEntries )
            {
                modified |= LLadd( wrapNotNull( zEntry ) );
            }
            return LLmodified( modified );
        }
    }

    /**
     * Removes all of this collection's elements that are also contained in the
     * specified collection (optional operation).  After this call returns,
     * this collection will contain no elements in common with the specified
     * collection.
     *
     * @param c collection containing elements to be removed from this collection
     *
     * @return <tt>true</tt> if this collection changed as a result of the
     *         call
     *
     * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
     *                                       is not supported by this collection
     * @throws ClassCastException            if the types of one or more elements
     *                                       in this collection are incompatible with the specified
     *                                       collection (optional)
     * @throws NullPointerException          if this collection contains one or more
     *                                       null elements and the specified collection does not support
     *                                       null elements (optional), or if the specified collection is null
     * @see #remove(Object)
     * @see #contains(Object)
     */
    @Override
    public final boolean removeAll( Collection<?> c )
    {
        // Default Implementation uses modifiable Iterator!
        //
        //        boolean modified = false;
        //        Iterator<?> e = iterator();
        //        while ( e.hasNext() )
        //        {
        //            if ( c.contains( e.next() ) )
        //            {
        //                e.remove();
        //                modified = true;
        //            }
        //        }
        //        return modified;
        Object[] zObjects = c.toArray();
        synchronized ( this )
        {
            boolean modified = false;
            for ( Object zToRemove : zObjects )
            {
                if ( mHelper.isInstance( zToRemove ) )
                {
                    modified |= LLremove( wrap( castNullOK( zToRemove ) ) );
                }
            }
            return LLmodified( modified );
        }
    }

    /**
     * Retains only the elements in this collection that are contained in the
     * specified collection (optional operation).  In other words, removes from
     * this collection all of its elements that are not contained in the
     * specified collection.
     *
     * @param c collection containing elements to be retained in this collection
     *
     * @return <tt>true</tt> if this collection changed as a result of the call
     *
     * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
     *                                       is not supported by this collection
     * @throws ClassCastException            if the types of one or more elements
     *                                       in this collection are incompatible with the specified
     *                                       collection (optional)
     * @throws NullPointerException          if this collection contains one or more
     *                                       null elements and the specified collection does not permit null
     *                                       elements (optional), or if the specified collection is null
     * @see #remove(Object)
     * @see #contains(Object)
     */
    @Override
    public final synchronized boolean retainAll( Collection<?> c )
    {
        // Default Implementation uses modifiable Iterator!
        //
        //        boolean modified = false;
        //        Iterator<E> e = iterator();
        //        while ( e.hasNext() )
        //        {
        //            if ( !c.contains( e.next() ) )
        //            {
        //                e.remove();
        //                modified = true;
        //            }
        //        }
        //        return modified;
        boolean modified = false;
        for ( Wrapper<E, C> zWrapper : getWrappedEntries() )
        {
            if ( !c.contains( zWrapper.getObject() ) )
            {
                modified |= LLremove( zWrapper );
            }
        }
        return LLmodified( modified );
    }

    /**
     * Returns the hash code value for this collection.
     * <p/>
     * The hash code is defined to be the sum of the hash codes of the
     * elements in the collection.  While this calculation is sub-optimal for
     * lists, it is required for sets, and is adequate for lists.
     * <p/>
     * This ensures that collections that are equal have the same hashcode,
     * as required by the general contract of {@link Object#hashCode}.
     *
     * @return the hash code value for this list
     */
    public final int hashCode()
    {
        int hashcode = 1;
        for ( Wrapper<E, C> zWrapper : getWrappedEntries() )
        {
            C zProxyValue = zWrapper.getProxyValue();
            if ( zProxyValue != null )
            {
                hashcode += zProxyValue.hashCode();
            }
        }
        return hashcode;
    }

    /**
     * Returns a string representation of this collection.  The string
     * representation consists of a list of the collection's elements in the
     * order they are returned by its iterator, enclosed in square brackets
     * (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
     * <tt>", "</tt> (comma and space).  Elements are converted to strings as
     * by {@link String#valueOf(Object)}.
     *
     * @return a string representation of this collection
     */
    public String toString()
    {
        if ( isEmpty() )
        {
            return "[]";
        }

        StringBuilder sb = new StringBuilder();
        String prefix = "[";
        for ( Object zEntry : toArray() )
        {
            sb.append( prefix ).append( zEntry == this ? "(this Collection)" : zEntry );
            prefix = ", ";
        }
        return sb.append( ']' ).toString();
    }

    /**
     * Returns an iterator over the elements in this collection.  There are no
     * guarantees concerning the order in which the elements are returned
     * (unless this collection is an instance of some class that provides a
     * guarantee).
     *
     * @return an <tt>Iterator</tt> over the elements in this collection
     */
    @Override
    public final Iterator<E> iterator()
    {
        ModVersionedEntries<E, C> zModVersionedEntries = getModVersionedEntries();
        return new OurIterator( 0, zModVersionedEntries.getWrappers().length, 0, zModVersionedEntries );
    }

    protected final Object getSynchronizedObject()
    {
        return this;
    }

    protected final synchronized ModVersionedEntries<E, C> getModVersionedEntries()
    {
        return new ModVersionedEntries<E, C>( LLgetModificationVersion(), getWrappedEntries() );
    }

    protected static class ModVersionedEntries<E, C extends Comparable<C>>
    {
        private int mModificationVersion;
        private Wrapper<E, C>[] mWrappers;

        public ModVersionedEntries( int pModificationVersion, Wrapper<E, C>[] pWrappers )
        {
            mModificationVersion = pModificationVersion;
            mWrappers = pWrappers;
        }

        public int getModificationVersion()
        {
            return mModificationVersion;
        }

        public Wrapper<E, C>[] getWrappers()
        {
            return mWrappers;
        }
    }

    protected final void LLcheckForComodification( int mExpectedModificationVersion )
    {
        if ( mExpectedModificationVersion != LLgetModificationVersion() )
        {
            throw new ConcurrentModificationException();
        }
    }

    protected class OurIterator implements Iterator<E>
    {
        protected void checkForComodification()
        {
            LLcheckForComodification( mModificationVersion );
        }

        private final int mModificationVersion;
        protected final E[] mEntries;
        protected final int mToIndex;
        protected int mNextIndex;

        protected OurIterator( int pFromIndex, int pSize, int pNextIndex, ModVersionedEntries<E, C> pModVersionedEntries )
        {
            mNextIndex = pFromIndex + pNextIndex;
            mModificationVersion = pModVersionedEntries.getModificationVersion();
            mEntries = toTypedArray( pModVersionedEntries.getWrappers() );
            mToIndex = pFromIndex + pSize;
        }

        /**
         * Returns <tt>true</tt> if the iteration has more elements. (In other
         * words, returns <tt>true</tt> if <tt>next</tt> would return an element
         * rather than throwing an exception.)
         *
         * @return <tt>true</tt> if the iterator has more elements.
         */
        @Override
        public boolean hasNext()
        {
            return (mNextIndex < mToIndex);
        }

        /**
         * Returns the next element in the iteration.
         *
         * @return the next element in the iteration.
         *
         * @throws NoSuchElementException iteration has no more elements.
         */
        @Override
        public E next()
        {
            checkForComodification();
            if ( !hasNext() )
            {
                throw new NoSuchElementException();
            }
            return mEntries[mNextIndex++];
        }

        /**
         * Removes from the underlying collection the last element returned by the
         * iterator (optional operation).  This method can be called only once per
         * call to <tt>next</tt>.  The behavior of an iterator is unspecified if
         * the underlying collection is modified while the iteration is in
         * progress in any way other than by calling this method.
         *
         * @throws UnsupportedOperationException if the <tt>remove</tt>
         *                                       operation is not supported by this Iterator.
         * @throws IllegalStateException         if the <tt>next</tt> method has not
         *                                       yet been called, or the <tt>remove</tt> method has already
         *                                       been called after the last call to the <tt>next</tt>
         *                                       method.
         */
        @Override
        public void remove()
        {
            throw new UnsupportedOperationException();
        }
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/NoEqualsCollection.java

Diff revisions: vs.
Revision Author Commited Message
610 Diff Diff GeorgeS picture GeorgeS Mon 12 Mar, 2012 00:54:00 +0000
587 GeorgeS picture GeorgeS Fri 02 Dec, 2011 11:55:12 +0000

Accordion...