001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math3.util;
019
020 import java.io.Serializable;
021
022 import org.apache.commons.math3.exception.util.LocalizedFormats;
023 import org.apache.commons.math3.exception.MathIllegalArgumentException;
024 import org.apache.commons.math3.exception.NullArgumentException;
025
026 /**
027 * A Default NumberTransformer for java.lang.Numbers and Numeric Strings. This
028 * provides some simple conversion capabilities to turn any java.lang.Number
029 * into a primitive double or to turn a String representation of a Number into
030 * a double.
031 *
032 * @version $Id: DefaultTransformer.java 1416643 2012-12-03 19:37:14Z tn $
033 */
034 public class DefaultTransformer implements NumberTransformer, Serializable {
035
036 /** Serializable version identifier */
037 private static final long serialVersionUID = 4019938025047800455L;
038
039 /**
040 * @param o the object that gets transformed.
041 * @return a double primitive representation of the Object o.
042 * @throws NullArgumentException if Object <code>o</code> is {@code null}.
043 * @throws MathIllegalArgumentException if Object <code>o</code>
044 * cannot successfully be transformed
045 * @see <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/Transformer.html">Commons Collections Transformer</a>
046 */
047 public double transform(Object o)
048 throws NullArgumentException, MathIllegalArgumentException {
049
050 if (o == null) {
051 throw new NullArgumentException(LocalizedFormats.OBJECT_TRANSFORMATION);
052 }
053
054 if (o instanceof Number) {
055 return ((Number)o).doubleValue();
056 }
057
058 try {
059 return Double.valueOf(o.toString()).doubleValue();
060 } catch (NumberFormatException e) {
061 throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_TRANSFORM_TO_DOUBLE,
062 o.toString());
063 }
064 }
065
066 /** {@inheritDoc} */
067 @Override
068 public boolean equals(Object other) {
069 if (this == other) {
070 return true;
071 }
072 return other instanceof DefaultTransformer;
073 }
074
075 /** {@inheritDoc} */
076 @Override
077 public int hashCode() {
078 // some arbitrary number ...
079 return 401993047;
080 }
081
082 }