BigIntegerPropertyEditor.java
01 /*
02  * Copyright 2008-2017 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.core.editors;
17 
18 import griffon.core.formatters.BigIntegerFormatter;
19 import griffon.core.formatters.Formatter;
20 
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 
24 import static griffon.util.GriffonNameUtils.isBlank;
25 
26 /**
27  @author Andres Almiray
28  @since 2.0.0
29  */
30 public class BigIntegerPropertyEditor extends AbstractPropertyEditor {
31     @Override
32     protected void setValueInternal(Object value) {
33         if (null == value) {
34             super.setValueInternal(null);
35         else if (value instanceof CharSequence) {
36             handleAsString(String.valueOf(value));
37         else if (value instanceof Number) {
38             handleAsNumber((Numbervalue);
39         else {
40             throw illegalValue(value, BigInteger.class);
41         }
42     }
43 
44     protected void handleAsString(String str) {
45         try {
46             super.setValueInternal(isBlank(strnull new BigInteger(str));
47         catch (NumberFormatException e) {
48             throw illegalValue(str, BigInteger.class, e);
49         }
50     }
51 
52     protected void handleAsNumber(Number number) {
53         if (number instanceof BigDecimal) {
54             super.setValueInternal(((BigDecimalnumber).toBigInteger());
55         else if (number instanceof BigInteger) {
56             super.setValueInternal(number);
57         else {
58             super.setValueInternal(number.longValue());
59         }
60     }
61 
62     @Override
63     protected Formatter<BigInteger> resolveFormatter() {
64         return isBlank(getFormat()) null new BigIntegerFormatter(getFormat());
65     }
66 }