1 17 18 package org.objectweb.jac.util; 19 20 import java.util.Arrays ; 21 import java.util.List ; 22 23 public class Enum { 24 String [] values; 25 int start = 0; 26 int step = 1; 27 public Enum(String [] values, int start, int step) { 28 this.values = values; 29 this.start = start; 30 this.step = step; 31 } 32 33 38 public String int2string(int index) { 39 if (((float)(index-start)/((float)step))!=(float)((index-start)/step)) 40 throw new InvalidIndexException(index); 41 String value; 42 try { 43 value=values[(index-start)/step]; 44 } catch(Exception e) { 45 throw new InvalidIndexException(index); 46 } 47 return value; 48 } 49 53 public int string2int(String string) { 54 if (string==null) 55 throw new RuntimeException ("Invalid string value null for enum"); 56 int index = start; 57 for(int i=0; i<values.length; i++) { 58 if (string.equals(values[i])) { 59 return index; 60 } else { 61 index += step; 62 } 63 } 64 throw new RuntimeException ("Invalid string value '"+string+"' for enum"); 65 } 66 public List getValues() { 67 return Arrays.asList(values); 68 } 69 public int getStart() { 70 return start; 71 } 72 public int getStep() { 73 return step; 74 } 75 } 76 | Popular Tags |