KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > EnumeratedType


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Vector JavaDoc;
27
28
29 /**
30  * This class represents an enumerated type. Enumrated types allow to
31  * define a set of allowed values for an integer.
32  */

33 public class EnumeratedType extends Type {
34
35    public EnumeratedType() {
36    }
37
38    public String JavaDoc getGenerationName() {
39       return "int";
40    }
41
42    public String JavaDoc getGenerationFullName() {
43       return "int";
44    }
45
46    /** first integer value */
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    /** step separating each value */
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    /** Name of values */
65    List JavaDoc names = new Vector JavaDoc();
66    public void addName(String JavaDoc name) {
67       names.add(name);
68    }
69    public void removeName(String JavaDoc name) {
70       names.remove(name);
71    }
72    public List JavaDoc getNames() {
73       return names;
74    }
75
76    /**
77     * Imports names from a file (one name per line)
78     * @param source file to import from
79     */

80    public void importFromFile(File JavaDoc source) throws IOException JavaDoc {
81       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(source));
82       String JavaDoc line;
83       while ((line=reader.readLine())!=null) {
84          line = line.trim();
85          if (!line.equals(""))
86             addName(line);
87       }
88    }
89
90    /**
91     * Gets the value associated to a name. Raises an exception if the
92     * name does not exists.
93     * @param name the name
94     * @return the integer value associated with the name
95     */

96    public int nameToValue(String JavaDoc name) throws Exception JavaDoc {
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 JavaDoc("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 JavaDoc valueToName(int value) throws Exception JavaDoc {
113       if ((value-startValue)%step != 0)
114          throw new Exception JavaDoc("No such value "+value+" in "+name);
115       return (String JavaDoc)names.get((value-startValue)/step);
116    }
117 }
118
Popular Tags