AliasMap.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.common.util;
00002 
00003 import java.io.File;
00004 import java.io.FileInputStream;
00005 import java.io.FileNotFoundException;
00006 import java.io.FileOutputStream;
00007 import java.io.IOException;
00008 import java.io.ObjectInputStream;
00009 import java.io.ObjectOutputStream;
00010 import java.util.HashMap;
00011 
00012 import edu.rice.cs.hpc.data.util.IUserData;
00013 
00014 
00015 /***
00016  * 
00017  * Mapping a name to another alias
00018  * This is useful when we want to change a name of a procedure X to Y (for display only) 
00019  *
00020  */
00021 public abstract class AliasMap<K,V> implements IUserData<K, V> {
00022     
00023     protected HashMap<K, V> data;
00024     
00025     
00026     /***
00027      * read map or create file if it doesn't exist
00028      */
00029     public AliasMap() {
00030 
00031     }
00032         
00033     /*
00034      * (non-Javadoc)
00035      * @see edu.rice.cs.hpc.data.util.IUserData#get(java.lang.String)
00036      */
00037     public V get(K key) {
00038         
00039         checkData();
00040         return data.get(key);
00041     }
00042 
00043     /*
00044      * (non-Javadoc)
00045      * @see edu.rice.cs.hpc.data.util.IUserData#put(java.lang.String, java.lang.String)
00046      */
00047     public void put(K key, V val) {
00048         
00049         checkData();
00050         data.put(key, val);
00051     }
00052 
00053     /***
00054      * Removes the mapping for the specified key from this map if present.
00055      * @param key
00056      * @return the value if the key exists
00057      */
00058     public V remove(K key) {
00059         checkData();
00060         V oldClass = data.remove(key);
00061         
00062         return oldClass;
00063     }
00064 
00065     /****
00066      * Removes all of the mappings from this map. The map will be empty after this call 
00067      */
00068     public void clear() {
00069         data.clear();
00070     }
00071     
00072     /***
00073      * save the changes permanently into the storage
00074      * Note: once it is stored, there is no way to restore back
00075      */
00076     public void save() {
00077         
00078         final String filename = getFilename();
00079         final File file = new File(filename);
00080         
00081         try {
00082             ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(file.getAbsoluteFile()) );
00083             out.writeObject(data);
00084             out.close();
00085         } catch (FileNotFoundException e) {
00086             // TODO Auto-generated catch block
00087             e.printStackTrace();
00088         } catch (IOException e) {
00089             // TODO Auto-generated catch block
00090             e.printStackTrace();
00091         }
00092     }   
00093     
00094     /***
00095      * check if we have loaded the data or not. If not, we need to read the file,
00096      * and initialize the data. If the file doesn't exist, it will be created and
00097      * initialized by the default data (implemented by the children)
00098      */
00099     protected void checkData() {
00100         if (data == null) {
00101             final String filename = getFilename();
00102             File file = new File( filename );
00103             
00104             if (file.canRead()) {
00105                 if (! readData(file.getAbsolutePath()) ) {
00106                     // old format, we need to remove the file
00107                     if ( file.delete() ) {
00108                         // initialize the data
00109                         initDefault();
00110                         readData(file.getAbsolutePath());
00111                     }
00112                 }
00113             } else if (!file.exists()) {
00114                 // file doesn't exist, but we can create
00115                 data = new HashMap<K, V>();
00116                 
00117                 // init data
00118                 initDefault();
00119                 
00120                 save();
00121             }
00122         }
00123     }
00124 
00125     /***
00126      * read data from a given file
00127      * 
00128      * @param filename
00129      * @throws FileNotFoundException
00130      * @throws IOException
00131      * @throws ClassNotFoundException
00132      */
00133     private boolean readData(String filename) {
00134         boolean result = false;
00135         ObjectInputStream in;
00136         try {
00137             in = new ObjectInputStream(new FileInputStream(filename));
00138             Object o = in.readObject();
00139             if (o instanceof HashMap<?,?>) {
00140                 data = (HashMap<K, V>) o;
00141                 result =  true;
00142             }
00143         } catch (FileNotFoundException e) {
00144             e.printStackTrace();
00145         } catch (IOException e) {
00146             e.printStackTrace();
00147         } catch (ClassNotFoundException e) {
00148             e.printStackTrace();
00149         }
00150         return result;
00151     }
00152     
00153     abstract public String getFilename();
00154     abstract public void initDefault();
00155 
00156 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1