ProcedureManager.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.data.util;
00002
00003 import java.io.FileInputStream;
00004 import java.io.FileOutputStream;
00005 import java.io.IOException;
00006 import java.io.InputStream;
00007 import java.io.ObjectInputStream;
00008 import java.io.ObjectOutputStream;
00009 import java.util.HashMap;
00010
00011
00012
00013
00014
00015
00016 public class ProcedureManager<ValueType> {
00017
00018
00019 protected HashMap<String, ValueType> map;
00020
00021
00022
00023
00024
00025
00026 public ProcedureManager(Class<?> location, final String file) throws IOException, ClassNotFoundException {
00027 map = new HashMap<String, ValueType>();
00028
00029 final InputStream is = location.getResourceAsStream(file);
00030 if (is != null) {
00031 addToMap(is);
00032 is.close();
00033 }
00034 }
00035
00041 public void open(String filename) throws IOException, ClassNotFoundException {
00042 FileInputStream fis = new FileInputStream(filename);
00043 addToMap(fis);
00044 fis.close();
00045 }
00046
00047 public void save(String filename) throws IOException {
00048 final FileOutputStream fos = new FileOutputStream(filename);
00049 final ObjectOutputStream oos = new ObjectOutputStream( fos );
00050 oos.writeObject(map);
00051 oos.close();
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061 private void addToMap(InputStream is) throws IOException, ClassNotFoundException {
00062 final ObjectInputStream in = new ObjectInputStream(is);
00063 Object o = in.readObject();
00064
00065 if (o instanceof HashMap<?,?>) {
00066 @SuppressWarnings("unchecked")
00067 final HashMap<String, ValueType> mapin = (HashMap<String, ValueType>) o;
00068 map.putAll(mapin);
00069 } else {
00070
00071 throw new RuntimeException("Incorrect file format " );
00072 }
00073 in.close();
00074 }
00075
00076
00077
00078
00079
00080
00081 protected ValueType get(String proc) {
00082 return this.map.get(proc);
00083 }
00084
00085
00086
00087
00088
00089
00090 protected void put(String proc, ValueType val) {
00091 map.put(proc, val);
00092 }
00093
00094 }