View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils2.sql.converters.locale;
19  
20  import java.sql.Time;
21  import java.text.ParseException;
22  import java.util.Locale;
23  
24  import org.apache.commons.beanutils2.ConversionException;
25  import org.apache.commons.beanutils2.locale.converters.DateLocaleConverter;
26  
27  /**
28   * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a
29   * {@link java.sql.Time} object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion error
30   * occurs.
31   */
32  public class SqlTimeLocaleConverter extends DateLocaleConverter<Time> {
33  
34      /**
35       * Builds instances of {@link SqlTimeLocaleConverter}.
36       */
37      public static class Builder extends DateLocaleConverter.Builder<Builder, Time> {
38  
39          /**
40           * Constructs a new instance.
41           */
42          public Builder() {
43              // empty
44          }
45  
46          @Override
47          public SqlTimeLocaleConverter get() {
48              return new SqlTimeLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern, isLenient());
49          }
50  
51      }
52  
53      /**
54       * Constructs a new builder.
55       *
56       * @return a new builder.
57       */
58      public static Builder builder() {
59          return new Builder();
60      }
61  
62      private SqlTimeLocaleConverter(final Time defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern,
63              final boolean lenient) {
64          super(defaultValue, locale, pattern, useDefault, locPattern, lenient);
65      }
66  
67      /**
68       * Converts the specified locale-sensitive input object into an output object of the specified type.
69       *
70       * @param value   The input object to be converted
71       * @param pattern The pattern is used for the conversion
72       * @return The converted value
73       * @throws ConversionException if conversion cannot be performed successfully
74       * @throws ParseException      if an error occurs parsing a String to a Number
75       */
76      @Override
77      protected Time parse(final Object value, final String pattern) throws ParseException {
78          // MUST cast to java.util.Date to avoid a CCE.
79          return new Time(((java.util.Date) super.parse(value, pattern)).getTime());
80      }
81  }