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.locale.converters;
019
020import java.text.ParseException;
021import java.util.Locale;
022
023import org.apache.commons.beanutils2.ConversionException;
024
025/**
026 * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a {@link Short}
027 * object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion error occurs.
028 */
029public class ShortLocaleConverter extends DecimalLocaleConverter<Short> {
030
031    /**
032     * Builds instances of {@link ByteLocaleConverter}.
033     */
034    public static class Builder extends DecimalLocaleConverter.Builder<Builder, Short> {
035
036        /**
037         * Constructs a new instance.
038         */
039        public Builder() {
040            // empty
041        }
042
043        /**
044         * Gets a new instance.
045         *
046         * @return a new instance.
047         */
048        @Override
049        public ShortLocaleConverter get() {
050            return new ShortLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
051        }
052
053    }
054
055    /**
056     * Constructs a new builder.
057     *
058     * @return a new builder.
059     */
060    public static Builder builder() {
061        return new Builder();
062    }
063
064    private ShortLocaleConverter(final Short defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
065        super(defaultValue, locale, pattern, useDefault, locPattern);
066    }
067
068    /**
069     * Parses the specified locale-sensitive input object into an output object of the specified type. This method will return values of type Short.
070     *
071     * @param value   The input object to be converted
072     * @param pattern The pattern is used for the conversion
073     * @return The converted value
074     * @throws ConversionException if conversion cannot be performed successfully
075     * @throws ParseException      if an error occurs parsing a String to a Number
076     * @since 1.8.0
077     */
078    @Override
079    protected Short parse(final Object value, final String pattern) throws ParseException {
080        final Object result = super.parse(value, pattern);
081
082        if (result == null || result instanceof Short) {
083            return (Short) result;
084        }
085
086        final Number parsed = (Number) result;
087        if (parsed.longValue() != parsed.shortValue()) {
088            throw new ConversionException("Supplied number is not of type Short: " + parsed.longValue());
089        }
090
091        // now returns property Short
092        return Short.valueOf(parsed.shortValue());
093    }
094
095}