001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/csct/resources/Utilities.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/exse/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     It has been implemented within SEAGIS - An OpenSource implementation of OpenGIS specification
012     (C) 2001, Institut de Recherche pour le D�veloppement (http://sourceforge.net/projects/seagis/)
013     SEAGIS Contacts:  Surveillance de l'Environnement Assist�e par Satellite
014     Institut de Recherche pour le D�veloppement / US-Espace
015     mailto:seasnet@teledetection.fr
016    
017    
018     This library is free software; you can redistribute it and/or
019     modify it under the terms of the GNU Lesser General Public
020     License as published by the Free Software Foundation; either
021     version 2.1 of the License, or (at your option) any later version.
022    
023     This library is distributed in the hope that it will be useful,
024     but WITHOUT ANY WARRANTY; without even the implied warranty of
025     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026     Lesser General Public License for more details.
027    
028     You should have received a copy of the GNU Lesser General Public
029     License along with this library; if not, write to the Free Software
030     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031    
032     Contact:
033    
034     Andreas Poth
035     lat/lon GmbH
036     Aennchenstr. 19
037     53115 Bonn
038     Germany
039     E-Mail: poth@lat-lon.de
040    
041     Klaus Greve
042     Department of Geography
043     University of Bonn
044     Meckenheimer Allee 166
045     53115 Bonn
046     Germany
047     E-Mail: klaus.greve@uni-bonn.de
048    
049     
050     ---------------------------------------------------------------------------*/
051    package org.deegree.model.csct.resources;
052    
053    // Collections
054    import java.util.Arrays;
055    
056    /**
057     * A set of miscellaneous methods.
058     *
059     * @version 1.0
060     * @author Martin Desruisseaux
061     */
062    public final class Utilities {
063        /**
064         * An array of strings containing only white spaces. String length are equal
065         * to their index + 1 in the <code>spacesFactory</code> array.  For example,
066         * <code>spacesFactory[4]</code> contains a string of length 5.  Strings are
067         * constructed only when first needed.
068         */
069        private static final String[] spacesFactory = new String[20];
070    
071        /**
072         * Forbive object creation.
073         */
074        private Utilities() {
075        }
076    
077        /**
078         * Determines whether the character is a superscript. Most superscripts have
079         * unicode value from \\u2070 to \\u207F inclusive. Superscripts are the
080         * following symbols:
081         *
082         * <blockquote><pre>
083         * \u2070 \u00B9 \u00B2 \u00B3 \u2074 \u2075 \u2076 \u2077 \u2078 \u2079 \u207A \u207B \u207C \u207D \u207E \u207F
084         * </pre></blockquote>
085         */
086        public static boolean isSuperScript( final char c ) {
087            switch ( c ) {
088            /*1*/case '\u2071':
089            /*2*/case '\u2072':
090            /*3*/case '\u2073':
091                return false;
092            /*1*/case '\u00B9':
093            /*2*/case '\u00B2':
094            /*3*/case '\u00B3':
095                return true;
096            }
097            return ( c >= '\u2070' && c <= '\u207F' );
098        }
099    
100        /**
101         * Determines whether the character is a subscript. Most subscripts have
102         * unicode value from \\u2080 ti \\u208E inclusive. Subscripts are the
103         * following symbols:
104         *
105         * <blockquote><pre>
106         * \u2080 \u2081 \u2082 \u2083 \u2084 \u2085 \u2086 \u2087 \u2088 \u2089 \u208A \u208B \u208C \u208D \u208E
107         * </pre></blockquote>
108         */
109        public static boolean isSubScript( final char c ) {
110            return ( c >= '\u2080' && c <= '\u208E' );
111        }
112    
113        /**
114         * Converts the character argument to superscript.
115         * Only the following characters can be converted
116         * (other characters are left unchanged):
117         *
118         * <blockquote><pre>
119         * 0 1 2 3 4 5 6 7 8 9 + - = ( ) n
120         * </pre></blockquote>
121         */
122        public static char toSuperScript( final char c ) {
123            switch ( c ) {
124            case '1':
125                return '\u00B9';
126            case '2':
127                return '\u00B2';
128            case '3':
129                return '\u00B3';
130            case '+':
131                return '\u207A';
132            case '-':
133                return '\u207B';
134            case '=':
135                return '\u207C';
136            case '(':
137                return '\u207D';
138            case ')':
139                return '\u207E';
140            case 'n':
141                return '\u207F';
142            }
143            if ( c >= '0' && c <= '9' )
144                return (char) ( c + ( '\u2070' - '0' ) );
145            return c;
146        }
147    
148        /**
149         * Converts the character argument to subscript.
150         * Only the following characters can be converted
151         * (other characters are left unchanged):
152         *
153         * <blockquote><pre>
154         * 0 1 2 3 4 5 6 7 8 9 + - = ( ) n
155         * </pre></blockquote>
156         */
157        public static char toSubScript( final char c ) {
158            switch ( c ) {
159            case '+':
160                return '\u208A';
161            case '-':
162                return '\u208B';
163            case '=':
164                return '\u208C';
165            case '(':
166                return '\u208D';
167            case ')':
168                return '\u208E';
169            }
170            if ( c >= '0' && c <= '9' )
171                return (char) ( c + ( '\u2080' - '0' ) );
172            return c;
173        }
174    
175        /**
176         * Converts the character argument to normal script.
177         */
178        public static char toNormalScript( final char c ) {
179            switch ( c ) {
180            case '\u00B9':
181                return '1';
182            case '\u00B2':
183                return '2';
184            case '\u00B3':
185                return '3';
186            case '\u2071':
187                return c;
188            case '\u2072':
189                return c;
190            case '\u2073':
191                return c;
192            case '\u207A':
193                return '+';
194            case '\u207B':
195                return '-';
196            case '\u207C':
197                return '=';
198            case '\u207D':
199                return '(';
200            case '\u207E':
201                return ')';
202            case '\u207F':
203                return 'n';
204            case '\u208A':
205                return '+';
206            case '\u208B':
207                return '-';
208            case '\u208C':
209                return '=';
210            case '\u208D':
211                return '(';
212            case '\u208E':
213                return ')';
214            }
215            if ( c >= '\u2070' && c <= '\u2079' )
216                return (char) ( c - ( '\u2070' - '0' ) );
217            if ( c >= '\u2080' && c <= '\u2089' )
218                return (char) ( c - ( '\u2080' - '0' ) );
219            return c;
220        }
221    
222        /**
223         * Returns a string of the specified length filled with white spaces.
224         * This method try to returns a pre-allocated string if possible.
225         *
226         * @param  length The string length. Negative values are clamp to 0.
227         * @return A string of length <code>length</code> filled with with spaces.
228         */
229        public static String spaces( int length ) {
230            // No need to synchronize.   In the unlikely case where two threads
231            // call this method in the same time and the two calls create a new
232            // string,  the String.intern() call will take care to canonicalize
233            // the strings.
234            final int last = spacesFactory.length - 1;
235            if ( length < 0 )
236                length = 0;
237            if ( length <= last ) {
238                if ( spacesFactory[length] == null ) {
239                    if ( spacesFactory[last] == null ) {
240                        char[] blancs = new char[last];
241                        Arrays.fill( blancs, ' ' );
242                        spacesFactory[last] = new String( blancs ).intern();
243                    }
244                    spacesFactory[length] = spacesFactory[last].substring( 0, length ).intern();
245                }
246                return spacesFactory[length];
247            } 
248            char[] blancs = new char[length];
249            Arrays.fill( blancs, ' ' );
250            return new String( blancs );
251            
252        }
253    
254        /**
255         * Returns a short class name for the specified class. This method will
256         * ommit the package name. For exemple, it will returns "String" instead
257         * of "java.lang.String" for a {@link String} object.
258         *
259         * @param  classe The object (may be <code>null</code>).
260         * @return A short class name for the specified object.
261         */
262        public static String getShortName( final Class classe ) {
263            if ( classe == null )
264                return "<*>";
265            String name = classe.getName();
266            int lower = name.lastIndexOf( '.' );
267            int upper = name.length();
268            return name.substring( lower + 1, upper ).replace( '$', '.' );
269        }
270    
271        /**
272         * Returns a short class name for the specified object. This method will
273         * ommit the package name. For exemple, it will returns "String" instead
274         * of "java.lang.String" for a {@link String} object.
275         *
276         * @param  object The object (may be <code>null</code>).
277         * @return A short class name for the specified object.
278         */
279        public static String getShortClassName( final Object object ) {
280            return getShortName( object != null ? object.getClass() : null );
281        }
282    
283        /**
284         * Convenience method for testing two objects for
285         * equality. One or both objects may be null.
286         */
287        public static boolean equals( final Object object1, final Object object2 ) {
288            return ( object1 == object2 ) || ( object1 != null && object1.equals( object2 ) );
289        }
290    
291       
292    }