1   /*
2    *  Copyright (c) 1998-2005, 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.6 2005/01/11 13:51:37 ian Exp $
12   *
13   */
14  package gate.util.persistence;
15  
16  import java.util.*;
17  
18  import gate.creole.ResourceInstantiationException;
19  import gate.persist.PersistenceException;
20  import gate.util.Err;
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        try{
68          result.add(PersistenceManager.
69                     getTransientRepresentation(elemIter.next()));
70        }catch(PersistenceException pe){
71          PersistenceManager.exceptionOccured = true;
72          pe.printStackTrace(Err.getPrintWriter());
73        }catch(ResourceInstantiationException rie){
74          PersistenceManager.exceptionOccured = true;
75          rie.printStackTrace(Err.getPrintWriter());
76        }
77      }
78  
79      return result;
80  
81  
82    }
83  
84  
85    protected List localList;
86    protected Class collectionType;
87    static final long serialVersionUID = 7908364068699089834L;
88  }