37
37
* @author Igor Polevoy
38
38
* @author Eric Nielsen
39
39
*/
40
- public class LazyList <T extends Model > extends AbstractList <T >{
40
+ public class LazyList <T extends Model > extends UnmodifiableLazyList <T > {
41
41
42
42
private static final Logger logger = LoggerFactory .getLogger (LazyList .class );
43
- protected List <T > delegate = new ArrayList <T >();
44
43
private final List <String > orderBys = new ArrayList <String >();
45
44
private boolean hydrated = false ;
46
45
private final MetaModel metaModel ;
@@ -51,7 +50,7 @@ public class LazyList<T extends Model> extends AbstractList<T>{
51
50
private final Map <Class <T >, Association > includes = new HashMap <Class <T >, Association >();
52
51
private final boolean forPaginator ;
53
52
54
- protected LazyList (String subQuery , MetaModel metaModel , Object ... params ){
53
+ protected LazyList (String subQuery , MetaModel metaModel , Object ... params ) {
55
54
this .fullQuery = null ;
56
55
this .subQuery = subQuery ;
57
56
this .params = params == null ? new Object []{}: params ;
@@ -76,6 +75,7 @@ protected LazyList(boolean forPaginator, MetaModel metaModel, String fullQuery,
76
75
77
76
//TODO: this is only used by SuperLazyList, to be reviewed?
78
77
protected LazyList () {
78
+ delegate = new ArrayList <T >();
79
79
this .fullQuery = null ;
80
80
this .subQuery = null ;
81
81
this .params = null ;
@@ -317,7 +317,8 @@ public String toSql(boolean showParameters) {
317
317
}
318
318
319
319
320
- protected void hydrate (){
320
+ @ Override
321
+ protected void hydrate () {
321
322
322
323
if (hydrated ) return ;
323
324
@@ -330,7 +331,7 @@ protected void hydrate(){
330
331
return ;
331
332
}
332
333
}
333
-
334
+ delegate = new ArrayList < T >();
334
335
long start = System .currentTimeMillis ();
335
336
new DB (metaModel .getDbName ()).find (sql , params ).with (new RowListenerAdapter () {
336
337
@ Override public void onNext (Map <String , Object > map ) {
@@ -339,6 +340,7 @@ protected void hydrate(){
339
340
});
340
341
LogFilter .logQuery (logger , sql , params , start );
341
342
if (metaModel .cached ()){
343
+ //TODO: review, LazyList is already unmodifiable, and this will be delegated twice when fetched from cache
342
344
delegate = Collections .unmodifiableList (delegate );
343
345
QueryCache .instance ().addItem (metaModel .getTableName (), sql , params , delegate );
344
346
}
@@ -538,150 +540,6 @@ private void processOther(Many2ManyAssociation association, Class<? extends Mode
538
540
}
539
541
}
540
542
541
- @ Override
542
- public T get (int index ) {
543
- hydrate ();
544
- return delegate .get (index );
545
- }
546
-
547
- @ Override
548
- public int size () {
549
- hydrate ();
550
- return delegate .size ();
551
- }
552
-
553
- @ Override
554
- public boolean isEmpty () {
555
- hydrate ();
556
- return delegate .isEmpty ();
557
- }
558
-
559
- @ Override
560
- public boolean contains (Object o ) {
561
- hydrate ();
562
- return delegate .contains (o );
563
- }
564
-
565
- @ Override
566
- public Iterator <T > iterator () {
567
- hydrate ();
568
- return delegate .iterator ();
569
- }
570
-
571
- @ Override
572
- public Object [] toArray () {
573
- hydrate ();
574
- return delegate .toArray ();
575
- }
576
-
577
- @ Override
578
- public <T > T [] toArray (T [] a ) {
579
- hydrate ();
580
- return delegate .toArray (a );
581
- }
582
-
583
- @ Override
584
- public boolean add (T o ) {
585
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
586
- }
587
-
588
- @ Override
589
- public boolean remove (Object o ) {
590
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
591
- }
592
-
593
- @ Override
594
- public boolean containsAll (Collection c ) {
595
- hydrate ();
596
- return delegate .containsAll (c );
597
- }
598
-
599
- @ Override
600
- public boolean addAll (Collection c ) {
601
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
602
- }
603
-
604
- @ Override
605
- public boolean addAll (int index , Collection c ) {
606
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
607
- }
608
-
609
- @ Override
610
- public boolean removeAll (Collection c ) {
611
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
612
- }
613
-
614
- @ Override
615
- public boolean retainAll (Collection c ) {
616
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
617
- }
618
-
619
- @ Override
620
- public void clear () {
621
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
622
- }
623
-
624
- @ Override
625
- public T set (int index , T element ) {
626
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
627
- }
628
-
629
- @ Override
630
- public void add (int index , T element ) {
631
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
632
- }
633
-
634
- @ Override
635
- public T remove (int index ) {
636
- throw new UnsupportedOperationException ("this operation is not supported, cannot manipulate DB results" );
637
- }
638
-
639
- @ Override
640
- public int indexOf (Object o ) {
641
- hydrate ();
642
- return delegate .indexOf (o );
643
- }
644
-
645
- @ Override
646
- public int lastIndexOf (Object o ) {
647
- hydrate ();
648
- return delegate .lastIndexOf (o );
649
- }
650
-
651
- @ Override
652
- public ListIterator <T > listIterator () {
653
- hydrate ();
654
- return delegate .listIterator ();
655
- }
656
-
657
- @ Override
658
- public ListIterator <T > listIterator (int index ) {
659
- hydrate ();
660
- return delegate .listIterator (index );
661
- }
662
-
663
- @ Override
664
- public List <T > subList (int fromIndex , int toIndex ) {
665
- hydrate ();
666
- return delegate .subList (fromIndex , toIndex );
667
- }
668
-
669
- /**
670
- * This is only to test caching.
671
- * @return
672
- */
673
- @ Override
674
- public int hashCode () {
675
- hydrate ();
676
- return delegate .hashCode ();
677
- }
678
-
679
- @ Override
680
- public String toString () {
681
- hydrate ();
682
- return delegate .toString ();
683
- }
684
-
685
543
/**
686
544
* Dumps contents of this list to <code>System.out</code>.
687
545
*/
0 commit comments