1 28 29 package com.caucho.ejb.entity; 30 31 import com.caucho.amber.AmberQuery; 32 import com.caucho.amber.manager.AmberConnection; 33 34 import java.util.AbstractList ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 38 41 abstract public class CmpCollectionImpl extends AbstractList { 42 private ArrayList _base = new ArrayList (); 43 private AmberQuery _query; 44 45 public void __caucho_init(AmberConnection aConn) 46 { 47 _query.init(aConn); 48 } 49 50 protected void fill(AmberQuery query) 51 { 52 try { 53 _query = query; 54 55 _base.clear(); 56 query.list(_base); 57 } catch (RuntimeException e) { 58 throw e; 59 } catch (Exception e) { 60 throw new RuntimeException (e); 61 } 62 } 63 64 public int size() 65 { 66 return _base.size(); 67 } 68 69 public Object get(int index) 70 { 71 return _base.get(index); 72 } 73 74 public boolean add(Object value) 75 { 76 if (addImpl(value) && ! _base.contains(value)) { 77 _base.add(value); 78 return true; 79 } 80 else 81 return false; 82 } 83 84 protected boolean addImpl(Object value) 85 { 86 throw new UnsupportedOperationException (); 87 } 88 89 public Object remove(int index) 90 { 91 Object oldValue = _base.remove(index); 92 93 if (removeImpl(oldValue)) 94 return oldValue; 95 else 96 return null; 97 } 98 99 public boolean remove(Object value) 100 { 101 return removeImpl(value); 102 } 103 104 protected boolean removeImpl(Object oldValue) 105 { 106 throw new UnsupportedOperationException (); 107 } 108 109 public Iterator iterator() 110 { 111 return new Itr(); 112 } 113 114 117 public class Itr implements Iterator { 118 int _index; 119 int _size; 120 121 Itr() 122 { 123 _size = size(); 124 } 125 126 129 public boolean hasNext() 130 { 131 return _index < _size; 132 } 133 134 137 public Object next() 138 { 139 if (_index < _size) 140 return get(_index++); 141 else 142 return null; 143 } 144 145 148 public void remove() 149 { 150 _index--; 151 _size--; 152 153 CmpCollectionImpl.this.remove(_index); 154 } 155 } 156 } 157 | Popular Tags |