Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Enumerated.java

00001 package edu.ksu.cis.bandera.bir;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   James Corbett (corbett@hawaii.edu)     *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import ca.mcgill.sable.util.*;
00036 
00037 import java.io.*;
00038 import java.util.*;
00039 
00040 /**
00041  * An enumerated type---essentially a set of named integer constants.
00042  */
00043 
00044 public class Enumerated extends Type implements BirConstants {
00045 
00046     int minElement;
00047     int maxElement;
00048     Hashtable elementName = new Hashtable();
00049     Hashtable elementValue = new Hashtable();
00050     Vector constants = new Vector();
00051 
00052     /**
00053      * Add a new enumeration constant (value automatically assigned to
00054      * be 1 greater than existing maximum).
00055      * @param name name of constant
00056      * @return Constant definition 
00057      */
00058 
00059     public Constant add(String name) {
00060     if (elementName.size() == 0) 
00061         return add(name, 0);
00062     else 
00063         return add(name, maxElement + 1);
00064     }
00065     /**
00066      * Add a new enumeration constant with a given value.
00067      * @param name name of constant
00068      * @param value of constant
00069      * @return Constant definition 
00070      */
00071 
00072     public Constant add(String name, int value) {
00073     if (elementName.size() == 0) 
00074         minElement = maxElement = value;
00075     else if (value > maxElement)
00076         maxElement = value;
00077     else if (value < minElement)
00078         minElement = value;
00079     Integer val = new Integer(value);
00080     elementName.put(val, name);
00081     elementValue.put(name, val);
00082     Constant constant = new Constant(name,value,this);
00083     constants.addElement(constant);
00084     return constant;
00085     }
00086     public void apply(TypeSwitch sw, Object o)
00087     {
00088         sw.caseEnumerated(this, o);
00089     }
00090     public boolean compatibleWith(Type type) {
00091     return this == type;
00092     }
00093     public boolean containsValue(Object value) { 
00094     return (value instanceof Constant) &&
00095         (((Constant)value).getType() == this);
00096     }
00097     public Expr defaultVal() { 
00098     return new Constant(getNameOf(minElement),minElement,this);
00099     }
00100     public boolean equals(Object o) {
00101     if (o instanceof Enumerated) {
00102         Vector o_constants = ((Enumerated)o).constants;
00103         for (int i = 0; i < constants.size(); i++)
00104         if (! o_constants.contains(constants.elementAt(i)))
00105             return false;
00106         return o_constants.size() == constants.size();
00107     }
00108     return false;
00109     }
00110     /**
00111      * Get Vector of enumeration Constant's.
00112      * @return Vector of Constant objects for enumerated type
00113      */
00114 
00115     public Vector getConstants() { return constants; }
00116     /**
00117      * Get size of enumerated type---this is not the number of constants
00118      * but rather the range of those constants (e.g., size {0,1,4} = 5 ).
00119      * @return size of type.
00120      */
00121 
00122     public int getEnumeratedSize() { 
00123     return (elementName.size() == 0) ? 0 : (maxElement - minElement) + 1;
00124     }
00125     public int getFirstElement() { return minElement; }
00126     public String getNameOf(int value) {
00127     Object name = elementName.get(new Integer(value));
00128     if (name != null)
00129         return (String) name;
00130     else 
00131         return null;
00132     }
00133     public int getValueOf(String name) {
00134     return ((Integer)elementValue.get(name)).intValue();
00135     }
00136     public boolean isKind(int kind) { 
00137     return (kind & ENUMERATED) != 0;
00138     }
00139     // Printing
00140     public String toString() {
00141     String range = "enum {";
00142     boolean first = true;
00143     for (int i = getFirstElement(); i < getEnumeratedSize(); i++) {
00144         String name = (String) elementName.get(new Integer(i));
00145         if (name != null) {
00146         if (! first) 
00147             range += ",";
00148         else
00149             first = false;
00150         range += name + "=" + i;
00151         }
00152     }
00153     return range + "}";
00154     }
00155 }

Generated at Thu Feb 7 06:45:06 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001