1 package org.hibernate.collection; 3 4 import java.io.Serializable ; 5 import java.lang.reflect.Array ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.hibernate.EntityMode; 15 import org.hibernate.HibernateException; 16 import org.hibernate.loader.CollectionAliases; 17 import org.hibernate.engine.SessionImplementor; 18 import org.hibernate.persister.collection.CollectionPersister; 19 import org.hibernate.type.Type; 20 21 28 public class PersistentArrayHolder extends AbstractPersistentCollection { 29 protected Object array; 30 31 private static final Log log = LogFactory.getLog(PersistentArrayHolder.class); 32 33 private transient Class elementClass; 35 private transient java.util.List tempList; 36 37 public PersistentArrayHolder(SessionImplementor session, Object array) { 38 super(session); 39 this.array = array; 40 setInitialized(); 41 } 42 43 public Serializable getSnapshot(CollectionPersister persister) throws HibernateException { 44 EntityMode entityMode = getSession().getEntityMode(); 45 int length = Array.getLength(array); 46 Serializable result = (Serializable ) Array.newInstance( persister.getElementClass(), length ); 47 for ( int i=0; i<length; i++ ) { 48 Object elt = Array.get(array, i); 49 try { 50 Array.set( result, i, persister.getElementType().deepCopy(elt, entityMode, persister.getFactory()) ); 51 } 52 catch (IllegalArgumentException iae) { 53 log.error("Array element type error", iae); 54 throw new HibernateException( "Array element type error", iae ); 55 } 56 } 57 return result; 58 } 59 60 public boolean isSnapshotEmpty(Serializable snapshot) { 61 return Array.getLength( snapshot ) == 0; 62 } 63 64 public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { 65 Object [] sn = (Object []) snapshot; 66 Object [] arr = (Object []) array; 67 ArrayList result = new ArrayList (); 68 for (int i=0; i<sn.length; i++) result.add( sn[i] ); 69 for (int i=0; i<sn.length; i++) identityRemove( result, arr[i], entityName, getSession() ); 70 return result; 71 } 72 73 public PersistentArrayHolder(SessionImplementor session, CollectionPersister persister) throws HibernateException { 74 super(session); 75 elementClass = persister.getElementClass(); 76 } 77 78 public Object getArray() { 79 return array; 80 } 81 82 public boolean isWrapper(Object collection) { 83 return array==collection; 84 } 85 86 public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { 87 Type elementType = persister.getElementType(); 88 Serializable snapshot = getSnapshot(); 89 int xlen = Array.getLength(snapshot); 90 if ( xlen!= Array.getLength(array) ) return false; 91 for ( int i=0; i<xlen; i++) { 92 if ( elementType.isDirty( Array.get(snapshot, i), Array.get(array, i), getSession() ) ) return false; 93 } 94 return true; 95 } 96 97 public Iterator elements() { 98 int length = Array.getLength(array); 100 java.util.List list = new ArrayList (length); 101 for (int i=0; i<length; i++) { 102 list.add( Array.get(array, i) ); 103 } 104 return list.iterator(); 105 } 106 public boolean empty() { 107 return false; 108 } 109 110 public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner) 111 throws HibernateException, SQLException { 112 113 Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ); 114 int index = ( (Integer ) persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ) ).intValue(); 115 for ( int i = tempList.size(); i<=index; i++) { 116 tempList.add(i, null); 117 } 118 tempList.set(index, element); 119 return element; 120 } 121 122 public Iterator entries(CollectionPersister persister) { 123 return elements(); 124 } 125 126 public void beginRead() { 127 super.beginRead(); 128 tempList = new ArrayList (); 129 } 130 public boolean endRead() { 131 setInitialized(); 132 array = Array.newInstance( elementClass, tempList.size() ); 133 for ( int i=0; i<tempList.size(); i++) { 134 Array.set(array, i, tempList.get(i) ); 135 } 136 tempList=null; 137 return true; 138 } 139 140 public void beforeInitialize(CollectionPersister persister) { 141 } 143 144 public boolean isDirectlyAccessible() { 145 return true; 146 } 147 148 public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) 149 throws HibernateException { 150 Serializable [] cached = (Serializable []) disassembled; 151 152 array = Array.newInstance( persister.getElementClass(), cached.length ); 153 154 for ( int i=0; i<cached.length; i++ ) { 155 Array.set( array, i, persister.getElementType().assemble( cached[i], getSession(), owner ) ); 156 } 157 } 158 159 public Serializable disassemble(CollectionPersister persister) throws HibernateException { 160 int length = Array.getLength(array); 161 Serializable [] result = new Serializable [length]; 162 for ( int i=0; i<length; i++ ) { 163 result[i] = persister.getElementType().disassemble( Array.get(array,i), getSession(), null ); 164 } 165 166 171 172 return result; 173 174 } 175 176 public Object getValue() { 177 return array; 178 } 179 180 public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { 181 java.util.List deletes = new ArrayList (); 182 Serializable sn = getSnapshot(); 183 int snSize = Array.getLength(sn); 184 int arraySize = Array.getLength(array); 185 int end; 186 if ( snSize > arraySize ) { 187 for ( int i=arraySize; i<snSize; i++ ) deletes.add( new Integer (i) ); 188 end = arraySize; 189 } 190 else { 191 end = snSize; 192 } 193 for ( int i=0; i<end; i++ ) { 194 if ( Array.get(array, i)==null && Array.get(sn, i)!=null ) deletes.add( new Integer (i) ); 195 } 196 return deletes.iterator(); 197 } 198 199 public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { 200 Serializable sn = getSnapshot(); 201 return Array.get(array, i)!=null && ( i >= Array.getLength(sn) || Array.get(sn, i)==null ); 202 } 203 204 public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException { 205 Serializable sn = getSnapshot(); 206 return i<Array.getLength(sn) && 207 Array.get(sn, i)!=null && 208 Array.get(array, i)!=null && 209 elemType.isDirty( Array.get(array, i), Array.get(sn, i), getSession() ); 210 } 211 212 public Object getIndex(Object entry, int i, CollectionPersister persister) { 213 return new Integer (i); 214 } 215 216 public Object getElement(Object entry) { 217 return entry; 218 } 219 220 public Object getSnapshotElement(Object entry, int i) { 221 Serializable sn = getSnapshot(); 222 return Array.get(sn, i); 223 } 224 225 public boolean entryExists(Object entry, int i) { 226 return entry!=null; 227 } 228 229 } 230 | Popular Tags |