001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/crs/components/PrimeMeridian.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.crs.components;
038    
039    import static org.deegree.crs.projections.ProjectionUtils.EPS11;
040    
041    import java.io.Serializable;
042    
043    import org.deegree.crs.Identifiable;
044    
045    /**
046     * The <code>PrimeMeridian</code> class saves the longitude to the greenwich meridian.
047     *
048     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
049     *
050     * @author last edited by: $Author: mschneider $
051     *
052     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
053     *
054     */
055    
056    public class PrimeMeridian extends Identifiable implements Serializable {
057    
058        private static final long serialVersionUID = -3719400673666655763L;
059    
060        private double longitude;
061    
062        private Unit units;
063    
064        /**
065         * The PrimeMeridian of greenwich with epsg:8901 code and radian units.
066         */
067        public static final PrimeMeridian GREENWICH = new PrimeMeridian(
068                                                                         Unit.DEGREE,
069                                                                         0,
070                                                                         new String[] {
071                                                                                       "EPSG:8901",
072                                                                                       "HTTP://WWW.OPENGIS.NET/GML/SRS/EPSG.XML#8901",
073                                                                                       "URN:OPENGIS:DEF:CRS:EPSG::8901",
074                                                                                       "URN:OGC:DEF:CRS:EPSG::8901" },
075                                                                         new String[] { "Greenwich" },
076                                                                         new String[] { "1995-06-02" }, null, null );
077    
078        /**
079         * @param units
080         *            Angular units of longitude, normally radians.
081         * @param longitude
082         *            (in given units) normally radians.
083         * @param id
084         *            to be cloned
085         */
086        public PrimeMeridian( Unit units, double longitude, Identifiable id ) {
087            super( id );
088            this.units = units;
089            this.longitude = longitude;
090        }
091    
092        /**
093         * @param units
094         *            Angular units of longitude, normally radians.
095         * @param longitude
096         *            (in given units) normally radians.
097         * @param identifiers
098         * @param names
099         * @param versions
100         * @param descriptions
101         * @param areasOfUse
102         */
103        public PrimeMeridian( Unit units, double longitude, String[] identifiers, String[] names, String[] versions,
104                              String[] descriptions, String[] areasOfUse ) {
105            this( units, longitude, new Identifiable( identifiers, names, versions, descriptions, areasOfUse ) );
106        }
107    
108        /**
109         * @param units
110         *            Angular units of longitude.
111         * @param longitude
112         * @param identifier
113         * @param name
114         * @param version
115         * @param description
116         * @param areaOfUse
117         */
118        public PrimeMeridian( Unit units, double longitude, String identifier, String name, String version,
119                              String description, String areaOfUse ) {
120            this( units, longitude, new String[] { identifier }, new String[] { name }, new String[] { version },
121                  new String[] { description }, new String[] { areaOfUse } );
122        }
123    
124        /**
125         * @param units
126         *            Angular units of longitude.
127         * @param longitude
128         * @param identifiers
129         */
130        public PrimeMeridian( Unit units, double longitude, String[] identifiers ) {
131            this( units, longitude, identifiers, null, null, null, null );
132        }
133    
134        /**
135         * @param units
136         *            Angular units of longitude.
137         * @param longitude
138         * @param identifier
139         */
140        public PrimeMeridian( Unit units, double longitude, String identifier ) {
141            this( units, longitude, new String[] { identifier } );
142        }
143    
144        /**
145         * A Prime meredian with 0 degrees longitude from the greenwich meridian.
146         *
147         * @param units
148         *            Angular units of longitude.
149         * @param identifiers
150         */
151        public PrimeMeridian( Unit units, String[] identifiers ) {
152            this( units, 0, identifiers );
153        }
154    
155        /**
156         * A Prime meredian with 0 degrees longitude from the greenwich meridian.
157         *
158         * @param units
159         *            Angular units of longitude.
160         * @param identifier
161         * @param name
162         *            human readable name
163         */
164        public PrimeMeridian( Unit units, String identifier, String name ) {
165            this( units, 0, new String[] { identifier }, new String[] { name }, null, null, null );
166        }
167    
168        /**
169         * @return the longitude value relative to the Greenwich Meridian. The longitude is expressed in this objects
170         *         angular units.
171         */
172        public double getLongitude() {
173            return longitude;
174        }
175    
176        /**
177         * @param targetUnit
178         *            The unit in which to express longitude.
179         * @return the longitude value relative to the Greenwich Meridian, expressed in the specified units. This
180         *         convenience method make easier to obtains longitude in degrees (<code>getLongitude(Unit.DEGREE)</code>),
181         *         no matter the underlying angular unit of this prime meridian.
182         */
183        public double getLongitude( final Unit targetUnit ) {
184            return getAngularUnit().convert( getLongitude(), targetUnit );
185        }
186    
187        /**
188         * @return the longitude value relative to the Greenwich Meridian, expressed in the radians.
189         */
190        public double getLongitudeAsRadian() {
191            return getAngularUnit().convert( getLongitude(), Unit.RADIAN );
192        }
193    
194        /**
195         * @return the angular unit.
196         */
197        public Unit getAngularUnit() {
198            return units;
199        }
200    
201        @Override
202        public boolean equals( Object other ) {
203            if ( other != null && other instanceof PrimeMeridian ) {
204                PrimeMeridian that = (PrimeMeridian) other;
205                return ( Math.abs( this.longitude - that.longitude ) < EPS11 )
206                       && ( ( this.units != null ) ? this.units.equals( that.units ) : ( that.units == null ) )
207                       && super.equals( that );
208            }
209            return false;
210        }
211    
212        @Override
213        public String toString() {
214            StringBuilder sb = new StringBuilder( super.toString() );
215            sb.append( ", Units: " ).append( units );
216            sb.append( ", longitude: " ).append( longitude );
217            return sb.toString();
218        }
219    
220        /**
221         * Implementation as proposed by Joshua Block in Effective Java (Addison-Wesley 2001), which supplies an even
222         * distribution and is relatively fast. It is created from field <b>f</b> as follows:
223         * <ul>
224         * <li>boolean -- code = (f ? 0 : 1)</li>
225         * <li>byte, char, short, int -- code = (int)f</li>
226         * <li>long -- code = (int)(f ^ (f &gt;&gt;&gt;32))</li>
227         * <li>float -- code = Float.floatToIntBits(f);</li>
228         * <li>double -- long l = Double.doubleToLongBits(f); code = (int)(l ^ (l &gt;&gt;&gt; 32))</li>
229         * <li>all Objects, (where equals(&nbsp;) calls equals(&nbsp;) for this field) -- code = f.hashCode(&nbsp;)</li>
230         * <li>Array -- Apply above rules to each element</li>
231         * </ul>
232         * <p>
233         * Combining the hash code(s) computed above: result = 37 * result + code;
234         * </p>
235         *
236         * @return (int) ( result >>> 32 ) ^ (int) result;
237         *
238         * @see java.lang.Object#hashCode()
239         */
240        @Override
241        public int hashCode() {
242            // the 2.nd million th. prime, :-)
243            long code = 32452843;
244            long ll = Double.doubleToLongBits( longitude );
245            code = code * 37 + (int) ( ll ^ ( ll >>> 32 ) );
246            if ( units != null ) {
247                code = code * 37 + units.hashCode();
248            }
249            return (int) ( code >>> 32 ) ^ (int) code;
250        }
251    
252    }