View Javadoc

1   /*
2    * $Id: FormatHelper.java 113 2004-10-22 19:22:56Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.commons.util;
24  
25  import java.text.NumberFormat;
26  import java.text.DateFormat;
27  import java.util.Collections;
28  import java.util.HashSet;
29  import java.util.Set;
30  import java.io.File;
31  
32  /**
33   * Utility class to format values.
34   *
35   * @author  Jose M. Palomar
36   * @version $Revision: 113 $
37   * @see ParseHelper
38   */
39  public final class FormatHelper {
40  
41      // Constants
42  
43      // Fields
44      /**
45       * Singleton instance.
46       */
47      private static final FormatHelper INSTANCE = new FormatHelper();
48  
49      /**
50       * Set of classes supported by this helper.
51       */
52      private Set supportedClasses;
53  
54      /**
55       * Integer formatter.
56       */
57      private NumberFormat integerFormat;
58  
59      /**
60       * Decimal formatter.
61       */
62      private NumberFormat decimalFormat;
63  
64      /**
65       * Date and time formatter.
66       */
67      private DateFormat dateTimeFormat;
68  
69      /**
70       * Time formatter.
71       */
72      private DateFormat timeFormat;
73  
74      /**
75       * Date formatter.
76       */
77      private DateFormat dateFormat;
78  
79      // Constructors
80      /**
81       * Constructs a new <code>FormatHelper</code> object.
82       */
83      private FormatHelper() {
84  
85          this.integerFormat = NumberFormat.getNumberInstance();
86          this.integerFormat.setParseIntegerOnly(true);
87          this.decimalFormat = NumberFormat.getNumberInstance();
88          this.dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
89          this.dateTimeFormat.setLenient(false);
90          this.dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
91          this.dateFormat.setLenient(false);
92          this.timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
93          this.timeFormat.setLenient(false);
94  
95          /* Supported classes */
96          this.supportedClasses = new HashSet();
97          this.supportedClasses.add(java.lang.String.class);
98          this.supportedClasses.add(java.lang.Integer.class);
99          this.supportedClasses.add(java.lang.Long.class);
100         this.supportedClasses.add(java.lang.Float.class);
101         this.supportedClasses.add(java.lang.Double.class);
102         this.supportedClasses.add(java.lang.Boolean.class);
103         this.supportedClasses.add(java.util.Date.class);
104         this.supportedClasses.add(java.sql.Date.class);
105         this.supportedClasses.add(java.sql.Time.class);
106         this.supportedClasses.add(java.sql.Timestamp.class);
107         this.supportedClasses.add(java.io.File.class);
108         this.supportedClasses = Collections.unmodifiableSet(this.supportedClasses);
109 
110     }
111 
112     // Methods
113     /**
114      * Obtains a <code>FormatHelper</code> instance.
115      *
116      * @return FormatHelper instance.
117      */
118     public static FormatHelper getInstance() {
119         return INSTANCE;
120     }
121 
122     /**
123      * Formats the value passed using the matching formatter for its type.
124      *
125      * @param value Object The value to format.
126      * @return String Formatted string.
127      */
128     public String formatObject(Object value) {
129 
130         Class type = value.getClass();
131 
132         if (type.getName().equals("java.lang.String")) {
133             return (String) value;
134         }
135         if (type.getName().equals("java.lang.Integer")) {
136             return formatInteger(value);
137         }
138         else if (type.getName().equals("java.lang.Long")) {
139             return formatLong(value);
140         }
141         else if (type.getName().equals("java.lang.Float")) {
142             return formatFloat(value);
143         }
144         else if (type.getName().equals("java.lang.Double")) {
145             return formatDouble(value);
146         }
147         else if (type.getName().equals("java.lang.Boolean")) {
148             return formatBoolean(value);
149         }
150         else if (type.getName().equals("java.util.Date")) {
151             return formatDateTime(value);
152         }
153         else if (type.getName().equals("java.sql.Date")) {
154             return formatDate(value);
155         }
156         else if (type.getName().equals("java.sql.Time")) {
157             return formatTime(value);
158         }
159         else if (type.getName().equals("java.sql.Timestamp")) {
160             return formatDateTime(value);
161         }
162         else if (type.getName().equals("java.io.File")) {
163             return formatFile(value);
164         }
165         else {
166             return value.toString();
167         }
168 
169     }
170 
171     /**
172      * Formats value using the <code>Integer</code> format.
173      *
174      * @param value Object The value to format.
175      * @return String Formatted string.
176      */
177     public String formatInteger(Object value) {
178         return this.integerFormat.format(value);
179     }
180 
181     /**
182      * Formats value using the <code>Long</code> format.
183      *
184      * @param value Object The value to format.
185      * @return String Formatted string.
186      */
187     public String formatLong(Object value) {
188         return this.integerFormat.format(value);
189     }
190 
191     /**
192      * Formats value using the <code>Float</code> format.
193      *
194      * @param value Object The value to format.
195      * @return String Formatted string.
196      */
197     public String formatFloat(Object value) {
198         return this.decimalFormat.format(value);
199     }
200 
201     /**
202      * Formats value using the <code>Double</code> format.
203      *
204      * @param value Object The value to format.
205      * @return String Formatted string.
206      */
207     public String formatDouble(Object value) {
208         return this.decimalFormat.format(value);
209     }
210 
211     /**
212      * Formats value using the <code>Boolean</code> format.
213      *
214      * @param value Object The value to format.
215      * @return String Formatted string.
216      */
217     public String formatBoolean(Object value) {
218         return value.toString();
219     }
220 
221     /**
222      * Formats value using the date and time format.
223      *
224      * @param value Object The value to format.
225      * @return String Formatted string.
226      */
227     public String formatDateTime(Object value) {
228         return this.dateTimeFormat.format(value);
229     }
230 
231     /**
232      * Formats value using the date format.
233      *
234      * @param value Object The value to format.
235      * @return String Formatted string.
236      */
237     public String formatDate(Object value) {
238         return this.dateFormat.format(value);
239     }
240 
241     /**
242      * Formats value using the time format.
243      *
244      * @param value Object The value to format.
245      * @return String Formatted string.
246      */
247     public String formatTime(Object value) {
248         return this.timeFormat.format(value);
249     }
250 
251     /**
252      * Formats value using the <code>File</code> format.
253      *
254      * @param value Object The value to format.
255      * @return String Formatted string.
256      */
257     public String formatFile(Object value) {
258         return ((File) value).getAbsolutePath();
259     }
260 
261     /**
262      * Returns a <code>Set</code> of classes supported by this helper.
263      *
264      * @return Set of classes supported.
265      */
266     public Set getSupportedClasses() {
267         return this.supportedClasses;
268     }
269 
270 }