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.configuration2.convert;
19
20 import java.util.Objects;
21
22 /**
23 * A custom {@link Number}.
24 */
25 public final class MyNumber extends Number {
26
27 private static final long serialVersionUID = 1L;
28
29 private final long value;
30
31 public MyNumber() {
32 this("0");
33 }
34
35 public MyNumber(final long value) {
36 this.value = value;
37 }
38
39 public MyNumber(final String string) {
40 value = string != null ? Long.parseLong(string) : 0;
41 }
42
43 @Override
44 public double doubleValue() {
45 return value;
46 }
47
48 @Override
49 public boolean equals(final Object obj) {
50 if (this == obj) {
51 return true;
52 }
53 if (!(obj instanceof MyNumber)) {
54 return false;
55 }
56 final MyNumber other = (MyNumber) obj;
57 return value == other.value;
58 }
59
60 @Override
61 public float floatValue() {
62 return value;
63 }
64
65 @Override
66 public int hashCode() {
67 return Objects.hash(value);
68 }
69
70 @Override
71 public int intValue() {
72 return (int) value;
73 }
74
75 @Override
76 public long longValue() {
77 return value;
78 }
79
80 @Override
81 public String toString() {
82 return Long.toString(value);
83 }
84 }