Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans;
   17   
   18   import java.util.HashMap;
   19   import java.util.Map;
   20   import java.util.List;
   21   import java.util.ArrayList;
   22   
   23   /**
   24    * The base class for code-generated string enumeration value classes.
   25    * <p>
   26    * Subclasses are intended to be final types with a finite set of
   27    * singleton instances.  Each instance has a string value, which
   28    * it returns via {@link #toString}, and an int value for the purpose
   29    * of switching in case statements, returned via {@link #intValue}.
   30    * <p>
   31    * Each subclass manages an instance of {@link StringEnumAbstractBase.Table},
   32    * which holds all the singleton instances for the subclass. A Table
   33    * can return a singleton instance given a String or an integer code.
   34    */ 
   35   public class StringEnumAbstractBase implements java.io.Serializable
   36   {
   37       private static final long serialVersionUID = 1L;
   38       
   39       private String _string;
   40       private int _int;
   41   
   42       /**
   43        * Singleton instances should only be created by subclasses.
   44        */ 
   45       protected StringEnumAbstractBase(String s, int i)
   46           { _string = s; _int = i; }
   47   
   48       /** Returns the underlying string value */
   49       public final String toString()
   50           { return _string; }
   51       /** Returns an int code that can be used for switch statements */
   52       public final int intValue()
   53           { return _int; }
   54       /** Returns the hash code of the underlying string */
   55       public final int hashCode()
   56           { return _string.hashCode(); }
   57   
   58       /**
   59        * Used to manage singleton instances of enumerations.
   60        * Each subclass of StringEnumAbstractBase has an instance
   61        * of a table to hold the singleton instances.
   62        */ 
   63       public static final class Table
   64       {
   65           private Map _map;
   66           private List _list;
   67           public Table(StringEnumAbstractBase[] array)
   68           {
   69               _map = new HashMap(array.length);
   70               _list = new ArrayList(array.length + 1);
   71               for (int i = 0; i < array.length; i++)
   72               {
   73                   _map.put(array[i].toString(), array[i]);
   74                   int j = array[i].intValue();
   75                   while (_list.size() <= j)
   76                       _list.add(null);
   77                   _list.set(j, array[i]);
   78               }
   79           }
   80           
   81           /** Returns the singleton for a {@link String}, or null if none. */
   82           public StringEnumAbstractBase forString(String s)
   83           {
   84               return (StringEnumAbstractBase)_map.get(s);
   85           }
   86           /** Returns the singleton for an int code, or null if none. */
   87           public StringEnumAbstractBase forInt(int i)
   88           {
   89               if (i < 0 || i > _list.size())
   90                   return null;
   91               return (StringEnumAbstractBase)_list.get(i);
   92           }
   93           /** Returns the last valid int code (the first is 1; zero is not used). */
   94           public int lastInt()
   95           {
   96               return _list.size() - 1;
   97           }
   98       }
   99   }

Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » [javadoc | source]