001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/datatypes/CodeList.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    package org.deegree.datatypes;
037    
038    import java.io.Serializable;
039    import java.net.URI;
040    import java.util.ArrayList;
041    import java.util.Arrays;
042    import java.util.List;
043    
044    /**
045     *
046     *
047     * @version $Revision: 18195 $
048     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
049     * @author last edited by: $Author: mschneider $
050     *
051     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
052     *
053     * @since 2.0
054     */
055    public class CodeList implements Serializable {
056    
057        /**
058         *
059         */
060        private static final long serialVersionUID = 1L;
061    
062        private URI codeSpace = null;
063    
064        private String name = null;
065    
066        private List<String> codes = new ArrayList<String>();
067    
068        /**
069         * @param name
070         * @param codes
071         */
072        public CodeList( String name, String[] codes ) {
073            this( name, codes, null );
074        }
075    
076        /**
077         * @param name
078         * @param codes
079         * @param codeSpace
080         */
081        public CodeList( String name, String[] codes, URI codeSpace ) {
082            setName( name );
083            setCodes( codes );
084            setCodeSpace( codeSpace );
085        }
086    
087        /**
088         * @return Returns the name.
089         *
090         */
091        public String getName() {
092            return name;
093        }
094    
095        /**
096         * @param name
097         *            The name to set.
098         *
099         */
100        public void setName( String name ) {
101            this.name = name;
102        }
103    
104        /**
105         * @return Returns the codeSpace.
106         *
107         */
108        public URI getCodeSpace() {
109            return codeSpace;
110        }
111    
112        /**
113         * @param codeSpace
114         *            The codeSpace to set.
115         *
116         */
117        public void setCodeSpace( URI codeSpace ) {
118            this.codeSpace = codeSpace;
119        }
120    
121        /**
122         * @return Returns the codes.
123         *
124         */
125        public String[] getCodes() {
126            return codes.toArray( new String[codes.size()] );
127        }
128    
129        /**
130         * @param codes
131         *            The codes to set.
132         */
133        public void setCodes( String[] codes ) {
134            this.codes = Arrays.asList( codes );
135        }
136    
137        /**
138         * @param code
139         *            The code to add
140         */
141        public void addCode( String code ) {
142            codes.add( code );
143        }
144    
145        /**
146         * @param code
147         *            The code to remove
148         */
149        public void removeCode( String code ) {
150            codes.remove( code );
151        }
152    
153        /**
154         * returns true if a CodeList contains the passed codeSpace-value combination. Otherwise false
155         * will be returned
156         *
157         * @param codeSpace
158         * @param value
159         * @return true if a CodeList contains the passed codeSpace-value combination. Otherwise false
160         *         will be returned
161         */
162        public boolean validate( String codeSpace, String value ) {
163            String[] codes = getCodes();
164            URI space = getCodeSpace();
165            String csp = null;
166            if ( space != null ) {
167                csp = space.toString();
168            }
169            for ( int j = 0; j < codes.length; j++ ) {
170                if ( ( csp != null && csp.equals( codeSpace ) ) || ( csp == null && codeSpace == null )
171                     && codes[j].equals( value ) ) {
172                    return true;
173                }
174            }
175            return false;
176        }
177    
178    }