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.locale.converters;
19
20 import java.text.ParseException;
21 import java.util.Locale;
22
23 import org.apache.commons.beanutils2.ConversionException;
24
25 /**
26 * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a {@link Byte}
27 * object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion error occurs.
28 */
29 public class ByteLocaleConverter extends DecimalLocaleConverter<Byte> {
30
31 /**
32 * Builds instances of {@link ByteLocaleConverter}.
33 */
34 public static class Builder extends DecimalLocaleConverter.Builder<Builder, Byte> {
35
36 /**
37 * Constructs a new instance.
38 */
39 public Builder() {
40 // empty
41 }
42
43 @Override
44 public ByteLocaleConverter get() {
45 return new ByteLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
46 }
47
48 }
49
50 /**
51 * Constructs a new builder.
52 *
53 * @return a new builder.
54 */
55 public static Builder builder() {
56 return new Builder();
57 }
58
59 private ByteLocaleConverter(final Byte defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
60 super(defaultValue, locale, pattern, useDefault, locPattern);
61 }
62
63 /**
64 * Parses the specified locale-sensitive input object into an output object of the specified type. This method will return values of type Byte.
65 *
66 * @param value The input object to be converted
67 * @param pattern The pattern is used for the conversion
68 * @return The converted value
69 * @throws ConversionException if conversion cannot be performed successfully
70 * @throws ParseException if an error occurs parsing a String to a Number
71 */
72 @Override
73 protected Byte parse(final Object value, final String pattern) throws ParseException {
74 final Number parsed = super.parse(value, pattern);
75 if (parsed.longValue() != parsed.byteValue()) {
76 throw new ConversionException("Supplied number is not of type Byte: " + parsed.longValue());
77 }
78 // now returns property Byte
79 return Byte.valueOf(parsed.byteValue());
80 }
81 }