001    /**
002     * <copyright>
003     *
004     * Copyright (c) 2002-2006 IBM Corporation and others.
005     * All rights reserved.   This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors: 
011     *   IBM - Initial API and implementation
012     *
013     * </copyright>
014     *
015     * $Id: AbstractEnumerator.java,v 1.6 2009/04/18 11:45:26 emerks Exp $
016     */
017    package org.eclipse.emf.common.util;
018    
019    import java.io.Externalizable;
020    import java.io.IOException;
021    import java.io.ObjectInput;
022    import java.io.ObjectOutput;
023    import java.io.ObjectStreamException;
024    import java.io.Serializable;
025    
026    /**
027     * An extensible enumerator implementation.
028     */
029    public abstract class AbstractEnumerator implements Enumerator, Serializable
030    {
031      /**
032       * The name of the enumerator.
033       */
034      private final String name;
035    
036      /**
037       * The <code>int</code> value of the enumerator.
038       */
039      private final int value;
040      
041      /**
042       * The literal value of the enumerator.
043       */
044      private final String literal;
045    
046      /**
047       * Creates an initialized instance.
048       * @param value the <code>int</code> value of the enumerator.
049       * @param name the name of the enumerator, which is also used as the literal value.
050       */
051      protected AbstractEnumerator(int value, String name)
052      {
053        this.name = literal = name;
054        this.value = value;
055      }
056      
057      /**
058       * Creates an initialized instance.
059       * @param value the <code>int</code> value of the enumerator.
060       * @param name the name of the enumerator.
061       * @param literal the literal value of the enumerator.
062       */
063      protected AbstractEnumerator(int value, String name, String literal)
064      {
065        this.name = name;
066        this.value = value;
067        this.literal = literal;
068      }
069    
070      /**
071       * Returns the name of the enumerator.
072       * @return the name.
073       */
074      public final String getName()
075      {
076        return name;
077      }
078    
079      /**
080       * Returns the <code>int</code> value of the enumerator.
081       * @return the value.
082       */
083      public final int getValue()
084      {
085        return value;
086      }
087      
088      /**
089       * Returns the literal value of the enumerator.
090       * @return the literal.
091       */
092      public final String getLiteral()
093      {
094        return literal;
095      }
096    
097      /**
098       * Returns the literal value of the enumerator, which is its string representation.
099       * @return the literal.
100       */
101      @Override
102      public final String toString()
103      {
104        return literal;
105      }
106    
107      private static class AbstractEnumeratorExternalizeable implements Externalizable
108      {
109        protected AbstractEnumerator enumerator;
110    
111        public AbstractEnumeratorExternalizeable(AbstractEnumerator enumerator)
112        {
113          this.enumerator = enumerator;
114        }
115    
116        public void writeExternal(ObjectOutput objectOutput) throws IOException
117        {
118          objectOutput.writeObject(enumerator.getClass());
119          objectOutput.writeUTF(enumerator.getName());
120        }
121    
122        private static final Class<?> [] SIGNATURE = new Class [] { String.class };
123    
124        public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException
125        {
126          Class<?> _class = (Class<?>)objectInput.readObject();
127          String name = objectInput.readUTF();
128          try
129          {
130            enumerator = (AbstractEnumerator)_class.getMethod("get", SIGNATURE).invoke(null, new Object [] { name });
131          }
132          catch (Exception exception)
133          {
134            IOException ioException = new IOException();
135            ioException.initCause(exception);
136            throw ioException;
137          }
138        }
139    
140        protected Object readResolve()
141        {
142          return enumerator;
143        } 
144      }
145    
146      protected Object writeReplace() throws ObjectStreamException
147      {
148        return new AbstractEnumeratorExternalizeable(this);
149      }
150    }