1 18 19 package org.objectweb.jac.ide; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.util.List ; 26 import java.util.Vector ; 27 28 29 33 public class EnumeratedType extends Type { 34 35 public EnumeratedType() { 36 } 37 38 public String getGenerationName() { 39 return "int"; 40 } 41 42 public String getGenerationFullName() { 43 return "int"; 44 } 45 46 47 int startValue = 0; 48 public void setStartValue(int startValue) { 49 this.startValue = startValue; 50 } 51 public int getStartValue() { 52 return startValue; 53 } 54 55 56 int step = 1; 57 public int getStep() { 58 return step; 59 } 60 public void setStep(int v) { 61 this.step = v; 62 } 63 64 65 List names = new Vector (); 66 public void addName(String name) { 67 names.add(name); 68 } 69 public void removeName(String name) { 70 names.remove(name); 71 } 72 public List getNames() { 73 return names; 74 } 75 76 80 public void importFromFile(File source) throws IOException { 81 BufferedReader reader = new BufferedReader (new FileReader (source)); 82 String line; 83 while ((line=reader.readLine())!=null) { 84 line = line.trim(); 85 if (!line.equals("")) 86 addName(line); 87 } 88 } 89 90 96 public int nameToValue(String name) throws Exception { 97 int value = startValue; 98 for (int i=0; i<names.size(); i++) { 99 if (names.get(i).equals(name)) 100 return value; 101 value += step; 102 } 103 throw new Exception ("No such name \""+name+"\" in "+name); 104 } 105 106 112 public String valueToName(int value) throws Exception { 113 if ((value-startValue)%step != 0) 114 throw new Exception ("No such value "+value+" in "+name); 115 return (String )names.get((value-startValue)/step); 116 } 117 } 118 | Popular Tags |