1 | /* |
2 | * JTiger Unit Testing Framework for J2SE 1.5 |
3 | * Copyright (C) 2005 Tony Morris |
4 | * |
5 | * This software is licenced under the |
6 | * Common Public Licence version 1.0 |
7 | * http://www.opensource.org/licenses/cpl1.0.php |
8 | * |
9 | * You received a copy of this licence with this software. |
10 | */ |
11 | package org.jtiger.framework; |
12 | |
13 | import java.util.Iterator; |
14 | import java.util.NoSuchElementException; |
15 | import java.util.List; |
16 | import java.util.ListIterator; |
17 | |
18 | /** |
19 | * Returns instances of {@link Sequence} such that state cannot be modified. |
20 | * |
21 | * @see Sequence |
22 | * @author %javadoc.author.tag% |
23 | * @version %version%<br/> |
24 | * <i>Build Number %build.number%</i><br/> |
25 | * <i>Build Time %build.time%</i> |
26 | */ |
27 | public final class SequenceFactory |
28 | { |
29 | private SequenceFactory() throws UnsupportedOperationException |
30 | { |
31 | throw new UnsupportedOperationException(); |
32 | } |
33 | |
34 | /** |
35 | * Returns a new instance of {@link Sequence} that is backed by the given arguments. |
36 | * |
37 | * @param a The array that backs the returned {@link Sequence}. |
38 | * @return A new instance of {@link Sequence} that is backed by the given arguments. |
39 | * @throws NullPointerException If <code>a</code> is <code>null</code>. |
40 | */ |
41 | public static <E> Sequence<E> newSequence(final E... a) throws NullPointerException |
42 | { |
43 | if(a == null) |
44 | { |
45 | throw new NullPointerException(); |
46 | } |
47 | else |
48 | { |
49 | return new ArraySequence<E>(a); |
50 | } |
51 | } |
52 | |
53 | /** |
54 | * Returns a new instance of {@link Sequence} that is backed by the given {@link java.util.List}. |
55 | * |
56 | * @param l The {@link java.util.List} that backs the returned {@link Sequence}. |
57 | * @return A new instance of {@link Sequence} that is backed by the given {@link java.util.List}. |
58 | * @throws NullPointerException If <code>l</code> is <code>null</code>. |
59 | */ |
60 | public static <E> Sequence<E> newSequence(final List<E> l) throws NullPointerException |
61 | { |
62 | if(l == null) |
63 | { |
64 | throw new NullPointerException(); |
65 | } |
66 | else |
67 | { |
68 | return new ListSequence<E>(l); |
69 | } |
70 | } |
71 | |
72 | /** |
73 | * Returns a new instance of {@link Sequence} that is backed by the given constant for the given length. |
74 | * |
75 | * @param constant The constant that backs the returned {@link Sequence}. |
76 | * @param length The length of the constant that backs the returned {@link Sequence}. |
77 | * @return A new instance of {@link Sequence} that is backed by the given constant. |
78 | * @throws IllegalArgumentException If <code>length</code> is less than <code>1</code>. |
79 | */ |
80 | public static <E> Sequence<E> newConstantSequence(final E constant, final int length) throws IllegalArgumentException |
81 | { |
82 | if(length < 1) |
83 | { |
84 | throw new IllegalArgumentException(); |
85 | } |
86 | else |
87 | { |
88 | return new ConstantSequence<E>(constant, length); |
89 | } |
90 | } |
91 | |
92 | private static final class ArraySequence<E> implements Sequence<E> |
93 | { |
94 | private static final long serialVersionUID = 2L; |
95 | |
96 | private final E[] a; |
97 | private int hc; |
98 | private boolean hcCalculated; |
99 | private String ts; |
100 | |
101 | ArraySequence(final E... a) |
102 | { |
103 | this.a = a; |
104 | } |
105 | |
106 | public E get(final int index) throws IndexOutOfBoundsException |
107 | { |
108 | if(index < 0 || index >= length()) |
109 | { |
110 | throw new IndexOutOfBoundsException(String.valueOf(index)); |
111 | } |
112 | else |
113 | { |
114 | return a[index]; |
115 | } |
116 | } |
117 | |
118 | public int length() |
119 | { |
120 | return a.length; |
121 | } |
122 | |
123 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
124 | { |
125 | if(sequence == null) |
126 | { |
127 | throw new NullPointerException(); |
128 | } |
129 | else if(length() == 0) |
130 | { |
131 | return sequence; |
132 | } |
133 | else if(sequence.length() == 0) |
134 | { |
135 | return this; |
136 | } |
137 | else |
138 | { |
139 | return new CompositeSequence<E>(this, sequence); |
140 | } |
141 | } |
142 | |
143 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
144 | { |
145 | if(fromPosition < 0) |
146 | { |
147 | throw new PositionOutOfBoundsException(fromPosition); |
148 | } |
149 | else if(toPosition > length()) |
150 | { |
151 | throw new PositionOutOfBoundsException(toPosition); |
152 | } |
153 | else if(fromPosition > toPosition) |
154 | { |
155 | throw new PositionOutOfBoundsException(fromPosition); |
156 | } |
157 | else |
158 | { |
159 | return new SubSequence<E>(this, fromPosition, toPosition); |
160 | } |
161 | } |
162 | |
163 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
164 | { |
165 | if(sequence == null) |
166 | { |
167 | throw new NullPointerException(); |
168 | } |
169 | else |
170 | { |
171 | if(position < 0 || position > length()) |
172 | { |
173 | throw new PositionOutOfBoundsException(position); |
174 | } |
175 | else if(position == 0) |
176 | { |
177 | return sequence.append(this); |
178 | } |
179 | else if(position == length()) |
180 | { |
181 | return this.append(sequence); |
182 | } |
183 | else |
184 | { |
185 | return new InsertSequence<E>(position, this, sequence); |
186 | } |
187 | } |
188 | } |
189 | |
190 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
191 | { |
192 | if(fromPosition < 0) |
193 | { |
194 | throw new PositionOutOfBoundsException(fromPosition); |
195 | } |
196 | else if(toPosition > length()) |
197 | { |
198 | throw new PositionOutOfBoundsException(toPosition); |
199 | } |
200 | else if(fromPosition > toPosition) |
201 | { |
202 | throw new PositionOutOfBoundsException(fromPosition); |
203 | } |
204 | else if(fromPosition == toPosition) |
205 | { |
206 | return this; |
207 | } |
208 | else |
209 | { |
210 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
211 | } |
212 | } |
213 | |
214 | public int indexOf(final E e) |
215 | { |
216 | return indexOf(0, e); |
217 | } |
218 | |
219 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
220 | { |
221 | if(position < 0 || position > length()) |
222 | { |
223 | throw new PositionOutOfBoundsException(position); |
224 | } |
225 | else |
226 | { |
227 | final SequenceIterator<E> it = getIterator(position); |
228 | |
229 | while(it.hasNext()) |
230 | { |
231 | final int itPosition = it.getPosition(); |
232 | final E x = it.next(); |
233 | |
234 | if(x == null ? e == null : x.equals(e)) |
235 | { |
236 | return itPosition; |
237 | } |
238 | } |
239 | |
240 | return -1; |
241 | } |
242 | } |
243 | |
244 | public int lastIndexOf(final E e) |
245 | { |
246 | return lastIndexOf(length(), e); |
247 | } |
248 | |
249 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
250 | { |
251 | if(position < 0 || position > length()) |
252 | { |
253 | throw new PositionOutOfBoundsException(position); |
254 | } |
255 | else |
256 | { |
257 | final SequenceIterator<E> it = getIterator(position); |
258 | |
259 | while(it.hasPrevious()) |
260 | { |
261 | final E x = it.previous(); |
262 | final int itPosition = it.getPosition(); |
263 | |
264 | if(x == null ? e == null : x.equals(e)) |
265 | { |
266 | return itPosition; |
267 | } |
268 | } |
269 | |
270 | return -1; |
271 | } |
272 | } |
273 | |
274 | public boolean isEmpty() |
275 | { |
276 | return length() == 0; |
277 | } |
278 | |
279 | public Iterator<E> iterator() |
280 | { |
281 | return getIterator(0); |
282 | } |
283 | |
284 | public SequenceIterator<E> getIterator() |
285 | { |
286 | return getIterator(0); |
287 | } |
288 | |
289 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
290 | { |
291 | if(position < 0 || position > length()) |
292 | { |
293 | throw new PositionOutOfBoundsException(position); |
294 | } |
295 | else |
296 | { |
297 | return new SequenceIterator<E>() |
298 | { |
299 | private int pos; |
300 | |
301 | { |
302 | pos = position; |
303 | } |
304 | |
305 | public final boolean hasNext() |
306 | { |
307 | return pos != length(); |
308 | } |
309 | |
310 | public final E next() |
311 | { |
312 | if(pos == length()) |
313 | { |
314 | throw new NoSuchElementException(); |
315 | } |
316 | else |
317 | { |
318 | return a[pos++]; |
319 | } |
320 | } |
321 | |
322 | public final void remove() |
323 | { |
324 | throw new UnsupportedOperationException(); |
325 | } |
326 | |
327 | public boolean hasPrevious() |
328 | { |
329 | return pos != 0; |
330 | } |
331 | |
332 | public E previous() throws NoSuchElementException |
333 | { |
334 | if(pos == 0) |
335 | { |
336 | throw new NoSuchElementException(); |
337 | } |
338 | else |
339 | { |
340 | return a[--pos]; |
341 | } |
342 | } |
343 | |
344 | public int getPosition() |
345 | { |
346 | return pos; |
347 | } |
348 | }; |
349 | } |
350 | } |
351 | |
352 | @Override |
353 | public String toString() |
354 | { |
355 | if(ts == null) |
356 | { |
357 | final StringBuilder sb = new StringBuilder(); |
358 | |
359 | sb.append("["); |
360 | |
361 | final SequenceIterator<E> it = getIterator(); |
362 | |
363 | while(it.hasNext()) |
364 | { |
365 | sb.append(it.next()); |
366 | |
367 | if(it.hasNext()) |
368 | { |
369 | sb.append("]["); |
370 | } |
371 | } |
372 | |
373 | sb.append("]"); |
374 | |
375 | ts = sb.toString(); |
376 | } |
377 | |
378 | return ts; |
379 | } |
380 | |
381 | @Override |
382 | public boolean equals(final Object o) |
383 | { |
384 | if(this == o) |
385 | { |
386 | return true; |
387 | } |
388 | else if(o == null || (!(o instanceof Sequence))) |
389 | { |
390 | return false; |
391 | } |
392 | else |
393 | { |
394 | final Sequence<?> s = (Sequence<?>)o; |
395 | |
396 | if(s.length() != length()) |
397 | { |
398 | return false; |
399 | } |
400 | else |
401 | { |
402 | final Iterator<?> it1 = iterator(); |
403 | final Iterator<?> it2 = s.iterator(); |
404 | |
405 | while(it1.hasNext() && it2.hasNext()) |
406 | { |
407 | final Object se1 = it1.next(); |
408 | final Object se2 = it2.next(); |
409 | |
410 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
411 | { |
412 | return false; |
413 | } |
414 | } |
415 | |
416 | return true; |
417 | } |
418 | } |
419 | } |
420 | |
421 | @Override |
422 | public int hashCode() |
423 | { |
424 | if(!hcCalculated) |
425 | { |
426 | final int oddPrime = 461; |
427 | int result = 73; |
428 | |
429 | for(E e : this) |
430 | { |
431 | if(e != null) |
432 | { |
433 | result = result * oddPrime + e.hashCode(); |
434 | } |
435 | } |
436 | |
437 | hc = result; |
438 | hcCalculated = true; |
439 | } |
440 | |
441 | return hc; |
442 | } |
443 | |
444 | public Sequence<E> clone() |
445 | { |
446 | return new ArraySequence<E>(a); |
447 | } |
448 | } |
449 | |
450 | private static final class ListSequence<E> implements Sequence<E> |
451 | { |
452 | private static final long serialVersionUID = 2L; |
453 | |
454 | private final List<E> l; |
455 | private int hc; |
456 | private boolean hcCalculated; |
457 | private String ts; |
458 | |
459 | ListSequence(final List<E> l) |
460 | { |
461 | this.l = l; |
462 | } |
463 | |
464 | public E get(final int index) throws IndexOutOfBoundsException |
465 | { |
466 | if(index < 0 || index >= length()) |
467 | { |
468 | throw new IndexOutOfBoundsException(String.valueOf(index)); |
469 | } |
470 | else |
471 | { |
472 | return l.get(index); |
473 | } |
474 | } |
475 | |
476 | public int length() |
477 | { |
478 | return l.size(); |
479 | } |
480 | |
481 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
482 | { |
483 | if(sequence == null) |
484 | { |
485 | throw new NullPointerException(); |
486 | } |
487 | else if(length() == 0) |
488 | { |
489 | return sequence; |
490 | } |
491 | else if(sequence.length() == 0) |
492 | { |
493 | return this; |
494 | } |
495 | else |
496 | { |
497 | return new CompositeSequence<E>(this, sequence); |
498 | } |
499 | } |
500 | |
501 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
502 | { |
503 | if(fromPosition < 0) |
504 | { |
505 | throw new PositionOutOfBoundsException(fromPosition); |
506 | } |
507 | else if(toPosition > length()) |
508 | { |
509 | throw new PositionOutOfBoundsException(toPosition); |
510 | } |
511 | else if(fromPosition > toPosition) |
512 | { |
513 | throw new PositionOutOfBoundsException(fromPosition); |
514 | } |
515 | else |
516 | { |
517 | return new SubSequence<E>(this, fromPosition, toPosition); |
518 | } |
519 | } |
520 | |
521 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
522 | { |
523 | if(sequence == null) |
524 | { |
525 | throw new NullPointerException(); |
526 | } |
527 | else |
528 | { |
529 | if(position < 0 || position > length()) |
530 | { |
531 | throw new PositionOutOfBoundsException(position); |
532 | } |
533 | else if(position == 0) |
534 | { |
535 | return sequence.append(this); |
536 | } |
537 | else if(position == length()) |
538 | { |
539 | return this.append(sequence); |
540 | } |
541 | else |
542 | { |
543 | return new InsertSequence<E>(position, this, sequence); |
544 | } |
545 | } |
546 | } |
547 | |
548 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
549 | { |
550 | if(fromPosition < 0) |
551 | { |
552 | throw new PositionOutOfBoundsException(fromPosition); |
553 | } |
554 | else if(toPosition > length()) |
555 | { |
556 | throw new PositionOutOfBoundsException(toPosition); |
557 | } |
558 | else if(fromPosition > toPosition) |
559 | { |
560 | throw new PositionOutOfBoundsException(fromPosition); |
561 | } |
562 | else if(fromPosition == toPosition) |
563 | { |
564 | return this; |
565 | } |
566 | else |
567 | { |
568 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
569 | } |
570 | } |
571 | |
572 | public int indexOf(final E e) |
573 | { |
574 | return indexOf(0, e); |
575 | } |
576 | |
577 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
578 | { |
579 | if(position < 0 || position > length()) |
580 | { |
581 | throw new PositionOutOfBoundsException(position); |
582 | } |
583 | else |
584 | { |
585 | final SequenceIterator<E> it = getIterator(position); |
586 | |
587 | while(it.hasNext()) |
588 | { |
589 | final int itPosition = it.getPosition(); |
590 | final E x = it.next(); |
591 | |
592 | if(x == null ? e == null : x.equals(e)) |
593 | { |
594 | return itPosition; |
595 | } |
596 | } |
597 | |
598 | return -1; |
599 | } |
600 | } |
601 | |
602 | public int lastIndexOf(final E e) |
603 | { |
604 | return lastIndexOf(length(), e); |
605 | } |
606 | |
607 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
608 | { |
609 | if(position < 0 || position > length()) |
610 | { |
611 | throw new PositionOutOfBoundsException(position); |
612 | } |
613 | else |
614 | { |
615 | final SequenceIterator<E> it = getIterator(position); |
616 | |
617 | while(it.hasPrevious()) |
618 | { |
619 | final E x = it.previous(); |
620 | final int itPosition = it.getPosition(); |
621 | |
622 | if(x == null ? e == null : x.equals(e)) |
623 | { |
624 | return itPosition; |
625 | } |
626 | } |
627 | |
628 | return -1; |
629 | } |
630 | } |
631 | |
632 | public boolean isEmpty() |
633 | { |
634 | return length() == 0; |
635 | } |
636 | |
637 | public Iterator<E> iterator() |
638 | { |
639 | return getIterator(0); |
640 | } |
641 | |
642 | public SequenceIterator<E> getIterator() |
643 | { |
644 | return getIterator(0); |
645 | } |
646 | |
647 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
648 | { |
649 | if(position < 0 || position > length()) |
650 | { |
651 | throw new PositionOutOfBoundsException(position); |
652 | } |
653 | else |
654 | { |
655 | return new SequenceIterator<E>() |
656 | { |
657 | private int pos; |
658 | private final ListIterator<E> it; |
659 | |
660 | { |
661 | pos = position; |
662 | it = l.listIterator(position); |
663 | } |
664 | |
665 | public final boolean hasNext() |
666 | { |
667 | return pos != length(); |
668 | } |
669 | |
670 | public final E next() |
671 | { |
672 | if(pos == length()) |
673 | { |
674 | throw new NoSuchElementException(); |
675 | } |
676 | else |
677 | { |
678 | pos++; |
679 | return it.next(); |
680 | } |
681 | } |
682 | |
683 | public final void remove() |
684 | { |
685 | throw new UnsupportedOperationException(); |
686 | } |
687 | |
688 | public boolean hasPrevious() |
689 | { |
690 | return pos != 0; |
691 | } |
692 | |
693 | public E previous() throws NoSuchElementException |
694 | { |
695 | if(pos == 0) |
696 | { |
697 | throw new NoSuchElementException(); |
698 | } |
699 | else |
700 | { |
701 | pos--; |
702 | return it.previous(); |
703 | } |
704 | } |
705 | |
706 | public int getPosition() |
707 | { |
708 | return pos; |
709 | } |
710 | }; |
711 | } |
712 | } |
713 | |
714 | @Override |
715 | public String toString() |
716 | { |
717 | if(ts == null) |
718 | { |
719 | final StringBuilder sb = new StringBuilder(); |
720 | |
721 | sb.append("["); |
722 | |
723 | final SequenceIterator<E> it = getIterator(); |
724 | |
725 | while(it.hasNext()) |
726 | { |
727 | sb.append(it.next()); |
728 | |
729 | if(it.hasNext()) |
730 | { |
731 | sb.append("]["); |
732 | } |
733 | } |
734 | |
735 | sb.append("]"); |
736 | |
737 | ts = sb.toString(); |
738 | } |
739 | |
740 | return ts; |
741 | } |
742 | |
743 | @Override |
744 | public boolean equals(final Object o) |
745 | { |
746 | if(this == o) |
747 | { |
748 | return true; |
749 | } |
750 | else if(o == null || (!(o instanceof Sequence))) |
751 | { |
752 | return false; |
753 | } |
754 | else |
755 | { |
756 | final Sequence<?> s = (Sequence<?>)o; |
757 | |
758 | if(s.length() != length()) |
759 | { |
760 | return false; |
761 | } |
762 | else |
763 | { |
764 | final Iterator<?> it1 = iterator(); |
765 | final Iterator<?> it2 = s.iterator(); |
766 | |
767 | while(it1.hasNext() && it2.hasNext()) |
768 | { |
769 | final Object se1 = it1.next(); |
770 | final Object se2 = it2.next(); |
771 | |
772 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
773 | { |
774 | return false; |
775 | } |
776 | } |
777 | |
778 | return true; |
779 | } |
780 | } |
781 | } |
782 | |
783 | @Override |
784 | public int hashCode() |
785 | { |
786 | if(!hcCalculated) |
787 | { |
788 | final int oddPrime = 461; |
789 | int result = 73; |
790 | |
791 | for(E e : this) |
792 | { |
793 | if(e != null) |
794 | { |
795 | result = result * oddPrime + e.hashCode(); |
796 | } |
797 | } |
798 | |
799 | hc = result; |
800 | hcCalculated = true; |
801 | } |
802 | |
803 | return hc; |
804 | } |
805 | |
806 | public Sequence<E> clone() |
807 | { |
808 | return new ListSequence<E>(l); |
809 | } |
810 | } |
811 | |
812 | private static final class CompositeSequence<E> implements Sequence<E> |
813 | { |
814 | private static final long serialVersionUID = 2L; |
815 | |
816 | private final Sequence<E> seq1; |
817 | private final Sequence<E> seq2; |
818 | private int hc; |
819 | private boolean hcCalculated; |
820 | private String ts; |
821 | |
822 | CompositeSequence(final Sequence<E> seq1, final Sequence<E> seq2) |
823 | { |
824 | this.seq1 = seq1; |
825 | this.seq2 = seq2; |
826 | } |
827 | |
828 | public E get(final int index) throws IndexOutOfBoundsException |
829 | { |
830 | if(index < 0 || index >= length()) |
831 | { |
832 | throw new IndexOutOfBoundsException(); |
833 | } |
834 | else if(index < seq1.length()) |
835 | { |
836 | return seq1.get(index); |
837 | } |
838 | else |
839 | { |
840 | return seq2.get(index - seq1.length()); |
841 | } |
842 | } |
843 | |
844 | public int length() |
845 | { |
846 | return seq1.length() + seq2.length(); |
847 | } |
848 | |
849 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
850 | { |
851 | if(sequence == null) |
852 | { |
853 | throw new NullPointerException(); |
854 | } |
855 | else if(sequence.length() == 0) |
856 | { |
857 | return this; |
858 | } |
859 | else |
860 | { |
861 | return new CompositeSequence<E>(this, sequence); |
862 | } |
863 | } |
864 | |
865 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
866 | { |
867 | if(fromPosition < 0) |
868 | { |
869 | throw new PositionOutOfBoundsException(fromPosition); |
870 | } |
871 | else if(toPosition > length()) |
872 | { |
873 | throw new PositionOutOfBoundsException(toPosition); |
874 | } |
875 | else if(fromPosition > toPosition) |
876 | { |
877 | throw new PositionOutOfBoundsException(fromPosition); |
878 | } |
879 | else |
880 | { |
881 | return new SubSequence<E>(this, fromPosition, toPosition); |
882 | } |
883 | } |
884 | |
885 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
886 | { |
887 | if(sequence == null) |
888 | { |
889 | throw new NullPointerException(); |
890 | } |
891 | else |
892 | { |
893 | if(position < 0 || position > length()) |
894 | { |
895 | throw new PositionOutOfBoundsException(position); |
896 | } |
897 | else if(position == 0) |
898 | { |
899 | return sequence.append(this); |
900 | } |
901 | else if(position == length()) |
902 | { |
903 | return this.append(sequence); |
904 | } |
905 | else |
906 | { |
907 | return new InsertSequence<E>(position, this, sequence); |
908 | } |
909 | } |
910 | } |
911 | |
912 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
913 | { |
914 | if(fromPosition < 0) |
915 | { |
916 | throw new PositionOutOfBoundsException(fromPosition); |
917 | } |
918 | else if(toPosition > length()) |
919 | { |
920 | throw new PositionOutOfBoundsException(toPosition); |
921 | } |
922 | else if(fromPosition > toPosition) |
923 | { |
924 | throw new PositionOutOfBoundsException(fromPosition); |
925 | } |
926 | else if(fromPosition == toPosition) |
927 | { |
928 | return this; |
929 | } |
930 | else |
931 | { |
932 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
933 | } |
934 | } |
935 | |
936 | public int indexOf(final E e) |
937 | { |
938 | return indexOf(0, e); |
939 | } |
940 | |
941 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
942 | { |
943 | if(position < 0 || position > length()) |
944 | { |
945 | throw new PositionOutOfBoundsException(position); |
946 | } |
947 | else |
948 | { |
949 | final SequenceIterator<E> it = getIterator(position); |
950 | |
951 | while(it.hasNext()) |
952 | { |
953 | final int itPosition = it.getPosition(); |
954 | final E x = it.next(); |
955 | |
956 | if(x == null ? e == null : x.equals(e)) |
957 | { |
958 | return itPosition; |
959 | } |
960 | } |
961 | |
962 | return -1; |
963 | } |
964 | } |
965 | |
966 | public int lastIndexOf(final E e) |
967 | { |
968 | return lastIndexOf(length(), e); |
969 | } |
970 | |
971 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
972 | { |
973 | if(position < 0 || position > length()) |
974 | { |
975 | throw new PositionOutOfBoundsException(position); |
976 | } |
977 | else |
978 | { |
979 | final SequenceIterator<E> it = getIterator(position); |
980 | |
981 | while(it.hasPrevious()) |
982 | { |
983 | final E x = it.previous(); |
984 | final int itPosition = it.getPosition(); |
985 | |
986 | if(x == null ? e == null : x.equals(e)) |
987 | { |
988 | return itPosition; |
989 | } |
990 | } |
991 | |
992 | return -1; |
993 | } |
994 | } |
995 | |
996 | public boolean isEmpty() |
997 | { |
998 | return false; |
999 | } |
1000 | |
1001 | public Iterator<E> iterator() |
1002 | { |
1003 | return getIterator(0); |
1004 | } |
1005 | |
1006 | public SequenceIterator<E> getIterator() |
1007 | { |
1008 | return getIterator(0); |
1009 | } |
1010 | |
1011 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
1012 | { |
1013 | if(position < 0 || position > length()) |
1014 | { |
1015 | throw new PositionOutOfBoundsException(position); |
1016 | } |
1017 | else |
1018 | { |
1019 | return new SequenceIterator<E>() |
1020 | { |
1021 | private int pos; |
1022 | |
1023 | { |
1024 | pos = position; |
1025 | } |
1026 | |
1027 | public final boolean hasNext() |
1028 | { |
1029 | return pos != length(); |
1030 | } |
1031 | |
1032 | public final E next() |
1033 | { |
1034 | if(pos == length()) |
1035 | { |
1036 | throw new NoSuchElementException(); |
1037 | } |
1038 | else if(pos < seq1.length()) |
1039 | { |
1040 | return seq1.get(pos++); |
1041 | } |
1042 | else |
1043 | { |
1044 | return seq2.get(pos++ - seq1.length()); |
1045 | } |
1046 | } |
1047 | |
1048 | public final void remove() |
1049 | { |
1050 | throw new UnsupportedOperationException(); |
1051 | } |
1052 | |
1053 | public boolean hasPrevious() |
1054 | { |
1055 | return pos != 0; |
1056 | } |
1057 | |
1058 | public E previous() throws NoSuchElementException |
1059 | { |
1060 | if(pos == 0) |
1061 | { |
1062 | throw new NoSuchElementException(); |
1063 | } |
1064 | else if(pos <= seq1.length()) |
1065 | { |
1066 | return seq1.get(--pos); |
1067 | } |
1068 | else |
1069 | { |
1070 | return seq2.get(--pos - seq1.length()); |
1071 | } |
1072 | } |
1073 | |
1074 | public int getPosition() |
1075 | { |
1076 | return pos; |
1077 | } |
1078 | }; |
1079 | } |
1080 | } |
1081 | |
1082 | @Override |
1083 | public String toString() |
1084 | { |
1085 | if(ts == null) |
1086 | { |
1087 | final StringBuilder sb = new StringBuilder(); |
1088 | |
1089 | sb.append("["); |
1090 | |
1091 | final SequenceIterator<E> it = getIterator(); |
1092 | |
1093 | while(it.hasNext()) |
1094 | { |
1095 | sb.append(it.next()); |
1096 | |
1097 | if(it.hasNext()) |
1098 | { |
1099 | sb.append("]["); |
1100 | } |
1101 | } |
1102 | |
1103 | sb.append("]"); |
1104 | |
1105 | ts = sb.toString(); |
1106 | } |
1107 | |
1108 | return ts; |
1109 | } |
1110 | |
1111 | @Override |
1112 | public boolean equals(final Object o) |
1113 | { |
1114 | if(this == o) |
1115 | { |
1116 | return true; |
1117 | } |
1118 | else if(o == null || (!(o instanceof Sequence))) |
1119 | { |
1120 | return false; |
1121 | } |
1122 | else |
1123 | { |
1124 | final Sequence<?> s = (Sequence<?>)o; |
1125 | |
1126 | if(s.length() != length()) |
1127 | { |
1128 | return false; |
1129 | } |
1130 | else |
1131 | { |
1132 | final Iterator<?> it1 = iterator(); |
1133 | final Iterator<?> it2 = s.iterator(); |
1134 | |
1135 | while(it1.hasNext() && it2.hasNext()) |
1136 | { |
1137 | final Object se1 = it1.next(); |
1138 | final Object se2 = it2.next(); |
1139 | |
1140 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
1141 | { |
1142 | return false; |
1143 | } |
1144 | } |
1145 | |
1146 | return true; |
1147 | } |
1148 | } |
1149 | } |
1150 | |
1151 | @Override |
1152 | public int hashCode() |
1153 | { |
1154 | if(!hcCalculated) |
1155 | { |
1156 | final int oddPrime = 461; |
1157 | int result = 73; |
1158 | |
1159 | for(E e : this) |
1160 | { |
1161 | if(e != null) |
1162 | { |
1163 | result = result * oddPrime + e.hashCode(); |
1164 | } |
1165 | } |
1166 | |
1167 | hc = result; |
1168 | hcCalculated = true; |
1169 | } |
1170 | |
1171 | return hc; |
1172 | } |
1173 | |
1174 | public Sequence<E> clone() |
1175 | { |
1176 | return new CompositeSequence<E>(seq1, seq2); |
1177 | } |
1178 | } |
1179 | |
1180 | private static final class SubSequence<E> implements Sequence<E> |
1181 | { |
1182 | private static final long serialVersionUID = 2L; |
1183 | |
1184 | private final Sequence<E> sequence; |
1185 | private final int fromPosition; |
1186 | private final int toPosition; |
1187 | private int hc; |
1188 | private boolean hcCalculated; |
1189 | private String ts; |
1190 | |
1191 | SubSequence(final Sequence<E> sequence, final int fromPosition, final int toPosition) |
1192 | { |
1193 | this.sequence = sequence; |
1194 | this.fromPosition = fromPosition; |
1195 | this.toPosition = toPosition; |
1196 | } |
1197 | |
1198 | public E get(final int index) throws IndexOutOfBoundsException |
1199 | { |
1200 | if(index < 0 || index >= length()) |
1201 | { |
1202 | throw new IndexOutOfBoundsException(); |
1203 | } |
1204 | else |
1205 | { |
1206 | return sequence.get(index + fromPosition); |
1207 | } |
1208 | } |
1209 | |
1210 | public int length() |
1211 | { |
1212 | return toPosition - fromPosition; |
1213 | } |
1214 | |
1215 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
1216 | { |
1217 | if(sequence == null) |
1218 | { |
1219 | throw new NullPointerException(); |
1220 | } |
1221 | else if(length() == 0) |
1222 | { |
1223 | return sequence; |
1224 | } |
1225 | else if(sequence.length() == 0) |
1226 | { |
1227 | return this; |
1228 | } |
1229 | else |
1230 | { |
1231 | return new CompositeSequence<E>(this, sequence); |
1232 | } |
1233 | } |
1234 | |
1235 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
1236 | { |
1237 | if(fromPosition < 0) |
1238 | { |
1239 | throw new PositionOutOfBoundsException(fromPosition); |
1240 | } |
1241 | else if(toPosition > length()) |
1242 | { |
1243 | throw new PositionOutOfBoundsException(toPosition); |
1244 | } |
1245 | else if(fromPosition > toPosition) |
1246 | { |
1247 | throw new PositionOutOfBoundsException(fromPosition); |
1248 | } |
1249 | else |
1250 | { |
1251 | return new SubSequence<E>(this, fromPosition, toPosition); |
1252 | } |
1253 | } |
1254 | |
1255 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
1256 | { |
1257 | if(sequence == null) |
1258 | { |
1259 | throw new NullPointerException(); |
1260 | } |
1261 | else |
1262 | { |
1263 | if(position < 0 || position > length()) |
1264 | { |
1265 | throw new PositionOutOfBoundsException(position); |
1266 | } |
1267 | else if(position == 0) |
1268 | { |
1269 | return sequence.append(this); |
1270 | } |
1271 | else if(position == length()) |
1272 | { |
1273 | return this.append(sequence); |
1274 | } |
1275 | else |
1276 | { |
1277 | return new InsertSequence<E>(position, this, sequence); |
1278 | } |
1279 | } |
1280 | } |
1281 | |
1282 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
1283 | { |
1284 | if(fromPosition < 0) |
1285 | { |
1286 | throw new PositionOutOfBoundsException(fromPosition); |
1287 | } |
1288 | else if(toPosition > length()) |
1289 | { |
1290 | throw new PositionOutOfBoundsException(toPosition); |
1291 | } |
1292 | else if(fromPosition > toPosition) |
1293 | { |
1294 | throw new PositionOutOfBoundsException(fromPosition); |
1295 | } |
1296 | else if(fromPosition == toPosition) |
1297 | { |
1298 | return this; |
1299 | } |
1300 | else |
1301 | { |
1302 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
1303 | } |
1304 | } |
1305 | |
1306 | public int indexOf(final E e) |
1307 | { |
1308 | return indexOf(0, e); |
1309 | } |
1310 | |
1311 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
1312 | { |
1313 | if(position < 0 || position > length()) |
1314 | { |
1315 | throw new PositionOutOfBoundsException(position); |
1316 | } |
1317 | else |
1318 | { |
1319 | final SequenceIterator<E> it = getIterator(position); |
1320 | |
1321 | while(it.hasNext()) |
1322 | { |
1323 | final int itPosition = it.getPosition(); |
1324 | final E x = it.next(); |
1325 | |
1326 | if(x == null ? e == null : x.equals(e)) |
1327 | { |
1328 | return itPosition; |
1329 | } |
1330 | } |
1331 | |
1332 | return -1; |
1333 | } |
1334 | } |
1335 | |
1336 | public int lastIndexOf(final E e) |
1337 | { |
1338 | return lastIndexOf(length(), e); |
1339 | } |
1340 | |
1341 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
1342 | { |
1343 | if(position < 0 || position > length()) |
1344 | { |
1345 | throw new PositionOutOfBoundsException(position); |
1346 | } |
1347 | else |
1348 | { |
1349 | final SequenceIterator<E> it = getIterator(position); |
1350 | |
1351 | while(it.hasPrevious()) |
1352 | { |
1353 | final E x = it.previous(); |
1354 | final int itPosition = it.getPosition(); |
1355 | |
1356 | if(x == null ? e == null : x.equals(e)) |
1357 | { |
1358 | return itPosition; |
1359 | } |
1360 | } |
1361 | |
1362 | return -1; |
1363 | } |
1364 | } |
1365 | |
1366 | public boolean isEmpty() |
1367 | { |
1368 | return length() == 0; |
1369 | } |
1370 | |
1371 | public Iterator<E> iterator() |
1372 | { |
1373 | return getIterator(0); |
1374 | } |
1375 | |
1376 | public SequenceIterator<E> getIterator() |
1377 | { |
1378 | return getIterator(0); |
1379 | } |
1380 | |
1381 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
1382 | { |
1383 | if(position < 0 || position > length()) |
1384 | { |
1385 | throw new PositionOutOfBoundsException(position); |
1386 | } |
1387 | else |
1388 | { |
1389 | return new SequenceIterator<E>() |
1390 | { |
1391 | private int pos; |
1392 | |
1393 | { |
1394 | pos = position; |
1395 | } |
1396 | |
1397 | public boolean hasNext() |
1398 | { |
1399 | return pos != length(); |
1400 | } |
1401 | |
1402 | public E next() throws NoSuchElementException |
1403 | { |
1404 | if(pos == length()) |
1405 | { |
1406 | throw new NoSuchElementException(); |
1407 | } |
1408 | else |
1409 | { |
1410 | return sequence.get(SubSequence.this.fromPosition + pos++); |
1411 | } |
1412 | } |
1413 | |
1414 | public void remove() |
1415 | { |
1416 | throw new UnsupportedOperationException(); |
1417 | } |
1418 | |
1419 | public boolean hasPrevious() |
1420 | { |
1421 | return pos != 0; |
1422 | } |
1423 | |
1424 | public E previous() throws NoSuchElementException |
1425 | { |
1426 | if(pos == 0) |
1427 | { |
1428 | throw new NoSuchElementException(); |
1429 | } |
1430 | else |
1431 | { |
1432 | return sequence.get(SubSequence.this.fromPosition + --pos); |
1433 | } |
1434 | } |
1435 | |
1436 | public int getPosition() |
1437 | { |
1438 | return pos; |
1439 | } |
1440 | }; |
1441 | } |
1442 | } |
1443 | |
1444 | @Override |
1445 | public String toString() |
1446 | { |
1447 | if(ts == null) |
1448 | { |
1449 | final StringBuilder sb = new StringBuilder(); |
1450 | |
1451 | sb.append("["); |
1452 | |
1453 | final SequenceIterator<E> it = getIterator(); |
1454 | |
1455 | while(it.hasNext()) |
1456 | { |
1457 | sb.append(it.next()); |
1458 | |
1459 | if(it.hasNext()) |
1460 | { |
1461 | sb.append("]["); |
1462 | } |
1463 | } |
1464 | |
1465 | sb.append("]"); |
1466 | |
1467 | ts = sb.toString(); |
1468 | } |
1469 | |
1470 | return ts; |
1471 | } |
1472 | |
1473 | @Override |
1474 | public boolean equals(final Object o) |
1475 | { |
1476 | if(this == o) |
1477 | { |
1478 | return true; |
1479 | } |
1480 | else if(o == null || (!(o instanceof Sequence))) |
1481 | { |
1482 | return false; |
1483 | } |
1484 | else |
1485 | { |
1486 | final Sequence<?> s = (Sequence<?>)o; |
1487 | |
1488 | if(s.length() != length()) |
1489 | { |
1490 | return false; |
1491 | } |
1492 | else |
1493 | { |
1494 | final Iterator<?> it1 = iterator(); |
1495 | final Iterator<?> it2 = s.iterator(); |
1496 | |
1497 | while(it1.hasNext() && it2.hasNext()) |
1498 | { |
1499 | final Object se1 = it1.next(); |
1500 | final Object se2 = it2.next(); |
1501 | |
1502 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
1503 | { |
1504 | return false; |
1505 | } |
1506 | } |
1507 | |
1508 | return true; |
1509 | } |
1510 | } |
1511 | } |
1512 | |
1513 | @Override |
1514 | public int hashCode() |
1515 | { |
1516 | if(!hcCalculated) |
1517 | { |
1518 | final int oddPrime = 461; |
1519 | int result = 73; |
1520 | |
1521 | for(E e : this) |
1522 | { |
1523 | if(e != null) |
1524 | { |
1525 | result = result * oddPrime + e.hashCode(); |
1526 | } |
1527 | } |
1528 | |
1529 | hc = result; |
1530 | hcCalculated = true; |
1531 | } |
1532 | |
1533 | return hc; |
1534 | } |
1535 | |
1536 | public Sequence<E> clone() |
1537 | { |
1538 | return new SubSequence<E>(sequence, fromPosition, toPosition); |
1539 | } |
1540 | } |
1541 | |
1542 | private static final class InsertSequence<E> implements Sequence<E> |
1543 | { |
1544 | private static final long serialVersionUID = 2L; |
1545 | |
1546 | private final int position; |
1547 | private final Sequence<E> sequence; |
1548 | private final Sequence<E> inserted; |
1549 | private int hc; |
1550 | private boolean hcCalculated; |
1551 | private String ts; |
1552 | |
1553 | InsertSequence(final int position, final Sequence<E> sequence, final Sequence<E> inserted) |
1554 | { |
1555 | this.position = position; |
1556 | this.sequence = sequence; |
1557 | this.inserted = inserted; |
1558 | } |
1559 | |
1560 | public E get(final int index) throws IndexOutOfBoundsException |
1561 | { |
1562 | if(index < 0 || index >= length()) |
1563 | { |
1564 | throw new IndexOutOfBoundsException(); |
1565 | } |
1566 | else if(index < position) |
1567 | { |
1568 | return sequence.get(index); |
1569 | } |
1570 | else if(index < inserted.length() + position) |
1571 | { |
1572 | return inserted.get(index - position); |
1573 | } |
1574 | else |
1575 | { |
1576 | return sequence.get(index - inserted.length()); |
1577 | } |
1578 | } |
1579 | |
1580 | public int length() |
1581 | { |
1582 | return sequence.length() + inserted.length(); |
1583 | } |
1584 | |
1585 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
1586 | { |
1587 | if(sequence == null) |
1588 | { |
1589 | throw new NullPointerException(); |
1590 | } |
1591 | else if(sequence.length() == 0) |
1592 | { |
1593 | return this; |
1594 | } |
1595 | else |
1596 | { |
1597 | return new CompositeSequence<E>(this, sequence); |
1598 | } |
1599 | } |
1600 | |
1601 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
1602 | { |
1603 | if(fromPosition < 0) |
1604 | { |
1605 | throw new PositionOutOfBoundsException(fromPosition); |
1606 | } |
1607 | else if(toPosition > length()) |
1608 | { |
1609 | throw new PositionOutOfBoundsException(toPosition); |
1610 | } |
1611 | else if(fromPosition > toPosition) |
1612 | { |
1613 | throw new PositionOutOfBoundsException(fromPosition); |
1614 | } |
1615 | else |
1616 | { |
1617 | return new SubSequence<E>(this, fromPosition, toPosition); |
1618 | } |
1619 | } |
1620 | |
1621 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
1622 | { |
1623 | if(sequence == null) |
1624 | { |
1625 | throw new NullPointerException(); |
1626 | } |
1627 | else |
1628 | { |
1629 | if(position < 0 || position > length()) |
1630 | { |
1631 | throw new PositionOutOfBoundsException(position); |
1632 | } |
1633 | else if(position == 0) |
1634 | { |
1635 | return sequence.append(this); |
1636 | } |
1637 | else if(position == length()) |
1638 | { |
1639 | return this.append(sequence); |
1640 | } |
1641 | else |
1642 | { |
1643 | return new InsertSequence<E>(position, this, sequence); |
1644 | } |
1645 | } |
1646 | } |
1647 | |
1648 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
1649 | { |
1650 | if(fromPosition < 0) |
1651 | { |
1652 | throw new PositionOutOfBoundsException(fromPosition); |
1653 | } |
1654 | else if(toPosition > length()) |
1655 | { |
1656 | throw new PositionOutOfBoundsException(toPosition); |
1657 | } |
1658 | else if(fromPosition > toPosition) |
1659 | { |
1660 | throw new PositionOutOfBoundsException(fromPosition); |
1661 | } |
1662 | else if(fromPosition == toPosition) |
1663 | { |
1664 | return this; |
1665 | } |
1666 | else |
1667 | { |
1668 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
1669 | } |
1670 | } |
1671 | |
1672 | public int indexOf(final E e) |
1673 | { |
1674 | return indexOf(0, e); |
1675 | } |
1676 | |
1677 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
1678 | { |
1679 | if(position < 0 || position > length()) |
1680 | { |
1681 | throw new PositionOutOfBoundsException(position); |
1682 | } |
1683 | else |
1684 | { |
1685 | final SequenceIterator<E> it = getIterator(position); |
1686 | |
1687 | while(it.hasNext()) |
1688 | { |
1689 | final int itPosition = it.getPosition(); |
1690 | final E x = it.next(); |
1691 | |
1692 | if(x == null ? e == null : x.equals(e)) |
1693 | { |
1694 | return itPosition; |
1695 | } |
1696 | } |
1697 | |
1698 | return -1; |
1699 | } |
1700 | } |
1701 | |
1702 | public int lastIndexOf(final E e) |
1703 | { |
1704 | return lastIndexOf(length(), e); |
1705 | } |
1706 | |
1707 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
1708 | { |
1709 | if(position < 0 || position > length()) |
1710 | { |
1711 | throw new PositionOutOfBoundsException(position); |
1712 | } |
1713 | else |
1714 | { |
1715 | final SequenceIterator<E> it = getIterator(position); |
1716 | |
1717 | while(it.hasPrevious()) |
1718 | { |
1719 | final E x = it.previous(); |
1720 | final int itPosition = it.getPosition(); |
1721 | |
1722 | if(x == null ? e == null : x.equals(e)) |
1723 | { |
1724 | return itPosition; |
1725 | } |
1726 | } |
1727 | |
1728 | return -1; |
1729 | } |
1730 | } |
1731 | |
1732 | public SequenceIterator<E> getIterator() |
1733 | { |
1734 | return getIterator(0); |
1735 | } |
1736 | |
1737 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
1738 | { |
1739 | if(position < 0 || position > length()) |
1740 | { |
1741 | throw new PositionOutOfBoundsException(position); |
1742 | } |
1743 | else |
1744 | { |
1745 | return new SequenceIterator<E>() |
1746 | { |
1747 | private int pos; |
1748 | |
1749 | { |
1750 | pos = position; |
1751 | } |
1752 | |
1753 | public final boolean hasNext() |
1754 | { |
1755 | return pos != length(); |
1756 | } |
1757 | |
1758 | public final E next() |
1759 | { |
1760 | if(pos == length()) |
1761 | { |
1762 | throw new NoSuchElementException(); |
1763 | } |
1764 | else if(pos < InsertSequence.this.position) |
1765 | { |
1766 | return sequence.get(pos++); |
1767 | } |
1768 | else if(pos < inserted.length() + InsertSequence.this.position) |
1769 | { |
1770 | return inserted.get(pos++ - InsertSequence.this.position); |
1771 | } |
1772 | else |
1773 | { |
1774 | return sequence.get(pos++ - inserted.length()); |
1775 | } |
1776 | } |
1777 | |
1778 | public final void remove() |
1779 | { |
1780 | throw new UnsupportedOperationException(); |
1781 | } |
1782 | |
1783 | public boolean hasPrevious() |
1784 | { |
1785 | return pos != 0; |
1786 | } |
1787 | |
1788 | public E previous() throws NoSuchElementException |
1789 | { |
1790 | if(pos == 0) |
1791 | { |
1792 | throw new NoSuchElementException(); |
1793 | } |
1794 | else if(pos <= InsertSequence.this.position) |
1795 | { |
1796 | return sequence.get(--pos); |
1797 | } |
1798 | else if(pos <= inserted.length() + InsertSequence.this.position) |
1799 | { |
1800 | return inserted.get(--pos - InsertSequence.this.position); |
1801 | } |
1802 | else |
1803 | { |
1804 | return sequence.get(--pos - inserted.length()); |
1805 | } |
1806 | } |
1807 | |
1808 | public int getPosition() |
1809 | { |
1810 | return pos; |
1811 | } |
1812 | }; |
1813 | } |
1814 | } |
1815 | |
1816 | public boolean isEmpty() |
1817 | { |
1818 | return false; |
1819 | } |
1820 | |
1821 | public Iterator<E> iterator() |
1822 | { |
1823 | return getIterator(0); |
1824 | } |
1825 | |
1826 | @Override |
1827 | public String toString() |
1828 | { |
1829 | if(ts == null) |
1830 | { |
1831 | final StringBuilder sb = new StringBuilder(); |
1832 | |
1833 | sb.append("["); |
1834 | |
1835 | final SequenceIterator<E> it = getIterator(); |
1836 | |
1837 | while(it.hasNext()) |
1838 | { |
1839 | sb.append(it.next()); |
1840 | |
1841 | if(it.hasNext()) |
1842 | { |
1843 | sb.append("]["); |
1844 | } |
1845 | } |
1846 | |
1847 | sb.append("]"); |
1848 | |
1849 | ts = sb.toString(); |
1850 | } |
1851 | |
1852 | return ts; |
1853 | } |
1854 | |
1855 | @Override |
1856 | public boolean equals(final Object o) |
1857 | { |
1858 | if(this == o) |
1859 | { |
1860 | return true; |
1861 | } |
1862 | else if(o == null || (!(o instanceof Sequence))) |
1863 | { |
1864 | return false; |
1865 | } |
1866 | else |
1867 | { |
1868 | final Sequence<?> s = (Sequence<?>)o; |
1869 | |
1870 | if(s.length() != length()) |
1871 | { |
1872 | return false; |
1873 | } |
1874 | else |
1875 | { |
1876 | final Iterator<?> it1 = iterator(); |
1877 | final Iterator<?> it2 = s.iterator(); |
1878 | |
1879 | while(it1.hasNext() && it2.hasNext()) |
1880 | { |
1881 | final Object se1 = it1.next(); |
1882 | final Object se2 = it2.next(); |
1883 | |
1884 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
1885 | { |
1886 | return false; |
1887 | } |
1888 | } |
1889 | |
1890 | return true; |
1891 | } |
1892 | } |
1893 | } |
1894 | |
1895 | @Override |
1896 | public int hashCode() |
1897 | { |
1898 | if(!hcCalculated) |
1899 | { |
1900 | final int oddPrime = 461; |
1901 | int result = 73; |
1902 | |
1903 | for(E e : this) |
1904 | { |
1905 | if(e != null) |
1906 | { |
1907 | result = result * oddPrime + e.hashCode(); |
1908 | } |
1909 | } |
1910 | |
1911 | hc = result; |
1912 | hcCalculated = true; |
1913 | } |
1914 | |
1915 | return hc; |
1916 | } |
1917 | |
1918 | public Sequence<E> clone() |
1919 | { |
1920 | return new InsertSequence<E>(position, sequence, inserted); |
1921 | } |
1922 | } |
1923 | |
1924 | private static final class DeleteSequence<E> implements Sequence<E> |
1925 | { |
1926 | private static final long serialVersionUID = 2L; |
1927 | |
1928 | private final int fromPosition; |
1929 | private final int toPosition; |
1930 | private final Sequence<E> sequence; |
1931 | private int hc; |
1932 | private boolean hcCalculated; |
1933 | private String ts; |
1934 | |
1935 | DeleteSequence(final int fromPosition, final int toPosition, final Sequence<E> sequence) |
1936 | { |
1937 | this.fromPosition = fromPosition; |
1938 | this.toPosition = toPosition; |
1939 | this.sequence = sequence; |
1940 | } |
1941 | |
1942 | public E get(final int index) throws IndexOutOfBoundsException |
1943 | { |
1944 | if(index < 0 || index >= length()) |
1945 | { |
1946 | throw new IndexOutOfBoundsException(); |
1947 | } |
1948 | else |
1949 | { |
1950 | if(index < fromPosition) |
1951 | { |
1952 | return sequence.get(index); |
1953 | } |
1954 | else |
1955 | { |
1956 | return sequence.get(index + toPosition - fromPosition); |
1957 | } |
1958 | } |
1959 | } |
1960 | |
1961 | public int length() |
1962 | { |
1963 | return sequence.length() - toPosition + fromPosition; |
1964 | } |
1965 | |
1966 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
1967 | { |
1968 | if(sequence == null) |
1969 | { |
1970 | throw new NullPointerException(); |
1971 | } |
1972 | else if(sequence.length() == 0) |
1973 | { |
1974 | return this; |
1975 | } |
1976 | else |
1977 | { |
1978 | return new CompositeSequence<E>(this, sequence); |
1979 | } |
1980 | } |
1981 | |
1982 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
1983 | { |
1984 | if(fromPosition < 0) |
1985 | { |
1986 | throw new PositionOutOfBoundsException(fromPosition); |
1987 | } |
1988 | else if(toPosition > length()) |
1989 | { |
1990 | throw new PositionOutOfBoundsException(toPosition); |
1991 | } |
1992 | else if(fromPosition > toPosition) |
1993 | { |
1994 | throw new PositionOutOfBoundsException(fromPosition); |
1995 | } |
1996 | else |
1997 | { |
1998 | return new SubSequence<E>(this, fromPosition, toPosition); |
1999 | } |
2000 | } |
2001 | |
2002 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
2003 | { |
2004 | if(sequence == null) |
2005 | { |
2006 | throw new NullPointerException(); |
2007 | } |
2008 | else |
2009 | { |
2010 | if(position < 0 || position > length()) |
2011 | { |
2012 | throw new PositionOutOfBoundsException(position); |
2013 | } |
2014 | else if(position == 0) |
2015 | { |
2016 | return sequence.append(this); |
2017 | } |
2018 | else if(position == length()) |
2019 | { |
2020 | return this.append(sequence); |
2021 | } |
2022 | else |
2023 | { |
2024 | return new InsertSequence<E>(position, this, sequence); |
2025 | } |
2026 | } |
2027 | } |
2028 | |
2029 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
2030 | { |
2031 | if(fromPosition < 0) |
2032 | { |
2033 | throw new PositionOutOfBoundsException(fromPosition); |
2034 | } |
2035 | else if(toPosition > length()) |
2036 | { |
2037 | throw new PositionOutOfBoundsException(toPosition); |
2038 | } |
2039 | else if(fromPosition > toPosition) |
2040 | { |
2041 | throw new PositionOutOfBoundsException(fromPosition); |
2042 | } |
2043 | else if(fromPosition == toPosition) |
2044 | { |
2045 | return this; |
2046 | } |
2047 | else |
2048 | { |
2049 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
2050 | } |
2051 | } |
2052 | |
2053 | public int indexOf(final E e) |
2054 | { |
2055 | return indexOf(0, e); |
2056 | } |
2057 | |
2058 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
2059 | { |
2060 | if(position < 0 || position > length()) |
2061 | { |
2062 | throw new PositionOutOfBoundsException(position); |
2063 | } |
2064 | else |
2065 | { |
2066 | final SequenceIterator<E> it = getIterator(position); |
2067 | |
2068 | while(it.hasNext()) |
2069 | { |
2070 | final int itPosition = it.getPosition(); |
2071 | final E x = it.next(); |
2072 | |
2073 | if(x == null ? e == null : x.equals(e)) |
2074 | { |
2075 | return itPosition; |
2076 | } |
2077 | } |
2078 | |
2079 | return -1; |
2080 | } |
2081 | } |
2082 | |
2083 | public int lastIndexOf(final E e) |
2084 | { |
2085 | return lastIndexOf(length(), e); |
2086 | } |
2087 | |
2088 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
2089 | { |
2090 | if(position < 0 || position > length()) |
2091 | { |
2092 | throw new PositionOutOfBoundsException(position); |
2093 | } |
2094 | else |
2095 | { |
2096 | final SequenceIterator<E> it = getIterator(position); |
2097 | |
2098 | while(it.hasPrevious()) |
2099 | { |
2100 | final E x = it.previous(); |
2101 | final int itPosition = it.getPosition(); |
2102 | |
2103 | if(x == null ? e == null : x.equals(e)) |
2104 | { |
2105 | return itPosition; |
2106 | } |
2107 | } |
2108 | |
2109 | return -1; |
2110 | } |
2111 | } |
2112 | |
2113 | public SequenceIterator<E> getIterator() |
2114 | { |
2115 | return getIterator(0); |
2116 | } |
2117 | |
2118 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
2119 | { |
2120 | if(position < 0 || position > length()) |
2121 | { |
2122 | throw new PositionOutOfBoundsException(position); |
2123 | } |
2124 | else |
2125 | { |
2126 | return new SequenceIterator<E>() |
2127 | { |
2128 | private int pos; |
2129 | |
2130 | { |
2131 | pos = position; |
2132 | } |
2133 | |
2134 | public final boolean hasNext() |
2135 | { |
2136 | return pos != length(); |
2137 | } |
2138 | |
2139 | public final E next() |
2140 | { |
2141 | if(pos == length()) |
2142 | { |
2143 | throw new NoSuchElementException(); |
2144 | } |
2145 | else if(pos < fromPosition) |
2146 | { |
2147 | return sequence.get(pos++); |
2148 | } |
2149 | else |
2150 | { |
2151 | return sequence.get(pos++ + toPosition - fromPosition); |
2152 | } |
2153 | } |
2154 | |
2155 | public final void remove() |
2156 | { |
2157 | throw new UnsupportedOperationException(); |
2158 | } |
2159 | |
2160 | public boolean hasPrevious() |
2161 | { |
2162 | return pos != 0; |
2163 | } |
2164 | |
2165 | public E previous() throws NoSuchElementException |
2166 | { |
2167 | if(pos == 0) |
2168 | { |
2169 | throw new NoSuchElementException(); |
2170 | } |
2171 | else if(pos <= fromPosition) |
2172 | { |
2173 | return sequence.get(--pos); |
2174 | } |
2175 | else |
2176 | { |
2177 | return sequence.get(--pos + toPosition - fromPosition); |
2178 | } |
2179 | } |
2180 | |
2181 | public int getPosition() |
2182 | { |
2183 | return pos; |
2184 | } |
2185 | }; |
2186 | } |
2187 | } |
2188 | |
2189 | public boolean isEmpty() |
2190 | { |
2191 | return fromPosition == 0 && toPosition == sequence.length(); |
2192 | } |
2193 | |
2194 | public Iterator<E> iterator() |
2195 | { |
2196 | return getIterator(0); |
2197 | } |
2198 | |
2199 | @Override |
2200 | public String toString() |
2201 | { |
2202 | if(ts == null) |
2203 | { |
2204 | final StringBuilder sb = new StringBuilder(); |
2205 | |
2206 | sb.append("["); |
2207 | |
2208 | final SequenceIterator<E> it = getIterator(); |
2209 | |
2210 | while(it.hasNext()) |
2211 | { |
2212 | sb.append(it.next()); |
2213 | |
2214 | if(it.hasNext()) |
2215 | { |
2216 | sb.append("]["); |
2217 | } |
2218 | } |
2219 | |
2220 | sb.append("]"); |
2221 | |
2222 | ts = sb.toString(); |
2223 | } |
2224 | |
2225 | return ts; |
2226 | } |
2227 | |
2228 | @Override |
2229 | public boolean equals(final Object o) |
2230 | { |
2231 | if(this == o) |
2232 | { |
2233 | return true; |
2234 | } |
2235 | else if(o == null || (!(o instanceof Sequence))) |
2236 | { |
2237 | return false; |
2238 | } |
2239 | else |
2240 | { |
2241 | final Sequence<?> s = (Sequence<?>)o; |
2242 | |
2243 | if(s.length() != length()) |
2244 | { |
2245 | return false; |
2246 | } |
2247 | else |
2248 | { |
2249 | final Iterator<?> it1 = iterator(); |
2250 | final Iterator<?> it2 = s.iterator(); |
2251 | |
2252 | while(it1.hasNext() && it2.hasNext()) |
2253 | { |
2254 | final Object se1 = it1.next(); |
2255 | final Object se2 = it2.next(); |
2256 | |
2257 | if(se1 == null ? se2 != null : !se1.equals(se2)) |
2258 | { |
2259 | return false; |
2260 | } |
2261 | } |
2262 | |
2263 | return true; |
2264 | } |
2265 | } |
2266 | } |
2267 | |
2268 | @Override |
2269 | public int hashCode() |
2270 | { |
2271 | if(!hcCalculated) |
2272 | { |
2273 | final int oddPrime = 461; |
2274 | int result = 73; |
2275 | |
2276 | for(E e : this) |
2277 | { |
2278 | if(e != null) |
2279 | { |
2280 | result = result * oddPrime + e.hashCode(); |
2281 | } |
2282 | } |
2283 | |
2284 | hc = result; |
2285 | hcCalculated = true; |
2286 | } |
2287 | |
2288 | return hc; |
2289 | } |
2290 | |
2291 | public Sequence<E> clone() |
2292 | { |
2293 | return new DeleteSequence<E>(fromPosition, toPosition, sequence); |
2294 | } |
2295 | } |
2296 | |
2297 | private static final class ConstantSequence<E> implements Sequence<E> |
2298 | { |
2299 | private static final long serialVersionUID = 2L; |
2300 | |
2301 | private final E constant; |
2302 | private final int length; |
2303 | |
2304 | private int hc; |
2305 | private boolean hcCalculated; |
2306 | private String ts; |
2307 | |
2308 | ConstantSequence(final E constant, final int length) |
2309 | { |
2310 | this.constant = constant; |
2311 | this.length = length; |
2312 | } |
2313 | |
2314 | public E get(final int index) throws IndexOutOfBoundsException |
2315 | { |
2316 | if(index < 0 || index >= length()) |
2317 | { |
2318 | throw new IndexOutOfBoundsException(); |
2319 | } |
2320 | else |
2321 | { |
2322 | return constant; |
2323 | } |
2324 | } |
2325 | |
2326 | public int length() |
2327 | { |
2328 | return length; |
2329 | } |
2330 | |
2331 | public Sequence<E> append(final Sequence<E> sequence) throws NullPointerException |
2332 | { |
2333 | if(sequence == null) |
2334 | { |
2335 | throw new NullPointerException(); |
2336 | } |
2337 | else if(sequence.length() == 0) |
2338 | { |
2339 | return this; |
2340 | } |
2341 | else |
2342 | { |
2343 | return new CompositeSequence<E>(this, sequence); |
2344 | } |
2345 | } |
2346 | |
2347 | public Sequence<E> getSubSequence(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
2348 | { |
2349 | if(fromPosition < 0) |
2350 | { |
2351 | throw new PositionOutOfBoundsException(fromPosition); |
2352 | } |
2353 | else if(toPosition > length()) |
2354 | { |
2355 | throw new PositionOutOfBoundsException(toPosition); |
2356 | } |
2357 | else if(fromPosition > toPosition) |
2358 | { |
2359 | throw new PositionOutOfBoundsException(fromPosition); |
2360 | } |
2361 | else |
2362 | { |
2363 | return new SubSequence<E>(this, fromPosition, toPosition); |
2364 | } |
2365 | } |
2366 | |
2367 | public Sequence<E> insert(final int position, final Sequence<E> sequence) throws PositionOutOfBoundsException, NullPointerException |
2368 | { |
2369 | if(sequence == null) |
2370 | { |
2371 | throw new NullPointerException(); |
2372 | } |
2373 | else |
2374 | { |
2375 | if(position < 0 || position > length()) |
2376 | { |
2377 | throw new PositionOutOfBoundsException(position); |
2378 | } |
2379 | else if(position == 0) |
2380 | { |
2381 | return sequence.append(this); |
2382 | } |
2383 | else if(position == length()) |
2384 | { |
2385 | return this.append(sequence); |
2386 | } |
2387 | else |
2388 | { |
2389 | return new InsertSequence<E>(position, this, sequence); |
2390 | } |
2391 | } |
2392 | } |
2393 | |
2394 | public Sequence<E> delete(final int fromPosition, final int toPosition) throws PositionOutOfBoundsException |
2395 | { |
2396 | if(fromPosition < 0) |
2397 | { |
2398 | throw new PositionOutOfBoundsException(fromPosition); |
2399 | } |
2400 | else if(toPosition > length()) |
2401 | { |
2402 | throw new PositionOutOfBoundsException(toPosition); |
2403 | } |
2404 | else if(fromPosition > toPosition) |
2405 | { |
2406 | throw new PositionOutOfBoundsException(fromPosition); |
2407 | } |
2408 | else if(fromPosition == toPosition) |
2409 | { |
2410 | return this; |
2411 | } |
2412 | else |
2413 | { |
2414 | return new DeleteSequence<E>(fromPosition, toPosition, this); |
2415 | } |
2416 | } |
2417 | |
2418 | public int indexOf(final E e) |
2419 | { |
2420 | return e == constant ? 0 : -1; |
2421 | } |
2422 | |
2423 | public int indexOf(final int position, final E e) throws PositionOutOfBoundsException |
2424 | { |
2425 | if(position < 0 || position > length()) |
2426 | { |
2427 | throw new PositionOutOfBoundsException(position); |
2428 | } |
2429 | else |
2430 | { |
2431 | if(position != length() && (constant == null ? e == null : constant.equals(e))) |
2432 | { |
2433 | return position; |
2434 | } |
2435 | else |
2436 | { |
2437 | return -1; |
2438 | } |
2439 | } |
2440 | } |
2441 | |
2442 | public int lastIndexOf(final E e) |
2443 | { |
2444 | return e == constant ? length() - 1 : -1; |
2445 | } |
2446 | |
2447 | public int lastIndexOf(final int position, final E e) throws PositionOutOfBoundsException |
2448 | { |
2449 | if(position < 0 || position > length()) |
2450 | { |
2451 | throw new PositionOutOfBoundsException(position); |
2452 | } |
2453 | else |
2454 | { |
2455 | if(position != 0 && (constant == null ? e == null : constant.equals(e))) |
2456 | { |
2457 | return position - 1; |
2458 | } |
2459 | else |
2460 | { |
2461 | return -1; |
2462 | } |
2463 | } |
2464 | } |
2465 | |
2466 | public boolean isEmpty() |
2467 | { |
2468 | return false; |
2469 | } |
2470 | |
2471 | public Iterator<E> iterator() |
2472 | { |
2473 | return getIterator(0); |
2474 | } |
2475 | |
2476 | public SequenceIterator<E> getIterator() |
2477 | { |
2478 | return getIterator(0); |
2479 | } |
2480 | |
2481 | public SequenceIterator<E> getIterator(final int position) throws PositionOutOfBoundsException |
2482 | { |
2483 | if(position < 0 || position > length()) |
2484 | { |
2485 | throw new PositionOutOfBoundsException(position); |
2486 | } |
2487 | else |
2488 | { |
2489 | return new SequenceIterator<E>() |
2490 | { |
2491 | private int pos; |
2492 | |
2493 | { |
2494 | pos = position; |
2495 | } |
2496 | |
2497 | public boolean hasNext() |
2498 | { |
2499 | return pos != length(); |
2500 | } |
2501 | |
2502 | public E next() throws NoSuchElementException |
2503 | { |
2504 | if(pos == length()) |
2505 | { |
2506 | throw new NoSuchElementException(); |
2507 | } |
2508 | else |
2509 | { |
2510 | pos++; |
2511 | return constant; |
2512 | } |
2513 | } |
2514 | |
2515 | public void remove() |
2516 | { |
2517 | throw new UnsupportedOperationException(); |
2518 | } |
2519 | |
2520 | public boolean hasPrevious() |
2521 | { |
2522 | return pos != 0; |
2523 | } |
2524 | |
2525 | public E previous() throws NoSuchElementException |
2526 | { |
2527 | if(pos == 0) |
2528 | { |
2529 | throw new NoSuchElementException(); |
2530 | } |
2531 | else |
2532 | { |
2533 | pos--; |
2534 | return constant; |
2535 | } |
2536 | } |
2537 | |
2538 | public int getPosition() |
2539 | { |
2540 | return pos; |
2541 | } |
2542 | }; |
2543 | } |
2544 | } |
2545 | |
2546 | @Override |
2547 | public String toString() |
2548 | { |
2549 | if(ts == null) |
2550 | { |
2551 | final StringBuilder sb = new StringBuilder(); |
2552 | |
2553 | sb.append("["); |
2554 | |
2555 | final SequenceIterator<E> it = getIterator(); |
2556 | |
2557 | while(it.hasNext()) |
2558 | { |
2559 | sb.append(it.next()); |
2560 | |
2561 | if(it.hasNext()) |
2562 | { |
2563 | sb.append("]["); |
2564 | } |
2565 | } |
2566 | |
2567 | sb.append("]"); |
2568 | |
2569 | ts = sb.toString(); |
2570 | } |
2571 | |
2572 | return ts; |
2573 | } |
2574 | |
2575 | @Override |
2576 | public boolean equals(final Object o) |
2577 | { |
2578 | if(this == o) |
2579 | { |
2580 | return true; |
2581 | } |
2582 | else if(o == null || (!(o instanceof Sequence))) |
2583 | { |
2584 | return false; |
2585 | } |
2586 | else |
2587 | { |
2588 | final Sequence<?> s = (Sequence<?>)o; |
2589 | |
2590 | final Object e = s.get(0); |
2591 | |
2592 | return s.length() == length() && (e == null ? constant == null : e.equals(constant)); |
2593 | } |
2594 | } |
2595 | |
2596 | @Override |
2597 | public int hashCode() |
2598 | { |
2599 | if(!hcCalculated) |
2600 | { |
2601 | final int oddPrime = 461; |
2602 | int result = 73; |
2603 | |
2604 | if(constant != null) |
2605 | { |
2606 | result = result * oddPrime + constant.hashCode(); |
2607 | } |
2608 | |
2609 | result = result * oddPrime + length(); |
2610 | |
2611 | hc = result; |
2612 | hcCalculated = true; |
2613 | } |
2614 | |
2615 | return hc; |
2616 | } |
2617 | |
2618 | public Sequence<E> clone() |
2619 | { |
2620 | return new ConstantSequence<E>(constant, length); |
2621 | } |
2622 | } |
2623 | } |