001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wms/operation/DimensionValues.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.ogcwebservices.wms.operation;
038    
039    import static java.lang.Float.parseFloat;
040    import static java.lang.Math.abs;
041    
042    import java.util.LinkedList;
043    
044    /**
045     * <code>DimensionValues</code>
046     *
047     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
048     * @author last edited by: $Author: mschneider $
049     *
050     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
051     */
052    public class DimensionValues {
053    
054        /**
055         * The actual values.
056         */
057        public LinkedList<DimensionValue> values;
058    
059        /**
060         * @param val
061         */
062        public DimensionValues( String val ) {
063            values = new LinkedList<DimensionValue>();
064            String[] vals = val.split( "," );
065            for ( String v : vals ) {
066                values.add( new DimensionValue( v ) );
067            }
068        }
069    
070        /**
071         * @return if multiple values are contained
072         */
073        public boolean hasMultipleValues() {
074            return values.size() > 1;
075        }
076    
077        /**
078         * @param value
079         * @return true, if the given single value is contained
080         */
081        public boolean includesValue( String value ) {
082            for ( DimensionValue val : values ) {
083                if ( val.value == null ) {
084                    // TODO use the resolution
085                    if ( val.low.compareTo( value ) <= 0 && val.high.compareTo( value ) >= 0 ) {
086                        return true;
087                    }
088                } else {
089                    if ( val.value.equals( value ) ) {
090                        return true;
091                    }
092                }
093            }
094    
095            return false;
096        }
097    
098        /**
099         * @param value
100         * @return true, if the given single value is contained
101         */
102        public boolean includesValue( float value ) {
103            for ( DimensionValue val : values ) {
104                if ( val.value == null ) {
105                    // TODO use resolution
106                    if ( val.lowf <= value && val.highf >= value ) {
107                        return true;
108                    }
109                } else {
110                    if ( abs( val.valuef - value ) <= 0.0000001 ) {
111                        return true;
112                    }
113                }
114            }
115    
116            return false;
117        }
118    
119        /**
120         * @param elev
121         * @return true, if the value(s) are valid
122         */
123        public boolean includes( DimensionValues elev ) {
124            for ( DimensionValue val : elev.values ) {
125                if ( val.value != null ) {
126                    if ( !includesValue( parseFloat( val.value ) ) ) {
127                        return false;
128                    }
129                } else {
130                    if ( !includesValue( parseFloat( val.high ) ) || !includesValue( parseFloat( val.low ) ) ) {
131                        return false;
132                    }
133                }
134            }
135    
136            return true;
137        }
138    
139        /**
140         * @param value
141         * @return the nearest value
142         */
143        public String getNearestValue( String value ) {
144            String nearestHigh = null, nearestLow = null;
145            for ( DimensionValue val : values ) {
146                if ( val.value == null ) {
147                    // TODO check it for ranges
148                } else {
149                    if ( nearestHigh != null && nearestHigh.compareTo( val.value ) > 0 && val.value.compareTo( value ) > 0 ) {
150                        nearestHigh = val.value;
151                        continue;
152                    }
153                    if ( nearestLow != null && nearestLow.compareTo( val.value ) < 0 && val.value.compareTo( value ) < 0 ) {
154                        nearestLow = val.value;
155                        continue;
156                    }
157                    if ( nearestHigh == null && val.value.compareTo( value ) > 0 ) {
158                        nearestHigh = val.value;
159                        continue;
160                    }
161                    if ( nearestLow == null && val.value.compareTo( value ) < 0 ) {
162                        nearestLow = val.value;
163                        continue;
164                    }
165                }
166            }
167    
168            if ( nearestHigh == null ) {
169                return nearestLow;
170            }
171            if ( nearestLow == null ) {
172                return nearestHigh;
173            }
174            try {
175                float low = parseFloat( nearestLow );
176                float high = parseFloat( nearestHigh );
177                float val = parseFloat( value );
178                if ( abs( val - low ) > abs( val - high ) ) {
179                    return nearestHigh;
180                }
181                return nearestLow;
182            } catch ( NumberFormatException nfe ) {
183                // TODO for time
184            }
185            return null; // TODO check which bound is nearer
186        }
187    
188        /**
189         * <code>DimensionValue</code>
190         *
191         * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
192         * @author last edited by: $Author: mschneider $
193         *
194         * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
195         */
196        public class DimensionValue {
197            /**
198             *
199             */
200            public String value;
201    
202            /**
203             *
204             */
205            public float valuef;
206    
207            /**
208             *
209             */
210            public String low;
211    
212            /**
213             *
214             */
215            public float lowf;
216    
217            /**
218             *
219             */
220            public String high;
221    
222            /**
223             *
224             */
225            public float highf;
226    
227            /**
228             *
229             */
230            public String res;
231    
232            /**
233             *
234             */
235            public float resf;
236    
237            DimensionValue( String val ) {
238                if ( val.indexOf( "/" ) != -1 ) {
239                    String[] vs = val.split( "/" );
240                    low = vs[0];
241                    high = vs[1];
242                    try {
243                        lowf = parseFloat( low );
244                        highf = parseFloat( high );
245                    } catch ( NumberFormatException nfe ) {
246                        // no float values then
247                    }
248                    if ( vs.length > 2 ) {
249                        res = vs[2];
250                        try {
251                            resf = parseFloat( res );
252                        } catch ( NumberFormatException nfe ) {
253                            // no float value then
254                        }
255                    }
256                } else {
257                    value = val;
258                    try {
259                        valuef = parseFloat( value );
260                    } catch ( NumberFormatException nfe ) {
261                        // no float value then
262                    }
263                }
264            }
265        }
266    }