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: MapPersistence.java,v 1.5 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  
21  public class MapPersistence implements Persistence {
22    /**
23     * Populates this Persistence with the data that needs to be stored from the
24     * original source object.
25     */
26    public void extractDataFromSource(Object source)throws PersistenceException{
27      if(! (source instanceof Map)){
28        throw new UnsupportedOperationException(
29                  getClass().getName() + " can only be used for " +
30                  Map.class.getName() +
31                  " objects!\n" + source.getClass().getName() +
32                  " is not a " + Map.class.getName());
33      }
34      mapType = source.getClass();
35  
36      Map map = (Map)source;
37      List keysList = new ArrayList();
38      List valuesList = new ArrayList();
39      localMap = new HashMap(map.size());
40      //collect the keys in the order given by the entrySet().iterator();
41      Iterator keyIter = map.keySet().iterator();
42      while(keyIter.hasNext()){
43        Object key = keyIter.next();
44        Object value = map.get(key);
45  
46        key = PersistenceManager.getPersistentRepresentation(key);
47        value = PersistenceManager.getPersistentRepresentation(value);
48        localMap.put(key, value);
49      }
50    }
51  
52    /**
53     * Creates a new object from the data contained. This new object is supposed
54     * to be a copy for the original object used as source for data extraction.
55     */
56    public Object createObject()throws PersistenceException,
57                                       ResourceInstantiationException{
58      //let's try to create a map of the same type as the original
59      Map result = null;
60      try{
61        result = (Map)mapType.newInstance();
62      }catch(Exception e){
63      }
64      if(result == null) result = new HashMap(localMap.size());
65  
66      //now we have a map let's populate it
67      Iterator keyIter = localMap.keySet().iterator();
68      while(keyIter.hasNext()){
69        Object key = keyIter.next();
70        Object value = localMap.get(key);
71  
72        key = PersistenceManager.getTransientRepresentation(key);
73        value = PersistenceManager.getTransientRepresentation(value);
74        result.put(key, value);
75      }
76  
77      return result;
78    }
79  
80    protected Class mapType;
81    protected HashMap localMap;
82    static final long serialVersionUID = 1835776085941379996L;
83  }