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 |
|
---|
18 | package agents.anac.y2019.harddealer.math3.util;
|
---|
19 |
|
---|
20 | import java.io.Serializable;
|
---|
21 |
|
---|
22 | import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
|
---|
23 | import agents.anac.y2019.harddealer.math3.exception.MathIllegalArgumentException;
|
---|
24 | import agents.anac.y2019.harddealer.math3.exception.NullArgumentException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * A Default NumberTransformer for java.lang.Numbers and Numeric Strings. This
|
---|
28 | * provides some simple conversion capabilities to turn any java.lang.Number
|
---|
29 | * into a primitive double or to turn a String representation of a Number into
|
---|
30 | * a double.
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class DefaultTransformer implements NumberTransformer, Serializable {
|
---|
34 |
|
---|
35 | /** Serializable version identifier */
|
---|
36 | private static final long serialVersionUID = 4019938025047800455L;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * @param o the object that gets transformed.
|
---|
40 | * @return a double primitive representation of the Object o.
|
---|
41 | * @throws NullArgumentException if Object <code>o</code> is {@code null}.
|
---|
42 | * @throws MathIllegalArgumentException if Object <code>o</code>
|
---|
43 | * cannot successfully be transformed
|
---|
44 | * @see <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/Transformer.html">Commons Collections Transformer</a>
|
---|
45 | */
|
---|
46 | public double transform(Object o)
|
---|
47 | throws NullArgumentException, MathIllegalArgumentException {
|
---|
48 |
|
---|
49 | if (o == null) {
|
---|
50 | throw new NullArgumentException(LocalizedFormats.OBJECT_TRANSFORMATION);
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (o instanceof Number) {
|
---|
54 | return ((Number)o).doubleValue();
|
---|
55 | }
|
---|
56 |
|
---|
57 | try {
|
---|
58 | return Double.parseDouble(o.toString());
|
---|
59 | } catch (NumberFormatException e) {
|
---|
60 | throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_TRANSFORM_TO_DOUBLE,
|
---|
61 | o.toString());
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** {@inheritDoc} */
|
---|
66 | @Override
|
---|
67 | public boolean equals(Object other) {
|
---|
68 | if (this == other) {
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 | return other instanceof DefaultTransformer;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** {@inheritDoc} */
|
---|
75 | @Override
|
---|
76 | public int hashCode() {
|
---|
77 | // some arbitrary number ...
|
---|
78 | return 401993047;
|
---|
79 | }
|
---|
80 |
|
---|
81 | }
|
---|