|
CollectionPersistence |
|
1 /* 2 * Copyright (c) 1998-2001, The University of Sheffield. 3 * 4 * This file is part of GATE (see http://gate.ac.uk/), and is free 5 * software, licenced under the GNU Library General Public License, 6 * Version 2, June 1991 (in the distribution as file licence.html, 7 * and also available at http://gate.ac.uk/gate/licence.html). 8 * 9 * Valentin Tablan 26/10/2001 10 * 11 * $Id: CollectionPersistence.java,v 1.2 2001/10/29 17:11:26 valyt Exp $ 12 * 13 */ 14 package gate.util.persistence; 15 16 import java.util.*; 17 import java.io.*; 18 19 import gate.persist.PersistenceException; 20 import gate.creole.ResourceInstantiationException; 21 22 23 public class CollectionPersistence implements Persistence { 24 25 /** 26 * Populates this Persistence with the data that needs to be stored from the 27 * original source object. 28 */ 29 public void extractDataFromSource(Object source)throws PersistenceException{ 30 if(! (source instanceof Collection)){ 31 throw new UnsupportedOperationException( 32 getClass().getName() + " can only be used for " + 33 Collection.class.getName() + 34 " objects!\n" + source.getClass().getName() + 35 " is not a " + Collection.class.getName()); 36 } 37 collectionType = source.getClass(); 38 39 Collection coll = (Collection)source; 40 41 //get the values in the iterator's order 42 localList = new ArrayList(coll.size()); 43 Iterator elemIter = coll.iterator(); 44 while(elemIter.hasNext()){ 45 localList.add(PersistenceManager. 46 getPersistentRepresentation(elemIter.next())); 47 } 48 } 49 50 /** 51 * Creates a new object from the data contained. This new object is supposed 52 * to be a copy for the original object used as source for data extraction. 53 */ 54 public Object createObject()throws PersistenceException, 55 ResourceInstantiationException{ 56 //let's try to create a collection of the same type as the original 57 Collection result = null; 58 try{ 59 result = (Collection)collectionType.newInstance(); 60 }catch(Exception e){ 61 } 62 if(result == null) result = new ArrayList(localList.size()); 63 64 //now we have the collection let's populate it 65 Iterator elemIter = localList.iterator(); 66 while(elemIter.hasNext()){ 67 result.add(PersistenceManager. 68 getTransientRepresentation(elemIter.next())); 69 } 70 71 return result; 72 73 74 } 75 76 77 protected List localList; 78 protected Class collectionType; 79 static final long serialVersionUID = 7908364068699089834L; 80 }
|
CollectionPersistence |
|