001    //$HeadURL: $
002    /*----------------    FILE HEADER  ------------------------------------------
003     This file is part of deegree.
004     Copyright (C) 2001-2008 by:
005     Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014     This library is distributed in the hope that it will be useful,
015     but WITHOUT ANY WARRANTY; without even the implied warranty of
016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     Lesser General Public License for more details.
018     You should have received a copy of the GNU Lesser General Public
019     License along with this library; if not, write to the Free Software
020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     Contact:
022    
023     Andreas Poth
024     lat/lon GmbH
025     Aennchenstr. 19
026     53177 Bonn
027     Germany
028     E-Mail: poth@lat-lon.de
029    
030     Prof. Dr. Klaus Greve
031     Department of Geography
032     University of Bonn
033     Meckenheimer Allee 166
034     53115 Bonn
035     Germany
036     E-Mail: greve@giub.uni-bonn.de
037     ---------------------------------------------------------------------------*/
038    
039    package org.deegree.crs;
040    
041    import org.deegree.i18n.Messages;
042    
043    /**
044     * The <code>Identifiable</code> class can be used to identify a crs, ellipsoid, Datum and primemeridian
045     * 
046     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
047     * 
048     * @author last edited by: $Author:$
049     * 
050     * @version $Revision:$, $Date:$
051     * 
052     */
053    
054    public class Identifiable {
055    
056        private String[] identifiers;
057    
058        private String[] versions;
059    
060        private String[] names;
061    
062        private String[] descriptions;
063    
064        private String[] areasOfUse;
065    
066        /**
067         * Takes the references of the other object and stores them in this Identifiable Object.
068         * @param other identifiable object.
069         */
070        public Identifiable( Identifiable other ){
071            this( other.getIdentifiers(), other.getNames(), other.getVersions(), other.getDescriptions(), other.getAreasOfUse() );
072        }
073    
074        /**
075         * 
076         * @param identifiers
077         * @param names the human readable names of the object.
078         * @param versions
079         * @param descriptions
080         * @param areasOfUse
081         * @throws IllegalArgumentException
082         *             if no identifier(s) was/were given.
083         */
084        public Identifiable( String[] identifiers, String[] names, String[] versions, String[] descriptions,
085                                String[] areasOfUse ) {
086            if ( identifiers == null || identifiers.length == 0 ) {
087                throw new IllegalArgumentException( "An identifiable object must at least have one identifier." );
088            }
089            this.identifiers = identifiers;
090    
091            this.names = names;
092            this.versions = versions;
093            this.descriptions = descriptions;
094            this.areasOfUse = areasOfUse;
095        }
096    
097        /**
098         * Creates arrays fromt the given identifier and name without setting the versions, descriptions and areasOfUse.
099         * 
100         * @param identifiers of the object.
101         */
102        public Identifiable( String[] identifiers ) {
103            this( identifiers, null, null, null, null );
104        }
105        
106    //    /**
107    //     * Creates arrays fromt the given identifier and name without setting the versions, descriptions and areasOfUse.
108    //     * 
109    //     * @param identifier of the object.
110    //     * @param name the human readable name of the object.
111    //     */
112    //    protected Identifiable( String identifier, String name ) {
113    //        this( new String[]{identifier}, new String[]{name}, null, null, null );
114    //    }
115    
116    
117        /**
118         * @return the first of all areasOfUse or <code>null</code> if no areasOfUse were given.
119         */
120        public final String getAreaOfUse() {
121            return ( areasOfUse != null && areasOfUse.length > 0 ) ? areasOfUse[0] : null;
122        }
123    
124        /**
125         * @return the first of all descriptions or <code>null</code> if no descriptions were given.
126         */
127        public final String getDescription() {
128            return ( descriptions != null && descriptions.length > 0 ) ? descriptions[0] : null;
129        }
130    
131        /**
132         * @return the first of all identifiers.
133         */
134        public final String getIdentifier() {
135            return identifiers[0];
136        }
137    
138        /**
139         * @return the first of all names or <code>null</code> if no names were given.
140         */
141        public final String getName() {
142            return ( names != null && names.length > 0 ) ? names[0] : null;
143        }
144    
145        /**
146         * @return the first of all versions or <code>null</code> if no versions were given.
147         */
148        public final String getVersion() {
149            return ( versions != null && versions.length > 0 ) ? versions[0] : null;
150        }
151    
152        /**
153         * throws an InvalidParameterException if the given object is null
154         * 
155         * @param toBeChecked
156         *            for <code>null</code>
157         * @param message
158         *            to put into the exception. If absend, the default message (CRS_INVALID_NULL_PARAMETER) will be
159         *            inserted.
160         * @throws IllegalArgumentException
161         *             if the given object is <code>null</code>.
162         */
163        protected void checkForNullObject( Object toBeChecked, String message )
164                                                                               throws IllegalArgumentException {
165            if ( toBeChecked != null ) {
166                return;
167            }
168            if ( message == null || "".equals( message.trim() ) ) {
169                message = Messages.getMessage( "CRS_INVALID_NULL_PARAMETER" );
170            }
171            throw new IllegalArgumentException( message );
172        }
173    
174        /**
175         * throws an IllegalArgumentException if the given object array is null or empty
176         * 
177         * @param toBeChecked
178         *            for <code>null</code> or empty
179         * @param message
180         *            to put into the exception. If absend, the default message (CRS_INVALID_NULL_PARAMETER) will be
181         *            inserted.
182         * @throws IllegalArgumentException
183         *             if the given object array is <code>null</code> or empty.
184         */
185        protected void checkForNullObject( Object[] toBeChecked, String message )
186                                                                                 throws IllegalArgumentException {
187            if ( toBeChecked != null && toBeChecked.length != 0 ) {
188                return;
189            }
190            if ( message == null || "".equals( message.trim() ) ) {
191                message = Messages.getMessage( "CRS_INVALID_NULL_ARRAY" );
192            }
193            throw new IllegalArgumentException( message );
194        }
195    
196        @Override
197        public String toString() {
198            StringBuilder sb = new StringBuilder( "id: [" );
199            for ( int i = 0; i < identifiers.length; ++i ) {
200                sb.append( identifiers[i] );
201                if ( ( i + 1 ) < identifiers.length ) {
202                    sb.append( ", " );
203                }
204            }
205            if ( getName() != null ) {
206                sb.append( "], name: [" );
207                for ( int i = 0; i < names.length; ++i ) {
208                    sb.append( names[i] );
209                    if ( ( i + 1 ) < names.length ) {
210                        sb.append( ", " );
211                    }
212                }
213            }
214            if ( getVersion() != null ) {
215                sb.append( "], version: [" );
216                for ( int i = 0; i < versions.length; ++i ) {
217                    sb.append( versions[i] );
218                    if ( ( i + 1 ) < versions.length ) {
219                        sb.append( ", " );
220                    }
221                }
222            }
223            if ( getDescription() != null ) {
224                sb.append( "], description: [" );
225                for ( int i = 0; i < descriptions.length; ++i ) {
226                    sb.append( descriptions[i] );
227                    if ( ( i + 1 ) < descriptions.length ) {
228                        sb.append( ", " );
229                    }
230                }
231            }
232            if ( getAreaOfUse() != null ) {
233                sb.append( "], areasOfUse: [" );
234                for ( int i = 0; i < areasOfUse.length; ++i ) {
235                    sb.append( areasOfUse[i] );
236                    if ( ( i + 1 ) < areasOfUse.length ) {
237                        sb.append( ", " );
238                    }
239                }
240            }
241            return sb.toString();
242        }
243    
244        /**
245         * @return the first id and the name (if given) as id: id, name: name.
246         */
247        public String getIdAndName() {
248            StringBuilder sb = new StringBuilder( "id: " ).append( getIdentifier() );
249            if( getName() != null ) {
250                sb.append( ", name: " ).append( getName() );
251            }
252            return sb.toString();
253    
254        }
255    
256        /**
257         * @return the areasOfUse or <code>null</code> if no areasOfUse were given.
258         */
259        public final String[] getAreasOfUse() {
260            return areasOfUse;
261        }
262    
263        /**
264         * @return the descriptions or <code>null</code> if no descriptions were given.
265         */
266        public final String[] getDescriptions() {
267            return descriptions;
268        }
269    
270        /**
271         * @return the identifiers, each identifiable object has atleast one id.
272         */
273        public final String[] getIdentifiers() {
274            return identifiers;
275        }
276    
277        /**
278         * @return the names or <code>null</code> if no names were given.
279         */
280        public final String[] getNames() {
281            return names;
282        }
283    
284        /**
285         * @return the versions or <code>null</code> if no versions were given.
286         */
287        public final String[] getVersions() {
288            return versions;
289        }
290    
291    }