001    /*
002      Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.ide;
020    
021    import java.io.BufferedReader;
022    import java.io.File;
023    import java.io.FileReader;
024    import java.io.IOException;
025    import java.util.List;
026    import java.util.Vector;
027    
028    
029    /**
030     * This class represents an enumerated type. Enumrated types allow to
031     * define a set of allowed values for an integer.
032     */
033    public class EnumeratedType extends Type {
034    
035       public EnumeratedType() {
036       }
037    
038       public String getGenerationName() {
039          return "int";
040       }
041    
042       public String getGenerationFullName() {
043          return "int";
044       }
045    
046       /** first integer value */
047       int startValue = 0;
048       public void setStartValue(int startValue) {
049          this.startValue = startValue;
050       }
051       public int getStartValue() {
052          return startValue;
053       }
054    
055       /** step separating each value */
056       int step = 1;
057       public int getStep() {
058          return step;
059       }
060       public void setStep(int  v) {
061          this.step = v;
062       }
063    
064       /** Name of values */
065       List names = new Vector();
066       public void addName(String name) {
067          names.add(name);
068       }
069       public void removeName(String name) {
070          names.remove(name);
071       }
072       public List getNames() {
073          return names;
074       }
075    
076       /**
077        * Imports names from a file (one name per line)
078        * @param source file to import from
079        */
080       public void importFromFile(File source) throws IOException {
081          BufferedReader reader = new BufferedReader(new FileReader(source));
082          String line;
083          while ((line=reader.readLine())!=null) {
084             line = line.trim();
085             if (!line.equals(""))
086                addName(line);
087          }
088       }
089    
090       /**
091        * Gets the value associated to a name. Raises an exception if the
092        * name does not exists.
093        * @param name the name
094        * @return the integer value associated with the name
095        */
096       public int nameToValue(String name) throws Exception {
097          int value = startValue;
098          for (int i=0; i<names.size(); i++) {
099             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       /**
107        * Gets the name associated with a value. Raises an exception if the
108        * there's no name with such a value.
109        * @param value the value
110        * @return the name value associated with the value
111        */
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    }