Storage.java |
1 package gate.creole.morph; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 6 /** 7 * <p>Title: Storage.java </p> 8 * <p>Description: This class is used as the storage in the system, where 9 * all the declared variables and their appropriate values are stored </p> 10 */ 11 public class Storage { 12 13 /** 14 * Stores variable name as the key, and its variable values as values of these 15 * keys 16 */ 17 private HashMap variables; 18 19 /** 20 * Constructor 21 */ 22 public Storage() { 23 variables = new HashMap(); 24 } 25 26 /** 27 * Adds the variable name and its value into the hashTable 28 * @param varName name of the variable 29 * @param varValue value for the variable 30 * @return true if variable doesnot exist, false otherwise 31 */ 32 public boolean add(String varName, String varValue) { 33 if(variables.containsKey(varName)) { 34 return false; 35 } else { 36 37 // before storing varValue try to find if it is 38 // a Character Range 39 // a Character Set 40 // a Sting Set 41 42 variables.put(varName,varValue); 43 return true; 44 } 45 } 46 47 /** 48 * This method looks into the hashtable and searches for the value of the 49 * given variable 50 * @param varName 51 * @return value of the variable if variable found in the table,null otherwise 52 */ 53 public String get(String varName) { 54 String varValue = (String)(variables.get(varName)); 55 return varValue; 56 } 57 58 /** 59 * This method checks for the existance of the variable into the hashtable 60 * @param varName 61 * @return true if variable exists, false otherwise 62 */ 63 public boolean isExist(String varName) { 64 if(variables.containsKey(varName)) { 65 return true; 66 } else { 67 return false; 68 } 69 } 70 71 /** 72 * Update the variable with the new value. If variable doesnot exist, add it 73 * to the hashtable 74 * @param varName name of the variable to be updated, or added 75 * @param varValue value of the variable 76 */ 77 public void update(String varName,String varValue) { 78 variables.put(varName,varValue); 79 80 } 81 82 /** 83 * This method returns names of all the variables available in the hashtable 84 * @return array of Strings - names of the variables 85 */ 86 public String [] getVarNames() { 87 Iterator iter = variables.keySet().iterator(); 88 String [] varNames = new String[variables.size()]; 89 int i=0; 90 while(iter.hasNext()) { 91 varNames[i] = (String)(iter.next()); 92 i++; 93 } 94 return varNames; 95 } 96 }