Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 85610710eaa65e9f7c7dc99ae3d192880dcb2546 (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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
/**
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 */
package org.eclipse.emf.common.util;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;


/**
 * A highly extensible abstract list implementation {@link #data() logically backed by an array} that is <b>never<b> modified.
 *
 */
public abstract class ArrayDelegatingEList<E> extends AbstractEList<E> implements RandomAccess, Cloneable, Serializable
{
  private static final long serialVersionUID = 1L;

  /**
   * Creates an empty instance with no initial capacity.
   * The data storage will be null.
   */
  public ArrayDelegatingEList()
  {
    super();
  }

  /**
   * Creates an instance that is a copy of the collection.
   * @param collection the initial contents of the list.
   */
  public ArrayDelegatingEList(Collection<? extends E> collection)
  {
    // Conditionally create the data.
    //
    int size = collection.size();
    if (size > 0)
    {
      Object [] data = newData(size);
      collection.toArray(data);
      setData(data);
    }
  }

  /**
   * Creates an initialized instance that directly uses the given arguments.
   * @param data the underlying storage of the list.
   */
  protected ArrayDelegatingEList(Object [] data)
  {
    setData(data);
  }

  /**
   * Returns new allocated data storage.
   * Clients may override this to create typed storage.
   * The cost of type checking via a typed array is negligible.
   * @return new data storage.
   */
  protected Object [] newData(int capacity)
  {
    return new Object [capacity];
  }
  
  /**
   * Assigns the object into the data storage at the given index and returns the object that's been stored.
   * Clients can monitor access to the storage via this method.
   * @param index the position of the new content.
   * @param object the new content.
   * @return the object that's been stored.
   *
   */
  protected E assign(Object [] data, int index, E object)
  {
    data[index] = object;
    return object;
  }

  /**
   * Returns the number of objects in the list.
   * @return the number of objects in the list.
   */
  @Override
  public final int size()
  {
    Object[] data = data();
    return data == null ? 0 : data.length;
  }

  /**
   * Returns whether the list has zero size.
   * @return whether the list has zero size.
   */
  @Override
  public boolean isEmpty()
  {
    return data() == null;
  }

  /**
   * Returns whether the list contains the object.
   * This implementation uses either <code>equals</code> or <code>"=="</code> depending on {@link #useEquals useEquals}.
   * @param object the object in question.
   * @return whether the list contains the object.
   * @see #useEquals
   */
  @Override
  public boolean contains(Object object)
  {
    Object[] data = data();
    if (data != null)
    {
      if (useEquals() && object != null)
      {
        for (Object datum : data)
        {
          if (object.equals(datum))
          {
            return true;
          }
        }
      }
      else
      {
        for (Object datum : data)
        {
          if (datum == object)
          {
            return true;
          }
        }
      }
    }

    return false;
  }

  /**
   * Returns the position of the first occurrence of the object in the list.
   * This implementation uses either <code>equals</code> or <code>"=="</code> depending on {@link #useEquals useEquals}.
   * @param object the object in question.
   * @return the position of the first occurrence of the object in the list.
   */
  @Override
  public int indexOf(Object object)
  {
    Object[] data = data();
    if (data != null)
    {
      if (useEquals() && object != null)
      {
        for (int i = 0, size = data.length; i < size; ++i)
        {
          if (object.equals(data[i]))
          {
            return i;
          }
        }
      }
      else
      {
        for (int i = 0, size = data.length; i < size; ++i)
        {
          if (data[i] == object)
          {
            return i;
          }
        }
      }
    }
    return -1;
  }

  /**
   * Returns the position of the last occurrence of the object in the list.
   * This implementation uses either <code>equals</code> or <code>"=="</code> depending on {@link #useEquals useEquals}.
   * @param object the object in question.
   * @return the position of the last occurrence of the object in the list.
   */
  @Override
  public int lastIndexOf(Object object)
  {
    Object[] data = data();
    if (data != null)
    {
      if (useEquals() && object != null)
      {
        for (int i = data.length - 1; i >= 0; --i)
        {
          if (object.equals(data[i]))
          {
            return i;
          }
        }
      }
      else
      {
        for (int i = data.length - 1; i >= 0; --i)
        {
          if (data[i] == object)
          {
            return i;
          }
        }
      }
    }
    return -1;
  }

  /**
   * Returns an array containing all the objects in sequence.
   * Clients may override {@link #newData newData} to create typed storage in this case.
   * @return an array containing all the objects in sequence.
   * @see #newData
   */
  @Override
  public Object[] toArray()
  {
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    Object[] result = newData(size);

    // Guard for no data.
    //
    if (size > 0)
    {
      System.arraycopy(data, 0, result, 0, size);
    }
    return result;
  }

  /**
   * Returns an array containing all the objects in sequence.
   * @param array the array that will be filled and returned, if it's big enough;
   * otherwise, a suitably large array of the same type will be allocated and used instead.
   * @return an array containing all the objects in sequence.
   * @see #newData
   */
  @Override
  public <T> T[] toArray(T[] array)
  {
    // Guard for no data.
    //
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    if (size > 0)
    {
      if (array.length < size)
      {
        @SuppressWarnings("unchecked") T [] newArray = (T[])Array.newInstance(array.getClass().getComponentType(), size);
        array  = newArray;
      }

      System.arraycopy(data, 0, array, 0, size);
    }

    if (array.length > size)
    {
      array[size] = null;
    }

    return array;
  }

  /**
   * Returns direct <b>unsafe</b> access to the underlying data storage.
   * Clients may <b>not</b> modify this array's elements
   * but <b>may</b> assume that the array remains valid even as the list is modified.
   * The array's length is exactly the same as the list's {@link #size() size};
   * <code>null</code> is used to represent the empty list.
   * @return direct <b>unsafe</b> access to the underlying data storage.
   */
  public abstract Object [] data();

  /**
   * Updates directly and <b>unsafely</b> the underlying data storage.
   * Clients <b>must</b> be aware that this subverts all callbacks
   * and hence possibly the integrity of the list.
   * The list implementation itself calls this method whenever a modification of the list requires a new backing array.
   * @param data the new underlying data storage.
   */
  public void setData(Object [] data)
  {
    ++modCount;
  }

  /**
   * Returns the object at the index.
   * This implementation delegates to {@link #resolve resolve}
   * so that clients may transform the fetched object.
   * @param index the position in question.
   * @return the object at the index.
   * @exception IndexOutOfBoundsException if the index isn't within the size range.
   * @see #resolve
   * @see #basicGet
   */
  @SuppressWarnings({"unchecked", "null"})
  @Override
  public E get(int index)
  {
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    if (index >= size)
      throw new BasicIndexOutOfBoundsException(index, size);

    return resolve(index, (E)data[index]);
  }

  /**
   * Returns the object at the index without {@link #resolve resolving} it.
   * @param index the position in question.
   * @return the object at the index.
   * @exception IndexOutOfBoundsException if the index isn't within the size range.
   * @see #resolve
   * @see #get
   */
  @SuppressWarnings({"unchecked", "null"})
  @Override
  public E basicGet(int index)
  {
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    if (index >= size)
      throw new BasicIndexOutOfBoundsException(index, size);

    return (E)data[index];
  }
  
  @Override
  @SuppressWarnings({"unchecked"})
  protected E primitiveGet(int index)
  {
    return (E)data()[index];
  }

  /**
   * Sets the object at the index
   * and returns the old object at the index;
   * it does no ranging checking or uniqueness checking.
   * This implementation delegates to {@link #assign assign}, {@link #didSet didSet}, and {@link #didChange didChange}.
   * @param index the position in question.
   * @param object the object to set.
   * @return the old object at the index.
   * @see #set
   */
  @Override
  public E setUnique(int index, E object)
  {
    Object[] data = copy();
    @SuppressWarnings("unchecked") E oldObject = (E)data[index];
    assign(data, index, validate(index, object));
    setData(data);
    didSet(index, object, oldObject);
    didChange();
    return oldObject;
  }

  /**
   * Adds the object at the end of the list;
   * it does no uniqueness checking.
   * This implementation delegates to {@link #assign assign}, {@link #didAdd didAdd}, and {@link #didChange didChange}.
   * after uniqueness checking.
   * @param object the object to be added.
   * @see #add(Object)
   */
  @Override
  public void addUnique(E object)
  {
    int size = size();
    Object [] data = grow(size + 1);
    assign(data, size, validate(size, object));
    setData(data);
    didAdd(size, object);
    didChange();
  }

  /**
   * Adds the object at the given index in the list;
   * it does no ranging checking or uniqueness checking.
   * This implementation delegates to {@link #assign assign}, {@link #didAdd didAdd}, and {@link #didChange didChange}.
   * @param object the object to be added.
   * @see #add(int, Object)
   */
  @Override
  public void addUnique(int index, E object)
  {
    Object[] oldData = data();
    int size = oldData == null ? 0 : oldData.length;
    Object[] data = grow(size + 1);
    E validatedObject = validate(index, object);
    if (index != size)
    {
      System.arraycopy(oldData, index, data, index + 1, size - index);
    }
    assign(data, index, validatedObject);
    setData(data);
    didAdd(index, object);
    didChange();
  }

  /**
   * Adds each object of the collection to the end of the list;
   * it does no uniqueness checking.
   * This implementation delegates to {@link #assign assign}, {@link #didAdd didAdd}, and {@link #didChange didChange}.
   * @param collection the collection of objects to be added.
   * @see #addAll(Collection)
   */
  @Override
  public boolean addAllUnique(Collection<? extends E> collection)
  {
    int growth = collection.size();
    if (growth != 0)
    {
      int oldSize = size();
      int size = oldSize + growth;
      Object[] data = grow(size);

      Iterator<? extends E> objects = collection.iterator();
      for (int i = oldSize; i < size; ++i)
      {
        E object = objects.next();
        assign(data, i, validate(i, object));
      }
      setData(data);
      for (int i = oldSize; i < size; ++i)
      {
        @SuppressWarnings("unchecked") E object = (E)data[i];
        didAdd(i, object);
        didChange();
      }
      return true;
    }
    else
    {
      ++modCount;
      return false;
    }
  }

  /**
   * Adds each object of the collection at each successive index in the list
   * and returns whether any objects were added;
   * it does no ranging checking or uniqueness checking.
   * This implementation delegates to {@link #assign assign}, {@link #didAdd didAdd}, and {@link #didChange didChange}.
   * @param index the index at which to add.
   * @param collection the collection of objects to be added.
   * @return whether any objects were added.
   * @see #addAll(int, Collection)
   */
  @Override
  public boolean addAllUnique(int index, Collection<? extends E> collection)
  {
    int growth = collection.size();
    if (growth != 0)
    {
      Object[] oldData = data();
      int oldSize = oldData == null ? 0 : oldData.length;
      int size = oldSize + growth;
      Object[] data = grow(size);

      int shifted = oldSize - index;
      if (shifted > 0)
      {
        System.arraycopy(oldData, index, data, index + growth, shifted);
      }

      Iterator<? extends E> objects = collection.iterator();
      for (int i = 0; i < growth; ++i)
      {
        E object = objects.next();
        int currentIndex = index + i;
        assign(data, currentIndex, validate(currentIndex, object));
      }
      setData(data);
      for (int i = 0; i < growth; ++i)
      {
        @SuppressWarnings("unchecked") E object = (E)data[index];
        didAdd(index, object);
        didChange();
        index++;
      }
      return true;
    }
    else
    {
      ++modCount;
      return false;
    }
  }

  /**
   * Adds each object from start to end of the array at the index of list
   * and returns whether any objects were added;
   * it does no ranging checking or uniqueness checking.
   * This implementation delegates to {@link #assign assign}, {@link #didAdd didAdd}, and {@link #didChange didChange}.
   * @param objects the objects to be added.
   * @param start the index of first object to be added.
   * @param end the index past the last object to be added.
   * @return whether any objects were added.
   * @see #addAllUnique(Object[], int, int)
   */
  @Override
  public boolean addAllUnique(Object [] objects, int start, int end)
  {
    int growth = end - start;
    if (growth > 0)
    {
      int oldSize = size();
      int size = oldSize + growth;
      Object[] data = grow(size);

      for (int i = start, index = size; i < end; ++i, ++index)
      {
        @SuppressWarnings("unchecked") E object = (E)objects[i];
        assign(data, index, validate(index, object));
      }
      setData(data);
      for (int i = start, index = size; i < end; ++i, ++index)
      {
        @SuppressWarnings("unchecked") E object = (E)objects[i];
        didAdd(index, object);
        didChange();
      }
      return true;
    }
    else
    {
      ++modCount;
      return false;
    }
  }

  /**
   * Adds each object from start to end of the array at each successive index in the list
   * and returns whether any objects were added;
   * it does no ranging checking or uniqueness checking.
   * This implementation delegates to {@link #assign assign}, {@link #didAdd didAdd}, and {@link #didChange didChange}.
   * @param index the index at which to add.
   * @param objects the objects to be added.
   * @param start the index of first object to be added.
   * @param end the index past the last object to be added.
   * @return whether any objects were added.
   * @see #addAllUnique(Object[], int, int)
   */
  @Override
  public boolean addAllUnique(int index, Object [] objects, int start, int end)
  {
    int growth = end - start;
    if (growth > 0)
    {
      Object[] oldData = data();
      int oldSize = oldData == null ? 0 : oldData.length;
      int size = oldSize + growth;
      Object[] data = grow(size);

      int shifted = oldSize - index;
      if (shifted > 0)
      {
        System.arraycopy(oldData, index, data, index + growth, shifted);
      }

      for (int i = start, currentIndex = index; i < end; ++i, ++currentIndex)
      {
        @SuppressWarnings("unchecked") E object = (E)objects[i];
        assign(data, currentIndex, validate(currentIndex, object));
      }
      setData(data);
      for (int i = start, currentIndex = index; i < end; ++i, ++currentIndex)
      {
        @SuppressWarnings("unchecked") E object = (E)objects[i];
        didAdd(currentIndex, object);
        didChange();
      }
      return true;
    }
    else
    {
      ++modCount;
      return false;
    }
  }

  /**
   * Removes each object of the collection from the list and returns whether any object was actually contained by the list.
   * @param collection the collection of objects to be removed.
   * @return whether any object was actually contained by the list.
   */
  @SuppressWarnings("null")
  @Override
  public boolean removeAll(Collection<?> collection)
  {
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    boolean modified = false;
    for (int i = size; --i >= 0; )
    {
      if (collection.contains(data[i]))
      {
        remove(i);
        modified = true;
      }
    }

    return modified;
  }

  /**
   * Removes the object at the index from the list and returns it.
   * This implementation delegates to {@link #didRemove didRemove} and {@link #didChange didChange}.
   * @param index the position of the object to remove.
   * @return the removed object.
   * @exception IndexOutOfBoundsException if the index isn't within the size range.
   */
  @SuppressWarnings("null")
  @Override
  public E remove(int index)
  {
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    if (index >= size)
      throw new BasicIndexOutOfBoundsException(index, size);

    @SuppressWarnings("unchecked") E oldObject = (E)data[index];

    Object[] newData;
    if (size == 1)
    {
      newData = null;
    }
    else
    {
      newData = newData(size - 1);
      System.arraycopy(data, 0, newData, 0, index);
      int shifted = size - index - 1;
      if (shifted > 0)
      {
        System.arraycopy(data, index+1, newData, index, shifted);
      }
    }

    setData(newData);
    didRemove(index, oldObject);
    didChange();

    return oldObject;
  }

  /**
   * Removes from the list each object not contained by the collection
   * and returns whether any object was actually removed.
   * This delegates to {@link #remove(int) remove(int)}
   * in the case that it finds an object that isn't retained.
   * @param collection the collection of objects to be retained.
   * @return whether any object was actually removed.
   */
  @SuppressWarnings("null")
  @Override
  public boolean retainAll(Collection<?> collection)
  {
    boolean modified = false;
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    for (int i = size; --i >= 0; )
    {
      if (!collection.contains(data[i]))
      {
        remove(i);
        modified = true;
      }
    }
    return modified;
  }

  /**
   * Clears the list of all objects.
   * This implementation discards the data storage without modifying it
   * and delegates to {@link #didClear didClear} and {@link #didChange didChange}.
   */
  @Override
  public void clear()
  {
    ++modCount;

    Object[] oldData = data();
    int oldSize = oldData == null ? 0 : oldData.length;

    // Give it all back to the garbage collector.
    //
    setData(null);

    didClear(oldSize, oldData);
    didChange();
  }

  /**
   * Moves the object at the source index of the list to the target index of the list
   * and returns the moved object.
   * This implementation delegates to {@link #assign assign}, {@link #didMove didMove}, and {@link #didChange didChange}.
   * @param targetIndex the new position for the object in the list.
   * @param sourceIndex the old position of the object in the list.
   * @return the moved object.
   * @exception IndexOutOfBoundsException if either index isn't within the size range.
   */
  @SuppressWarnings("null")
  @Override
  public E move(int targetIndex, int sourceIndex)
  {
    Object[] data = copy();
    int size = data == null ? 0 : data.length;
    if (targetIndex >= size)
      throw new IndexOutOfBoundsException("targetIndex=" + targetIndex + ", size=" + size);

    if (sourceIndex >= size)
      throw new IndexOutOfBoundsException("sourceIndex=" + sourceIndex + ", size=" + size);

    @SuppressWarnings("unchecked") E object = (E)data[sourceIndex];
    if (targetIndex != sourceIndex)
    {
      if (targetIndex < sourceIndex)
      {
        System.arraycopy(data, targetIndex, data, targetIndex + 1, sourceIndex - targetIndex);
      }
      else
      {
        System.arraycopy(data, sourceIndex + 1, data, sourceIndex, targetIndex - sourceIndex);
      }
      assign(data, targetIndex, object);
      setData(data);
      didMove(targetIndex, object, sourceIndex);
      didChange();
    }
    return object;
  }

  /**
   * Grows the capacity of the list to exactly the new size.
   */
  protected Object[] grow(int size)
  {
    Object[] oldData = data();
    Object[] data = newData(size);
    if (oldData != null)
    {
      System.arraycopy(oldData, 0, data, 0, oldData.length);
    }
    return data;
  }

  private static Object [] EMPTY_ARRAY = new Object [0];

  /**
   * Returns a copy of the existing data array.
   */
  protected Object[] copy()
  {
    Object[] data = data();
    if (data != null)
    {
      Object[] newData = newData(data.length);
      System.arraycopy(data, 0, newData, 0, data.length);
      return newData;
    }
    else
    {
      return EMPTY_ARRAY;
    }
  }

  private synchronized void writeObject(ObjectOutputStream objectOutputStream) throws IOException
  {
    objectOutputStream.defaultWriteObject();
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    if (data == null)
    {
      objectOutputStream.writeInt(0);
    }
    else
    {
      objectOutputStream.writeInt(size);
      for (int i = 0; i < size; ++i)
      {
        objectOutputStream.writeObject(data[i]);
      }
    }
  }

  private synchronized void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException
  {
    objectInputStream.defaultReadObject();
    int size = objectInputStream.readInt();
    Object[] data;
    if (size > 0)
    {
      try
      {
        data = newData(size);
      }
      catch (Throwable exception)
      {
        data = new Object[size];
      }

      setData(data);

      for (int i = 0; i < size; ++i)
      {
        @SuppressWarnings("unchecked") E object = (E)objectInputStream.readObject();
        didAdd(i, assign(data, i, object));
      }
    }
  }

  /**
   * Returns a shallow copy of this list.
   * @return a shallow copy of this list.
   */
  @Override
  public Object clone()
  {
    try
    {
      @SuppressWarnings("unchecked") ArrayDelegatingEList<E> clone = (ArrayDelegatingEList<E>)super.clone();
      Object[] data = data();
      int size = data == null ? 0 : data.length;
      if (size > 0)
      {
        Object[] newData = newData(size);
        clone.setData(newData);
        System.arraycopy(data, 0, newData, 0, size);
      }
      return clone;
    }
    catch (CloneNotSupportedException exception)
    {
      throw new InternalError();
    }
  }

  /**
   * Returns an iterator.
   * This implementation allocates a {@link ArrayDelegatingEList.EIterator}.
   * @return an iterator.
   * @see ArrayDelegatingEList.EIterator
   */
  @Override
  public Iterator<E> iterator()
  {
    return new EIterator<E>();
  }

  /**
   * An extensible iterator implementation.
   */
  protected class EIterator<E1> extends AbstractEList<E>.EIterator<E1>
  {
    /**
     * The expected data array of the containing list.
     */
    protected Object [] expectedData = data();

    /**
     * Checks that the modification count and data array are as expected.
     * @exception ConcurrentModificationException if the modification count is not as expected.
     */
    @Override
    protected void checkModCount()
    {
      if (modCount != expectedModCount || data() != expectedData)
      {
        throw new ConcurrentModificationException();
      }
    }

    @Override
    public void remove()
    {
      super.remove();
      expectedData = data();
    }
  }

  /**
   * Returns a read-only iterator that does not {@link #resolve resolve} objects.
   * This implementation allocates a {@link NonResolvingEIterator}.
   * @return a read-only iterator that does not resolve objects.
   */
  @Override
  protected Iterator<E> basicIterator()
  {
    return new NonResolvingEIterator<E>();
  }

  /**
   * An extended read-only iterator that does not {@link ArrayDelegatingEList#resolve resolve} objects.
   */
  protected class NonResolvingEIterator<E1> extends AbstractEList<E>.NonResolvingEIterator<E1>
  {
    /**
     * The expected data array of the containing list.
     */
    protected Object [] expectedData = data();

    /**
     * Checks that the modification count and data array are as expected.
     * @exception ConcurrentModificationException if the modification count is not as expected.
     */
    @Override
    protected void checkModCount()
    {
      if (modCount != expectedModCount || data() != expectedData)
      {
        throw new ConcurrentModificationException();
      }
    }
  }

  /**
   * Returns a list iterator.
   * This implementation allocates a {@link ArrayDelegatingEList.EListIterator}.
   * @return a list iterator.
   * @see ArrayDelegatingEList.EListIterator
   */
  @Override
  public ListIterator<E> listIterator()
  {
    return new EListIterator<E>();
  }

  /**
   * Returns a list iterator advanced to the given index.
   * This implementation allocates a {@link ArrayDelegatingEList.EListIterator}.
   * @param index the starting index.
   * @return a list iterator advanced to the index.
   * @see ArrayDelegatingEList.EListIterator
   * @exception IndexOutOfBoundsException if the index isn't within the size range.
   */
  @Override
  public ListIterator<E> listIterator(int index)
  {
    int size = size();
    if (index < 0 || index > size)
      throw new BasicIndexOutOfBoundsException(index, size);

    return new EListIterator<E>(index);
  }

  /**
   * An extensible list iterator implementation.
   */
  protected class EListIterator<E1> extends AbstractEList<E>.EListIterator<E1>
  {
    /**
     * The expected data array of the containing list.
     */
    protected Object [] expectedData = data();

    /**
     * Creates an instance.
     */
    public EListIterator()
    {
      super();
    }

    /**
     * Creates an instance advanced to the index.
     * @param index the starting index.
     */
    public EListIterator(int index)
    {
      super(index);
    }

    /**
     * Checks that the modification count and data array are as expected.
     * @exception ConcurrentModificationException if the modification count is not as expected.
     */
    @Override
    protected void checkModCount()
    {
      if (modCount != expectedModCount || data() != expectedData)
      {
        throw new ConcurrentModificationException();
      }
    }

    @Override
    public void remove()
    {
      super.remove();
      expectedData = data();
    }

    @Override
    protected void doSet(E object)
    {
      super.doSet(object);
      expectedData = data();
    }
  }

  /**
   * Returns a read-only list iterator that does not {@link #resolve resolve} objects.
   * This implementation allocates a {@link NonResolvingEListIterator}.
   * @return a read-only list iterator that does not resolve objects.
   */
  @Override
  protected ListIterator<E> basicListIterator()
  {
    return new NonResolvingEListIterator<E>();
  }

  /**
   * Returns a read-only list iterator advanced to the given index that does not {@link #resolve resolve} objects.
   * This implementation allocates a {@link NonResolvingEListIterator}.
   * @param index the starting index.
   * @return a read-only list iterator advanced to the index.
   * @exception IndexOutOfBoundsException if the index isn't within the size range.
   */
  @Override
  protected ListIterator<E> basicListIterator(int index)
  {
    int size = size();
    if (index < 0 || index > size)
      throw new BasicIndexOutOfBoundsException(index, size);

    return new NonResolvingEListIterator<E>(index);
  }

  /**
   * An extended read-only list iterator that does not {@link ArrayDelegatingEList#resolve resolve} objects.
   */
  protected class NonResolvingEListIterator<E1> extends AbstractEList<E>.NonResolvingEListIterator<E1>
  {
    /**
     * The expected data array of the containing list.
     */
    protected Object [] expectedData = data();

    /**
     * Creates an instance.
     */
    public NonResolvingEListIterator()
    {
      super();
    }

    /**
     * Creates an instance advanced to the index.
     * @param index the starting index.
     */
    public NonResolvingEListIterator(int index)
    {
      super(index);
    }

    /**
     * Checks that the modification count and data array are as expected.
     * @exception ConcurrentModificationException if the modification count is not as expected.
     */
    @Override
    protected void checkModCount()
    {
      if (modCount != expectedModCount || data() != expectedData)
      {
        throw new ConcurrentModificationException();
      }
    }
  }

  /**
   * Returns an <b>unsafe</b> list that provides a {@link #resolve non-resolving} view of the underlying data storage.
   * @return an <b>unsafe</b> list that provides a non-resolving view of the underlying data storage.
   */
  @Override
  protected List<E> basicList()
  {
    Object[] data = data();
    int size = data == null ? 0 : data.length;
    if (size == 0)
    {
      return ECollections.emptyEList();
    }
    else
    {
      return new BasicEList.UnmodifiableEList<E>(size, data);
    }
  }
}

Back to the top