001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/coverage/CodeList.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.model.coverage;
045    
046    // J2SE direct dependencies
047    import java.io.InvalidObjectException;
048    import java.io.ObjectStreamException;
049    import java.io.Serializable;
050    import java.util.Collection;
051    
052    /**
053     * Base class for all code lists.
054     * 
055     * @author <A HREF="http://www.opengis.org">OpenGIS&reg; consortium</A>
056     * @version 2.0
057     */
058    public abstract class CodeList implements Serializable {
059        /**
060         * Serial number for compatibility with different versions.
061         */
062        private static final long serialVersionUID = 5655809691319522885L;
063    
064        /**
065         * The code value. For J2SE 1.3 profile only.
066         */
067        private transient final int ordinal;
068    
069        /**
070         * The code name. For J2SE 1.3 profile only.
071         */
072        private final String name;
073    
074        /**
075         * Create a new code list instance.
076         * 
077         * @param name
078         *            The code name.
079         * @param ordinal
080         *            The code value.
081         */
082        protected CodeList( final String name, final int ordinal ) {
083            this.name = name;
084            this.ordinal = ordinal;
085        }
086    
087        /**
088         * Create a new code list instance and add it to the given collection.
089         * 
090         * @param name
091         *            The code name.
092         * @param values
093         *            The collection to add the enum to.
094         */
095        CodeList( final String name, final Collection<CodeList> values ) {
096            this.name = name;
097            synchronized ( values ) {
098                this.ordinal = values.size();
099                if ( !values.add( this ) ) {
100                    throw new IllegalArgumentException( String.valueOf( values ) );
101                }
102            }
103        }
104    
105        /**
106         * Returns the ordinal of this enumeration constant (its position in its enum declaration, where
107         * the initial constant is assigned an ordinal of zero).
108         * 
109         * @return the ordinal of this enumeration constant.
110         */
111        public final int ordinal() {
112            return ordinal;
113        }
114    
115        /**
116         * Returns the name of this enum constant.
117         * 
118         * @return the name of this enum constant.
119         */
120        public final String name() {
121            return name;
122        }
123    
124        /**
125         * Returns the list of enumerations of the same kind than this enum.
126         */
127        public abstract CodeList[] family();
128    
129        /**
130         * Returns a string representation of this code list.
131         */
132        public String toString() {
133            String classname = getClass().getName();
134            final int i = classname.lastIndexOf( '.' );
135            if ( i >= 0 ) {
136                classname = classname.substring( i + 1 );
137            }
138            return classname + '[' + name + ']';
139        }
140    
141        /**
142         * Resolve the code list to an unique instance after deserialization. The instance is resolved
143         * using its {@linkplain #name() name} only (not its {@linkplain #ordinal() ordinal}).
144         * 
145         * @return This code list as a unique instance.
146         * @throws ObjectStreamException
147         *             if the deserialization failed.
148         */
149        protected Object readResolve()
150                                throws ObjectStreamException {
151            final CodeList[] codes = family();
152            for ( int i = 0; i < codes.length; i++ ) {
153                if ( name.equals( codes[i].name ) ) {
154                    return codes[i];
155                }
156            }
157            throw new InvalidObjectException( toString() );
158        }
159    }