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.math.util;
019    
020    import java.io.Serializable;
021    
022    import org.apache.commons.math.MathException;
023    import org.apache.commons.math.exception.util.LocalizedFormats;
024    import org.apache.commons.math.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 1131229 2011-06-03 20:49:25Z luc $
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 MathException if it cannot successfully be transformed.
043         * @throws NullArgumentException if is {@code null}.
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) throws MathException {
047            if (o == null) {
048                throw new NullArgumentException(LocalizedFormats.OBJECT_TRANSFORMATION);
049            }
050    
051            if (o instanceof Number) {
052                return ((Number)o).doubleValue();
053            }
054    
055            try {
056                return Double.valueOf(o.toString()).doubleValue();
057            } catch (NumberFormatException e) {
058                throw new MathException(e,
059                                        LocalizedFormats.CANNOT_TRANSFORM_TO_DOUBLE, e.getMessage());
060            }
061        }
062    
063        /** {@inheritDoc} */
064        @Override
065        public boolean equals(Object other) {
066            if (this == other) {
067                return true;
068            }
069            if (other == null) {
070                return false;
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    }