View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.bean.conversion;
12  
13  import static org.csveed.bean.conversion.ConversionUtil.hasText;
14  
15  import java.text.NumberFormat;
16  
17  /**
18   * The Class CustomNumberConverter.
19   */
20  public class CustomNumberConverter extends AbstractConverter<Number> {
21  
22      /** The number class. */
23      private final Class<? extends Number> numberClass;
24  
25      /** The number format. */
26      private final NumberFormat numberFormat;
27  
28      /** The allow empty. */
29      private final boolean allowEmpty;
30  
31      /**
32       * Instantiates a new custom number converter.
33       *
34       * @param numberClass
35       *            the number class
36       * @param allowEmpty
37       *            the allow empty
38       *
39       * @throws IllegalArgumentException
40       *             the illegal argument exception
41       */
42      public CustomNumberConverter(Class<? extends Number> numberClass, boolean allowEmpty)
43              throws IllegalArgumentException {
44          this(numberClass, null, allowEmpty);
45      }
46  
47      /**
48       * Instantiates a new custom number converter.
49       *
50       * @param numberClass
51       *            the number class
52       * @param numberFormat
53       *            the number format
54       * @param allowEmpty
55       *            the allow empty
56       *
57       * @throws IllegalArgumentException
58       *             the illegal argument exception
59       */
60      public CustomNumberConverter(Class<? extends Number> numberClass, NumberFormat numberFormat, boolean allowEmpty)
61              throws IllegalArgumentException {
62          super(Number.class);
63  
64          if (numberClass == null || !Number.class.isAssignableFrom(numberClass)) {
65              throw new IllegalArgumentException("Property class must be a subclass of Number");
66          }
67          this.numberClass = numberClass;
68          this.numberFormat = numberFormat;
69          this.allowEmpty = allowEmpty;
70      }
71  
72      @Override
73      public Number fromString(String text) throws Exception {
74          if (this.allowEmpty && !hasText(text)) {
75              return null;
76          }
77          if (this.numberFormat != null) {
78              return determineValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
79          }
80          return determineValue(NumberUtils.parseNumber(text, this.numberClass));
81      }
82  
83      /**
84       * Determine value.
85       *
86       * @param value
87       *            the value
88       *
89       * @return the number
90       */
91      public Number determineValue(Object value) {
92          if (value instanceof Number) {
93              return NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass);
94          }
95          return null;
96      }
97  
98      @Override
99      public String toString(Number value) throws Exception {
100         if (value == null) {
101             return "";
102         }
103         if (this.numberFormat != null) {
104             return this.numberFormat.format(value);
105         }
106         return value.toString();
107     }
108 
109 }