Skip to content

Commit 437993e

Browse files
committed
#362 Corrections for a completely unmodifiable LazyList
Plus more generics updates
1 parent 4109d52 commit 437993e

File tree

2 files changed

+202
-149
lines changed

2 files changed

+202
-149
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/LazyList.java

Lines changed: 7 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@
3737
* @author Igor Polevoy
3838
* @author Eric Nielsen
3939
*/
40-
public class LazyList<T extends Model> extends AbstractList<T>{
40+
public class LazyList<T extends Model> extends UnmodifiableLazyList<T> {
4141

4242
private static final Logger logger = LoggerFactory.getLogger(LazyList.class);
43-
protected List<T> delegate = new ArrayList<T>();
4443
private final List<String> orderBys = new ArrayList<String>();
4544
private boolean hydrated = false;
4645
private final MetaModel metaModel;
@@ -51,7 +50,7 @@ public class LazyList<T extends Model> extends AbstractList<T>{
5150
private final Map<Class<T>, Association> includes = new HashMap<Class<T>, Association>();
5251
private final boolean forPaginator;
5352

54-
protected LazyList(String subQuery, MetaModel metaModel, Object ... params){
53+
protected LazyList(String subQuery, MetaModel metaModel, Object... params) {
5554
this.fullQuery = null;
5655
this.subQuery = subQuery;
5756
this.params = params == null? new Object[]{}: params;
@@ -76,6 +75,7 @@ protected LazyList(boolean forPaginator, MetaModel metaModel, String fullQuery,
7675

7776
//TODO: this is only used by SuperLazyList, to be reviewed?
7877
protected LazyList() {
78+
delegate = new ArrayList<T>();
7979
this.fullQuery = null;
8080
this.subQuery = null;
8181
this.params = null;
@@ -317,7 +317,8 @@ public String toSql(boolean showParameters) {
317317
}
318318

319319

320-
protected void hydrate(){
320+
@Override
321+
protected void hydrate() {
321322

322323
if(hydrated) return;
323324

@@ -330,7 +331,7 @@ protected void hydrate(){
330331
return;
331332
}
332333
}
333-
334+
delegate = new ArrayList<T>();
334335
long start = System.currentTimeMillis();
335336
new DB(metaModel.getDbName()).find(sql, params).with(new RowListenerAdapter() {
336337
@Override public void onNext(Map<String, Object> map) {
@@ -339,6 +340,7 @@ protected void hydrate(){
339340
});
340341
LogFilter.logQuery(logger, sql, params, start);
341342
if(metaModel.cached()){
343+
//TODO: review, LazyList is already unmodifiable, and this will be delegated twice when fetched from cache
342344
delegate = Collections.unmodifiableList(delegate);
343345
QueryCache.instance().addItem(metaModel.getTableName(), sql, params, delegate);
344346
}
@@ -538,150 +540,6 @@ private void processOther(Many2ManyAssociation association, Class<? extends Mode
538540
}
539541
}
540542

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-
685543
/**
686544
* Dumps contents of this list to <code>System.out</code>.
687545
*/

0 commit comments

Comments
 (0)