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
018package org.apache.commons.math3.util;
019
020import java.io.Serializable;
021
022import org.apache.commons.math3.exception.util.LocalizedFormats;
023import org.apache.commons.math3.exception.MathIllegalArgumentException;
024import 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 */
033public class DefaultTransformer implements NumberTransformer, Serializable {
034
035    /** Serializable version identifier */
036    private static final long serialVersionUID = 4019938025047800455L;
037
038    /**
039     * @param o  the object that gets transformed.
040     * @return a double primitive representation of the Object o.
041     * @throws NullArgumentException if Object <code>o</code> is {@code null}.
042     * @throws MathIllegalArgumentException if Object <code>o</code>
043     * cannot successfully be transformed
044     * @see <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/Transformer.html">Commons Collections Transformer</a>
045     */
046    public double transform(Object o)
047        throws NullArgumentException, MathIllegalArgumentException {
048
049        if (o == null) {
050            throw new NullArgumentException(LocalizedFormats.OBJECT_TRANSFORMATION);
051        }
052
053        if (o instanceof Number) {
054            return ((Number)o).doubleValue();
055        }
056
057        try {
058            return Double.parseDouble(o.toString());
059        } catch (NumberFormatException e) {
060            throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_TRANSFORM_TO_DOUBLE,
061                                                   o.toString());
062        }
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public boolean equals(Object other) {
068        if (this == other) {
069            return true;
070        }
071        return other instanceof DefaultTransformer;
072    }
073
074    /** {@inheritDoc} */
075    @Override
076    public int hashCode() {
077        // some arbitrary number ...
078        return 401993047;
079    }
080
081}