LongLocaleConverter.java

  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.  *      http://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. package org.apache.commons.beanutils2.locale.converters;

  18. import java.text.ParseException;
  19. import java.util.Locale;

  20. import org.apache.commons.beanutils2.ConversionException;

  21. /**
  22.  * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a {@link Long}
  23.  * object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion error occurs.
  24.  */
  25. public class LongLocaleConverter extends DecimalLocaleConverter<Long> {

  26.     /**
  27.      * Builds instances of {@link ByteLocaleConverter}.
  28.      */
  29.     public static class Builder extends DecimalLocaleConverter.Builder<Builder, Long> {

  30.         /**
  31.          * Constructs a new instance.
  32.          */
  33.         public Builder() {
  34.             // empty
  35.         }

  36.         @Override
  37.         public LongLocaleConverter get() {
  38.             return new LongLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
  39.         }

  40.     }

  41.     /**
  42.      * Constructs a new builder.
  43.      *
  44.      * @return a new builder.
  45.      */
  46.     public static Builder builder() {
  47.         return new Builder();
  48.     }

  49.     private LongLocaleConverter(final Long defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
  50.         super(defaultValue, locale, pattern, useDefault, locPattern);
  51.     }

  52.     /**
  53.      * Parses the specified locale-sensitive input object into an output object of the specified type. This method will return a Long type.
  54.      *
  55.      * @param value   The input object to be converted
  56.      * @param pattern The pattern is used for the conversion
  57.      * @return The converted value
  58.      * @throws ConversionException if conversion cannot be performed successfully
  59.      * @throws ParseException      if an error occurs parsing a String to a Number
  60.      * @since 1.8.0
  61.      */
  62.     @Override
  63.     protected Long parse(final Object value, final String pattern) throws ParseException {
  64.         final Number result = super.parse(value, pattern);

  65.         if (result == null || result instanceof Long) {
  66.             return (Long) result;
  67.         }

  68.         return Long.valueOf(result.longValue());
  69.     }
  70. }