001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/spatialschema/CurveSegmentImpl.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004    This file is part of deegree.
005    Copyright (C) 2001-2007 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.spatialschema;
045    
046    import java.io.Serializable;
047    
048    import org.deegree.model.crs.CoordinateSystem;
049    
050    /**
051     * default implementation of the CurveSegment interface from
052     * package jago.model. the class is abstract because it should be
053     * specialized by derived classes <code>LineString</code> for example
054     * 
055     * 
056     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
057     * @author last edited by: $Author: apoth $
058     * 
059     * @version. $Revision: 6704 $, $Date: 2007-04-26 21:43:13 +0200 (Do, 26 Apr 2007) $
060     */
061    abstract class CurveSegmentImpl implements CurveSegment, Serializable {
062        /** Use serialVersionUID for interoperability. */
063        private final static long serialVersionUID = -8102075931849374162L;
064    
065        protected CoordinateSystem crs = null;
066    
067        protected Position[] points = new Position[0];
068    
069        /**
070         * Creates a new CurveSegmentImpl object.
071         *
072         * @param gmps 
073         * @param crs 
074         *
075         * @throws GeometryException 
076         */
077        protected CurveSegmentImpl( Position[] gmps, CoordinateSystem crs )
078                                throws GeometryException {
079            if ( gmps == null ) {
080                throw new GeometryException( "can't create an empty curve segment" );
081            }
082    
083            points = gmps;
084    
085            // get spatial reference system of the curve segment from the first point
086            this.crs = crs;
087        }
088    
089        /**
090         * returns the first point of the curve. if the curve segment
091         * doesn't contain a point <code>null</code> will be returned
092         */
093        public Point getStartPoint() {
094            return new PointImpl( points[0], crs );
095        }
096    
097        /**
098         * returns the last point of the curve. if the curve segment
099         * doesn't contain a point <code>null</code> will be returned
100         */
101        public Point getEndPoint() {
102            return new PointImpl( points[getNumberOfPoints() - 1], crs );
103        }
104    
105        /**
106         * returns the number of points building the curve 
107         * or curve segment
108         */
109        public int getNumberOfPoints() {
110            return points.length;
111        }
112    
113        /**
114         * returns all positions of the segement as array of Position. If
115         * the segment is empty null will be returned
116         */
117        public Position[] getPositions() {
118            return points;
119        }
120    
121        /**
122         * returns the curve segment position at the submitted index
123         */
124        public Position getPositionAt( int index ) {
125            return points[index];
126        }
127    
128        /**
129         * reverses the direction of the curvesegment
130         */
131        public void reverse() {
132            Position[] reverse_ = new Position[points.length];
133    
134            for ( int i = 0; i < points.length; i++ ) {
135                reverse_[points.length - 1 - i] = points[i];
136            }
137    
138            points = reverse_;
139        }
140    
141        /**
142         * returns the coordinate system of the curve segment
143         */
144        public CoordinateSystem getCoordinateSystem() {
145            return crs;
146        }
147    
148        /**
149         * checks if this curve segment is completly equal to the submitted geometry
150         * @param other object to compare to
151         */
152        public boolean equals( Object other ) {
153            if ( ( other == null ) || !( other instanceof CurveSegmentImpl ) ) {
154                return false;
155            }
156    
157            if ( ( crs == null ) && ( ( (CurveSegmentImpl)other ).getCoordinateSystem() != null ) ) {
158                return false;
159            }
160    
161            if ( crs != null ) {
162                if ( !crs.equals( ( (CurveSegmentImpl)other ).getCoordinateSystem() ) ) {
163                    return false;
164                }
165            } else {
166                if ( ( (CurveSegmentImpl)other ).getCoordinateSystem() != null ) {
167                    return false;
168                }
169            }
170    
171            Position[] p1 = getPositions();
172            Position[] p2 = ( (CurveSegment)other ).getPositions();
173            
174            if ( p1.length != p2.length ) {
175                return false;
176            }
177           
178    
179            //if ( !Arrays.equals( p1, p2 ) ) {
180                //TODO 
181                // correct comparing of each point considering current tolerance level
182            //}
183    
184            return true;
185        }
186    
187        /**
188         * The Boolean valued operation "contains" shall return TRUE if this Geometry
189         * contains another Geometry.<p></p>
190         */
191        public boolean contains( Geometry gmo ) {
192            throw new NoSuchMethodError( "the contains operation for curve segments " + 
193                                         "isn't supported at the moment." );
194        }
195    
196        /**
197         *
198         *
199         * @return  string representation
200         */
201        public String toString() {
202            String ret = null;
203            ret = "points = ";
204            ret += ( "crs = " + crs + "\n" );
205            return ret;
206        }
207    }