1- //ChuList v1.9, ©2020 Pecacheu. Licensed under GNU GPL 3.0
2-
1+ //ChuList v1.9.1, ©2024 Pecacheu. Licensed under GNU GPL 3.0
32//An actually properly implemented List class? With error checking!? Imagine that!
43
54package net .forestfire .elevators ;
65
7- import java .util .ArrayList ;
8- import java .util .Collection ;
9- import java .util .Comparator ;
10- import java .util .Iterator ;
11- import java .util .List ;
12- import java .util .ListIterator ;
13- import java .util .NoSuchElementException ;
14- import java .util .Spliterator ;
15- import java .util .function .Predicate ;
16- import java .util .function .UnaryOperator ;
17-
18- @ SuppressWarnings ("serial" )
6+ import java .util .*;
7+
198public class ChuList <E > extends ArrayList <E > {
209public int length =0 ; //<- JavaScript Compatibility (Maybe not the best idea, though)
21- public ChuList () {
22- super ();
23- }
24- public ChuList (int initialCapacity ) {
25- super (initialCapacity );
26- }
27- public ChuList (Collection <? extends E > c ) {
28- super (c ); length =super .size ();
29- }
10+ public ChuList () { super (); }
11+ public ChuList (int initialCapacity ) { super (initialCapacity ); }
12+ public ChuList (Collection <? extends E > c ) { super (c ); length =super .size (); }
3013
31- //Construct with initial elements:
14+ //Construct with initial elements
3215@ SafeVarargs
3316public ChuList (E ... elements ) {
34- super ();
35- for (int i =0 ,l =elements .length ; i <l ; i ++) super .add (elements [i ]);
17+ super (); super .addAll (Arrays .asList (elements ));
3618 length = super .size ();
3719}
3820
@@ -41,15 +23,14 @@ public boolean add(E item) {
4123 super .add (item ); length = super .size (); return true ;
4224}
4325
44- //JavaScript Compatibility:
45- public void push (E item ) {
46- add (item );
47- }
26+ //JavaScript Compatibility
27+ public void push (E item ) { add (item ); }
4828
49- //Now, this is just common-sense functionality:
29+ //Now, this is just common-sense functionality
5030public String join (String sep ) {
51- String str = "" ; for (int i =0 ,l =super .size (); i <l ; i ++)
52- str += (i ==0 ?"" :sep )+super .get (i ); return str ;
31+ StringBuilder sb = new StringBuilder ();
32+ for (int i =0 , l =super .size (); i <l ; ++i ) sb .append (i ==0 ?"" :sep ).append (super .get (i ));
33+ return sb .toString ();
5334}
5435
5536@ Override
@@ -93,26 +74,21 @@ public void clear() {
9374 super .clear (); length = 0 ;
9475}
9576
96- //Actually Working toString For Debugging:
77+ //Actually working toString for debugging
9778@ Override
98- public String toString () {
99- String str = "[" ; for (int i =0 ,l =super .size (); i <l ; i ++)
100- str += (i ==0 ?"" :", " )+super .get (i ); str += "]" ; return str ;
101- }
79+ public String toString () { return "[" +this .join (", " )+"]" ; }
10280
10381public ChuList <E > addAll (int index , E [] arr ) {
104- for (int i =0 ,l =arr .length ; i <l ; i ++ ) add (index +i , arr [i ]);
82+ for (int i =0 ,l =arr .length ; i <l ; ++ i ) add (index +i , arr [i ]);
10583 return this ;
10684}
10785
10886public ChuList <E > addAll (E [] arr ) {
109- for (int i =0 ,l =arr .length ; i <l ; i ++) super .add (arr [i ]);
110- return this ;
87+ for (E e : arr ) super .add (e ); return this ;
11188}
11289
11390public ChuList <E > addAll (ArrayList <E > arr ) {
114- for (int i =0 ,l =arr .size (); i <l ; i ++) super .add (arr .get (i ));
115- return this ;
91+ for (E e : arr ) super .add (e ); return this ;
11692}
11793
11894@ Override
@@ -122,76 +98,9 @@ public ChuList<E> subList(int fromIndex, int toIndex) {
12298 else list = super .subList (fromIndex , toIndex ); return new ChuList <E >(list );
12399}
124100
125- /*@SuppressWarnings("unchecked")
126- @Override
127- public E[] toArray() {
128- int size = super.size(); Object[] arr = new Object[size];
129- for(int i=0; i<size; i++) arr[i] = super.get(i); return ((E[])arr);
130- }*/
131-
132- //Unchanged Methods:
133-
134- //size()
135- //ensureCapacity()
136- //trimToSize()
137- //indexOf()
138- //lastIndexOf()
139- //forEach()
140- //isEmpty()
141- //equals()
142- //toArray()
143- //hashCode()
144-
145- //--------- Unimplemented Methods:
146-
147- //TODO Add Error-Checking To These Methods:
148-
149- @ Override
150- public boolean addAll (Collection <? extends E > c ) {
151- //return super.addAll(c);
152- return false ;
153- }
154- @ Override
155- public boolean addAll (int index , Collection <? extends E > c ) {
156- //return super.addAll(index, c);
157- return false ;
158- }
159- @ Override
160- public boolean removeAll (Collection <?> c ) {
161- //return super.removeAll(c);
162- return false ;
163- }
164- @ Override
165- public boolean removeIf (Predicate <? super E > filter ) {
166- //return super.removeIf(filter);
167- return false ;
168- }
169- @ Override
170- protected void removeRange (int fromIndex , int toIndex ) {
171- //super.removeRange(fromIndex, toIndex);
172- }
173- @ Override
174- public void replaceAll (UnaryOperator <E > operator ) {
175- //super.replaceAll(operator);
176- }
177- @ Override
178- public boolean retainAll (Collection <?> c ) {
179- //return super.retainAll(c);
180- return false ;
181- }
182- @ Override
183- public boolean containsAll (Collection <?> c ) {
184- //return super.containsAll(c);
185- return false ;
186- }
187- @ Override
188- public void sort (Comparator <? super E > c ) {
189- //super.sort(c);
190- }
191-
192101//Iterators. These are only provided to maximize compatibility. Please use a standard for statement instead!
193102
194- //Better Iterator Class:
103+ //Better Iterator Class
195104public ChuIterator <E > chuIterator () { return new ChuIterator <E >(this ); }
196105
197106@ Override
@@ -204,38 +113,27 @@ public void sort(Comparator<? super E> c) {
204113public Spliterator <E > spliterator () { return super .spliterator (); }
205114}
206115
207- //Here's a better Iterator class to match:
116+ //Here's a better Iterator class to match
208117class ChuIterator <E > implements Iterator <E > {
209118public final ChuList <E > list ;
210119public int index = -1 ;
211120
212121ChuIterator (ChuList <E > l ) { list = l ; }
213122
214123@ Override
215- public boolean hasNext () {
216- return index < list .size ()-1 ;
217- }
124+ public boolean hasNext () { return index < list .size ()-1 ; }
218125
219126@ Override
220127public E next () {
221128 index ++; E item = list .get (index ); if (item ==null )
222129 throw new NoSuchElementException (); return item ;
223130}
224131
225- public void goBack () {
226- if (index > -1 ) index --;
227- }
228-
229- public E last () {
230- return list .get (index -1 );
231- }
232-
233- public E lookAhead () {
234- return lookAhead (1 );
235- }
132+ public void goBack () { if (index > -1 ) index --; }
133+ public E last () { return list .get (index -1 ); }
134+ public E lookAhead () { return lookAhead (1 ); }
236135public E lookAhead (int by ) {
237- if (by < 1 ) return null ;
238- return list .get (index +by );
136+ if (by < 1 ) return null ; return list .get (index +by );
239137}
240138}
241139
0 commit comments