001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.beanutils2.sql.converters.locale; 019 020import java.sql.Time; 021import java.text.ParseException; 022import java.util.Locale; 023 024import org.apache.commons.beanutils2.ConversionException; 025import org.apache.commons.beanutils2.locale.converters.DateLocaleConverter; 026 027/** 028 * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a 029 * {@link java.sql.Time} object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion error 030 * occurs. 031 */ 032public class SqlTimeLocaleConverter extends DateLocaleConverter<Time> { 033 034 /** 035 * Builds instances of {@link SqlTimeLocaleConverter}. 036 */ 037 public static class Builder extends DateLocaleConverter.Builder<Builder, Time> { 038 039 /** 040 * Constructs a new instance. 041 */ 042 public Builder() { 043 // empty 044 } 045 046 @Override 047 public SqlTimeLocaleConverter get() { 048 return new SqlTimeLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern, isLenient()); 049 } 050 051 } 052 053 /** 054 * Constructs a new builder. 055 * 056 * @return a new builder. 057 */ 058 public static Builder builder() { 059 return new Builder(); 060 } 061 062 private SqlTimeLocaleConverter(final Time defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern, 063 final boolean lenient) { 064 super(defaultValue, locale, pattern, useDefault, locPattern, lenient); 065 } 066 067 /** 068 * Converts the specified locale-sensitive input object into an output object of the specified type. 069 * 070 * @param value The input object to be converted 071 * @param pattern The pattern is used for the conversion 072 * @return The converted value 073 * @throws ConversionException if conversion cannot be performed successfully 074 * @throws ParseException if an error occurs parsing a String to a Number 075 */ 076 @Override 077 protected Time parse(final Object value, final String pattern) throws ParseException { 078 // MUST cast to java.util.Date to avoid a CCE. 079 return new Time(((java.util.Date) super.parse(value, pattern)).getTime()); 080 } 081}