22
33import java .util .Arrays ;
44import java .util .Collection ;
5+ import java .util .Iterator ;
56import java .util .Objects ;
67import java .util .Spliterator ;
78import java .util .Spliterators ;
@@ -69,12 +70,24 @@ public boolean addAll(Collection<? extends E> collection) {
6970 return true ;
7071 }
7172
73+ @ Override
74+ public UnsafeMutableArray <E > unsafeAdd (E element ) {
75+ wrapped ()[getAndIncrementSize ()] = element ;
76+ return this ;
77+ }
78+
7279 @ Override
7380 public void replace (int index , E element ) {
7481 checkIndex (index );
7582 unsafeSet (index , element );
7683 }
7784
85+ @ Override
86+ public UnsafeMutableArray <E > unsafeSet (int index , E element ) {
87+ wrapped ()[index ] = element ;
88+ return this ;
89+ }
90+
7891 @ Override
7992 public E remove (int index ) {
8093 checkIndex (index );
@@ -91,6 +104,24 @@ public boolean remove(Object element) {
91104 return true ;
92105 }
93106
107+ @ Override
108+ public E unsafeRemove (int index ) {
109+
110+ int numMoved = size () - index - 1 ;
111+
112+ @ Nullable E [] wrapped = wrapped ();
113+ E element = wrapped [index ];
114+
115+ if (numMoved > 0 ) {
116+ System .arraycopy (wrapped , index + 1 , wrapped , index , numMoved );
117+ }
118+
119+ wrapped [decrementAnGetSize ()] = null ;
120+
121+ //noinspection DataFlowIssue
122+ return element ;
123+ }
124+
94125 @ Override
95126 public boolean removeAll (Collection <?> collection ) {
96127 if (collection .isEmpty ()) {
@@ -130,11 +161,16 @@ public boolean retainAll(Collection<?> collection) {
130161
131162 @ Override
132163 public void clear () {
133- if (isEmpty ()) {
134- return ;
164+ int size = size ();
165+ if (size > 0 ) {
166+ Arrays .fill (wrapped (), 0 , size , null );
167+ size (0 );
135168 }
136- Arrays .fill (wrapped (), 0 , size (), null );
137- size (0 );
169+ }
170+
171+ @ Override
172+ public Iterator <E > iterator () {
173+ return new DefaultMutableArrayIterator <>(this );
138174 }
139175
140176 @ Override
@@ -159,10 +195,42 @@ protected void processAdd(E[] array, int currentSize, int elementsToAdd) {
159195
160196 protected abstract void size (int size );
161197
198+ protected abstract int getAndIncrementSize ();
199+ protected abstract int decrementAnGetSize ();
200+
162201 protected abstract void wrapped (@ Nullable E [] wrapped );
163202
203+ @ Override
204+ public UnsafeMutableArray <E > prepareForSize (int expectedSize ) {
205+ @ Nullable E [] wrapped = wrapped ();
206+ if (expectedSize > wrapped .length ) {
207+ int newLength = Math .max ((wrapped .length * 3 ) / 2 , expectedSize );
208+ wrapped (Arrays .copyOf (wrapped , newLength ));
209+ }
210+ return this ;
211+ }
212+
213+
214+ @ Override
215+ public UnsafeMutableArray <E > trimToSize () {
216+ @ Nullable E [] wrapped = wrapped ();
217+ int size = size ();
218+
219+ if (size == wrapped .length ) {
220+ return this ;
221+ }
222+ wrapped (Arrays .copyOfRange (wrapped , 0 , size ));
223+ return this ;
224+ }
225+
164226 @ Override
165227 public UnsafeMutableArray <E > asUnsafe () {
166228 return this ;
167229 }
230+
231+ protected static void validateCapacity (int capacity ) {
232+ if (capacity < 0 ) {
233+ throw new IllegalArgumentException ("Capacity cannot be negative" );
234+ }
235+ }
168236}
0 commit comments