View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16   
17  package org.apache.commons.beanutils.locale.converters;
18  
19  import java.sql.Timestamp;
20  import java.text.ParseException;
21  import java.util.Locale;
22  
23  /**
24   * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} 
25   * implementation that converts an incoming
26   * locale-sensitive String into a <code>java.sql.Timestamp</code> object,
27   * optionally using a default value or throwing a 
28   * {@link org.apache.commons.beanutils.ConversionException}
29   * if a conversion error occurs.</p>
30   *
31   * @author Yauheny Mikulski
32   */
33  
34  public class SqlTimestampLocaleConverter extends DateLocaleConverter {
35  
36  
37      // ----------------------------------------------------------- Constructors
38  
39      /**
40       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
41       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
42       * if a conversion error occurs. The locale is the default locale for
43       * this instance of the Java Virtual Machine and an unlocalized pattern is used
44       * for the convertion.
45       *
46       */
47      public SqlTimestampLocaleConverter() {
48  
49          this(false);
50      }
51  
52      /**
53       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
54       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
55       * if a conversion error occurs. The locale is the default locale for
56       * this instance of the Java Virtual Machine.
57       *
58       * @param locPattern    Indicate whether the pattern is localized or not
59       */
60      public SqlTimestampLocaleConverter(boolean locPattern) {
61  
62          this(Locale.getDefault(), locPattern);
63      }
64  
65      /**
66       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
67       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
68       * if a conversion error occurs. An unlocalized pattern is used for the convertion.
69       *
70       * @param locale        The locale
71       */
72      public SqlTimestampLocaleConverter(Locale locale) {
73  
74          this(locale, (String) null);
75      }
76  
77      /**
78       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
79       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
80       * if a conversion error occurs.
81       *
82       * @param locale        The locale
83       * @param locPattern    Indicate whether the pattern is localized or not
84       */
85      public SqlTimestampLocaleConverter(Locale locale, boolean locPattern) {
86  
87          this(locale, (String) null);
88      }
89  
90      /**
91       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
92       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
93       * if a conversion error occurs. An unlocalized pattern is used for the convertion.
94       *
95       * @param locale        The locale
96       * @param pattern       The convertion pattern
97       */
98      public SqlTimestampLocaleConverter(Locale locale, String pattern) {
99  
100         this(locale, pattern, false);
101     }
102 
103     /**
104      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
105      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
106      * if a conversion error occurs.
107      *
108      * @param locale        The locale
109      * @param pattern       The convertion pattern
110      * @param locPattern    Indicate whether the pattern is localized or not
111      */
112     public SqlTimestampLocaleConverter(Locale locale, String pattern, boolean locPattern) {
113 
114         super(locale, pattern, locPattern);
115     }
116 
117     /**
118      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
119      * that will return the specified default value
120      * if a conversion error occurs. The locale is the default locale for
121      * this instance of the Java Virtual Machine and an unlocalized pattern is used
122      * for the convertion.
123      *
124      * @param defaultValue  The default value to be returned
125      */
126     public SqlTimestampLocaleConverter(Object defaultValue) {
127         this(defaultValue, false);
128     }
129 
130     /**
131      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
132      * that will return the specified default value
133      * if a conversion error occurs. The locale is the default locale for
134      * this instance of the Java Virtual Machine.
135      *
136      * @param defaultValue  The default value to be returned
137      * @param locPattern    Indicate whether the pattern is localized or not
138      */
139     public SqlTimestampLocaleConverter(Object defaultValue, boolean locPattern) {
140 
141         this(defaultValue, Locale.getDefault(), locPattern);
142     }
143 
144     /**
145      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
146      * that will return the specified default value
147      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
148      *
149      * @param defaultValue  The default value to be returned
150      * @param locale        The locale
151      */
152     public SqlTimestampLocaleConverter(Object defaultValue, Locale locale) {
153 
154         this(defaultValue, locale, false);
155     }
156 
157     /**
158      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
159      * that will return the specified default value
160      * if a conversion error occurs.
161      *
162      * @param defaultValue  The default value to be returned
163      * @param locale        The locale
164      * @param locPattern    Indicate whether the pattern is localized or not
165      */
166     public SqlTimestampLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
167 
168         this(defaultValue, locale, null, locPattern);
169     }
170 
171     /**
172      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
173      * that will return the specified default value
174      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
175      *
176      * @param defaultValue  The default value to be returned
177      * @param locale        The locale
178      * @param pattern       The convertion pattern
179      */
180     public SqlTimestampLocaleConverter(Object defaultValue, Locale locale, String pattern) {
181 
182         this(defaultValue, locale, pattern, false);
183     }
184 
185     /**
186      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
187      * that will return the specified default value
188      * if a conversion error occurs.
189      *
190      * @param defaultValue  The default value to be returned
191      * @param locale        The locale
192      * @param pattern       The convertion pattern
193      * @param locPattern    Indicate whether the pattern is localized or not
194      */
195     public SqlTimestampLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
196 
197         super(defaultValue, locale, pattern, locPattern);
198     }
199 
200     // --------------------------------------------------------- Methods
201 
202     /**
203      * Convert the specified locale-sensitive input object into an output object of the
204      * specified type.
205      *
206      * @param value The input object to be converted
207      * @param pattern The pattern is used for the convertion
208      *
209      * @exception ConversionException if conversion cannot be performed
210      *  successfully
211      */
212     protected Object parse(Object value, String pattern) throws ParseException {
213 
214         return new Timestamp(((java.util.Date) super.parse(value, pattern)).getTime());
215     }
216 }