001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/csct/ct/DomainFlags.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/exse/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     It has been implemented within SEAGIS - An OpenSource implementation of OpenGIS specification
012     (C) 2001, Institut de Recherche pour le D�veloppement (http://sourceforge.net/projects/seagis/)
013     SEAGIS Contacts:  Surveillance de l'Environnement Assist�e par Satellite
014     Institut de Recherche pour le D�veloppement / US-Espace
015     mailto:seasnet@teledetection.fr
016    
017    
018     This library is free software; you can redistribute it and/or
019     modify it under the terms of the GNU Lesser General Public
020     License as published by the Free Software Foundation; either
021     version 2.1 of the License, or (at your option) any later version.
022    
023     This library is distributed in the hope that it will be useful,
024     but WITHOUT ANY WARRANTY; without even the implied warranty of
025     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026     Lesser General Public License for more details.
027    
028     You should have received a copy of the GNU Lesser General Public
029     License along with this library; if not, write to the Free Software
030     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031    
032     Contact:
033    
034     Andreas Poth
035     lat/lon GmbH
036     Aennchenstr. 19
037     53115 Bonn
038     Germany
039     E-Mail: poth@lat-lon.de
040    
041     Klaus Greve
042     Department of Geography
043     University of Bonn
044     Meckenheimer Allee 166
045     53115 Bonn
046     Germany
047     E-Mail: klaus.greve@uni-bonn.de
048    
049     
050     ---------------------------------------------------------------------------*/
051    package org.deegree.model.csct.ct;
052    
053    // OpenGIS dependencies
054    import java.io.ObjectStreamException;
055    import java.util.Locale;
056    import java.util.NoSuchElementException;
057    
058    import javax.media.jai.EnumeratedParameter;
059    
060    import org.deegree.model.csct.resources.XArray;
061    import org.deegree.model.csct.resources.css.ResourceKeys;
062    import org.deegree.model.csct.resources.css.Resources;
063    
064    /**
065     * Flags indicating parts of domain covered by a convex hull. These flags can be combined. For
066     * example, the enum <code>{@link #INSIDE}.or({@link #OUTSIDE})</code> means that some parts of
067     * the convex hull are inside the domain, and some parts of the convex hull are outside the domain.
068     * 
069     * @version 1.00
070     * @author OpenGIS (www.opengis.org)
071     * @author Martin Desruisseaux
072     * 
073     * @author last edited by: $Author: bezema $
074     * 
075     * @version $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
076     * 
077     * @see "org.opengis.ct.CT_DomainFlags"
078     */
079    public final class DomainFlags extends EnumeratedParameter {
080        /**
081         * Serial number for interoperability with different versions.
082         */
083        private static final long serialVersionUID = 6991557069252861278L;
084    
085        /**
086         * Domain flags by value. Used to canonicalize after deserialization.
087         */
088        private static final DomainFlags[] ENUMS = new DomainFlags[8];
089    
090        /**
091         * At least one point in a convex hull is inside the transform's domain.
092         */
093        public static final int CT_DF_Inside = 1;
094    
095        /**
096         * At least one point in a convex hull is outside the transform's domain.
097         */
098        public static final int CT_DF_Outside = 2;
099    
100        /**
101         * At least one point in a convex hull is not transformed continuously. As an example, consider
102         * a "Longitude_Rotation" transform which adjusts longitude coordinates to take account of a
103         * change in Prime Meridian. If the rotation is 5 degrees east, then the point (Lat=0,Lon=175)
104         * is not transformed continuously, since it is on the meridian line which will be split at
105         * +180/-180 degrees.
106         */
107        public static final int CT_DF_Discontinuous = 4;
108    
109        /**
110         * Initialize {@link #ENUMS} during class loading. This code must be done before we initialize
111         * public fields.
112         */
113        static {
114            for ( int i = ENUMS.length; --i >= 0; ) {
115                String name = null;
116                switch ( i ) {
117                case 0:
118                    name = "UNKNOW";
119                    break;
120                case CT_DF_Inside:
121                    name = "INSIDE";
122                    break;
123                case CT_DF_Outside:
124                    name = "OUTSIDE";
125                    break;
126                case CT_DF_Discontinuous:
127                    name = "DISCONTINUOUS";
128                    break;
129                }
130                ENUMS[i] = new DomainFlags( name, i );
131            }
132        }
133    
134        /**
135         * At least one point in a convex hull is inside the transform's domain.
136         * 
137         * @see "org.opengis.ct.CT_DomainFlags#CT_DF_Inside"
138         */
139        public static final DomainFlags INSIDE = ENUMS[CT_DF_Inside];
140    
141        /**
142         * At least one point in a convex hull is outside the transform's domain.
143         * 
144         * @see "org.opengis.ct.CT_DomainFlags#CT_DF_Outside"
145         */
146        public static final DomainFlags OUTSIDE = ENUMS[CT_DF_Outside];
147    
148        /**
149         * At least one point in a convex hull is not transformed continuously. As an example, consider
150         * a "Longitude_Rotation" transform which adjusts longitude coordinates to take account of a
151         * change in Prime Meridian. If the rotation is 5 degrees east, then the point (Lat=0,Lon=175)
152         * is not transformed continuously, since it is on the meridian line which will be split at
153         * +180/-180 degrees.
154         * 
155         * @see "org.opengis.ct.CT_DomainFlags#CT_DF_Discontinuous"
156         */
157        public static final DomainFlags DISCONTINUOUS = ENUMS[CT_DF_Discontinuous];
158    
159        /**
160         * Construct a new enum value.
161         */
162        private DomainFlags( final String name, final int value ) {
163            super( name, value );
164        }
165    
166        /**
167         * Return the enum for the specified value. This method is provided for compatibility with
168         * {@link "org.opengis.ct.CT_DomainFlags"}.
169         * 
170         * @param value
171         *            The enum value.
172         * @return The enum for the specified value.
173         * @throws NoSuchElementException
174         *             if there is no enum for the specified value.
175         */
176        public static DomainFlags getEnum( final int value )
177                                throws NoSuchElementException {
178            if ( value >= 1 && value < ENUMS.length )
179                return ENUMS[value];
180            throw new NoSuchElementException( String.valueOf( value ) );
181        }
182    
183        /**
184         * Returns enum's names in the specified locale. For example if this enum has value "3", then
185         * <code>getNames</code> returns an array of two elements: "Inside" and "Outside".
186         * 
187         * @param locale
188         *            The locale, or <code>null</code> for the current default locale.
189         * @return Enum's names in the specified locale (never <code>null</code>).
190         */
191        public String[] getNames( final Locale locale ) {
192            int count = 0;
193            int bits = getValue();
194            Resources resources = null;
195            final int[] nameKeys = { ResourceKeys.INSIDE, ResourceKeys.OUTSIDE,
196                                    ResourceKeys.DISCONTINUOUS };
197            final String[] names = new String[nameKeys.length];
198            for ( int i = 0; i < nameKeys.length; i++ ) {
199                if ( ( bits & 1 ) != 0 ) {
200                    if ( resources == null )
201                        resources = Resources.getResources( locale );
202                    names[count++] = resources.getString( nameKeys[i] );
203                }
204                bits >>>= 1;
205            }
206            return (String[]) XArray.resize( names, count );
207        }
208    
209        /**
210         * Returns a combination of two domain flags. This is equivalent to
211         * <code>getEnum(this.getValue()&nbsp;|&nbsp;flags.getValue())</code>.
212         * 
213         * @param flags
214         * @return a combination of two domain flags. This is equivalent to
215         *         <code>getEnum(this.getValue()&nbsp;|&nbsp;flags.getValue())</code>.
216         */
217        public DomainFlags or( final DomainFlags flags ) {
218            return getEnum( getValue() | flags.getValue() );
219        }
220    
221        /**
222         * Use a single instance of {@link DomainFlags} after deserialization. It allow client code to
223         * test <code>enum1==enum2</code> instead of <code>enum1.equals(enum2)</code>.
224         * 
225         * @return A single instance of this enum.
226         * @throws ObjectStreamException
227         *             is deserialization failed.
228         */
229        private Object readResolve() {
230            final int value = getValue();
231            if ( value >= 0 && value < ENUMS.length ) {
232                return ENUMS[value]; // Canonicalize
233            }
234            return ENUMS[0]; // Collapse unknow value to a single canonical one
235        }
236    }