|
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.3 2002/05/13 11:35:32 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 import gate.util.*; 22 23 24 public class CollectionPersistence implements Persistence { 25 26 /** 27 * Populates this Persistence with the data that needs to be stored from the 28 * original source object. 29 */ 30 public void extractDataFromSource(Object source)throws PersistenceException{ 31 if(! (source instanceof Collection)){ 32 throw new UnsupportedOperationException( 33 getClass().getName() + " can only be used for " + 34 Collection.class.getName() + 35 " objects!\n" + source.getClass().getName() + 36 " is not a " + Collection.class.getName()); 37 } 38 collectionType = source.getClass(); 39 40 Collection coll = (Collection)source; 41 42 //get the values in the iterator's order 43 localList = new ArrayList(coll.size()); 44 Iterator elemIter = coll.iterator(); 45 while(elemIter.hasNext()){ 46 localList.add(PersistenceManager. 47 getPersistentRepresentation(elemIter.next())); 48 } 49 } 50 51 /** 52 * Creates a new object from the data contained. This new object is supposed 53 * to be a copy for the original object used as source for data extraction. 54 */ 55 public Object createObject()throws PersistenceException, 56 ResourceInstantiationException{ 57 //let's try to create a collection of the same type as the original 58 Collection result = null; 59 try{ 60 result = (Collection)collectionType.newInstance(); 61 }catch(Exception e){ 62 } 63 if(result == null) result = new ArrayList(localList.size()); 64 65 //now we have the collection let's populate it 66 Iterator elemIter = localList.iterator(); 67 while(elemIter.hasNext()){ 68 try{ 69 result.add(PersistenceManager. 70 getTransientRepresentation(elemIter.next())); 71 }catch(PersistenceException pe){ 72 PersistenceManager.exceptionOccured = true; 73 pe.printStackTrace(Err.getPrintWriter()); 74 }catch(ResourceInstantiationException rie){ 75 PersistenceManager.exceptionOccured = true; 76 rie.printStackTrace(Err.getPrintWriter()); 77 } 78 } 79 80 return result; 81 82 83 } 84 85 86 protected List localList; 87 protected Class collectionType; 88 static final long serialVersionUID = 7908364068699089834L; 89 }
|
CollectionPersistence |
|