KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > containers > NumberFormats


1 package org.jahia.data.containers;
2
3 import java.util.List JavaDoc;
4 import java.util.Arrays JavaDoc;
5
6 /**
7  * Created by IntelliJ IDEA.
8  * User: hollis
9  * Date: 28 avr. 2005
10  * Time: 11:56:54
11  * To change this template use File | Settings | File Templates.
12  */

13 public class NumberFormats {
14
15     public static final String JavaDoc BYTE_FORMAT = "Byte";
16     public static final String JavaDoc SHORT_FORMAT = "Short";
17     public static final String JavaDoc INTEGER_FORMAT = "Integer";
18     public static final String JavaDoc LONG_FORMAT = "Long";
19     public static final String JavaDoc FLOAT_FORMAT = "Float";
20     public static final String JavaDoc DOUBLE_FORMAT = "Double";
21
22     private static final String JavaDoc[] formats = {BYTE_FORMAT,SHORT_FORMAT,INTEGER_FORMAT,
23                                             LONG_FORMAT,FLOAT_FORMAT,DOUBLE_FORMAT};
24
25     private static List JavaDoc formatsList;
26
27     static {
28         formatsList = Arrays.asList(formats);
29     }
30
31     /**
32      * Return true if the format is the one of the above
33      *
34      * @param format
35      * @return
36      */

37     public static boolean isValidFormat(String JavaDoc format){
38         if ( format == null ){
39             return false;
40         }
41         return formatsList.contains(format);
42     }
43
44     /**
45      *
46      * @param v1
47      * @param v2
48      * @param format
49      * @return
50      */

51     public static int compareNumber(String JavaDoc v1,
52                                     String JavaDoc v2,
53                                     String JavaDoc format){
54        int result = 0;
55        if ( BYTE_FORMAT.equals(format) ){
56            return compareByte(v1,v2);
57        } else if ( SHORT_FORMAT.equals(format) ){
58            return compareShort(v1,v2);
59        } else if ( INTEGER_FORMAT.equals(format) ){
60            return compareInteger(v1,v2);
61        } else if ( LONG_FORMAT.equals(format) ){
62            return compareLong(v1,v2);
63        } else if ( FLOAT_FORMAT.equals(format) ){
64            return compareFloat(v1,v2);
65        } else if ( DOUBLE_FORMAT.equals(format) ){
66            return compareDouble(v1,v2);
67        }
68        return result;
69     }
70
71
72    public static int compareByte(String JavaDoc v1, String JavaDoc v2){
73         int result = 0;
74         byte n1 = 0;
75         byte n2 = 0;
76
77         try {
78             n1 = Byte.parseByte(v1);
79         } catch ( Throwable JavaDoc t){
80             n1 = Byte.MIN_VALUE;
81         }
82         try {
83             n2 = Byte.parseByte(v2);
84         } catch ( Throwable JavaDoc t){
85             n2 = Byte.MIN_VALUE;
86         }
87         if ( n1 == n2 ){
88             return 0;
89         } else if ( n1 > n2 ){
90             return 1;
91         }
92         return -1;
93     }
94
95     public static int compareShort(String JavaDoc v1, String JavaDoc v2){
96         int result = 0;
97         short n1 = 0;
98         short n2 = 0;
99
100         try {
101             n1 = Short.parseShort(v1);
102         } catch ( Throwable JavaDoc t){
103             n1 = Short.MIN_VALUE;
104         }
105         try {
106             n2 = Short.parseShort(v2);
107         } catch ( Throwable JavaDoc t){
108             n2 = Short.MIN_VALUE;
109         }
110         if ( n1 == n2 ){
111             return 0;
112         } else if ( n1 > n2 ){
113             return 1;
114         }
115         return -1;
116     }
117
118     public static int compareInteger(String JavaDoc v1, String JavaDoc v2){
119         int result = 0;
120         int n1 = 0;
121         int n2 = 0;
122
123         try {
124             n1 = Integer.parseInt(v1);
125         } catch ( Throwable JavaDoc t){
126             n1 = Integer.MIN_VALUE;
127         }
128         try {
129             n2 = Integer.parseInt(v2);
130         } catch ( Throwable JavaDoc t){
131             n2 = Integer.MIN_VALUE;
132         }
133         if ( n1 == n2 ){
134             return 0;
135         } else if ( n1 > n2 ){
136             return 1;
137         }
138         return -1;
139     }
140
141     public static int compareLong(String JavaDoc v1, String JavaDoc v2){
142         int result = 0;
143         long n1 = 0;
144         long n2 = 0;
145
146         try {
147             n1 = Long.parseLong(v1);
148         } catch ( Throwable JavaDoc t){
149             n1 = Long.MIN_VALUE;
150         }
151         try {
152             n2 = Long.parseLong(v2);
153         } catch ( Throwable JavaDoc t){
154             n2 = Long.MIN_VALUE;
155         }
156         if ( n1 == n2 ){
157             return 0;
158         } else if ( n1 > n2 ){
159             return 1;
160         }
161         return -1;
162     }
163
164     public static int compareFloat(String JavaDoc v1, String JavaDoc v2){
165         int result = 0;
166         float n1 = 0;
167         float n2 = 0;
168
169         try {
170             n1 = Float.parseFloat(v1);
171         } catch ( Throwable JavaDoc t){
172             n1 = Float.MIN_VALUE;
173         }
174         try {
175             n2 = Float.parseFloat(v2);
176         } catch ( Throwable JavaDoc t){
177             n2 = Float.MIN_VALUE;
178         }
179         if ( n1 == n2 ){
180             return 0;
181         } else if ( n1 > n2 ){
182             return 1;
183         }
184         return -1;
185     }
186
187     public static int compareDouble(String JavaDoc v1, String JavaDoc v2){
188         int result = 0;
189         double n1 = 0;
190         double n2 = 0;
191
192         try {
193             n1 = Double.parseDouble(v1);
194         } catch ( Throwable JavaDoc t){
195             n1 = Double.MIN_VALUE;
196         }
197         try {
198             n2 = Double.parseDouble(v2);
199         } catch ( Throwable JavaDoc t){
200             n2 = Double.MIN_VALUE;
201         }
202         if ( n1 == n2 ){
203             return 0;
204         } else if ( n1 > n2 ){
205             return 1;
206         }
207         return -1;
208     }
209
210 }
211
Popular Tags