001/*
002 * This file is part of OCaml-Java runtime.
003 * Copyright (C) 2007-2013 Xavier Clerc.
004 *
005 * OCaml-Java runtime is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation; either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * OCaml-Java runtime is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.ocamljava.runtime.wrappers;
020
021import org.ocamljava.runtime.values.Value;
022
023/**
024 * The {@code OCamlFloat} class is the wrapper class for OCaml values of
025 * type {@code float}.
026 *
027 * @author <a href="mailto:xclerc@ocamljava.org">Xavier Clerc</a>
028 * @version 2.0
029 * @since 2.0
030 */
031public final class OCamlFloat extends OCamlValue implements OCamlNumber {
032
033    /** Wrapper for {@code OCamlFloat} values. */
034    public static final Wrapper<OCamlFloat> WRAPPER = new SimpleWrapper<OCamlFloat>() {
035
036        /**
037         * {@inheritDoc}
038         */
039        @Override
040        public OCamlFloat wrap(final Value v) {
041            return new OCamlFloat(v);
042        } // end method 'wrap(Value)'
043
044    }; // end anonymous inner-class
045
046    /**
047     * Constructs a new instance wrapping the passed value.
048     * @param v value to wrap - should not be {@code null}
049     */
050    private OCamlFloat(final Value v) {
051        super(v);
052    } // end constructor(Value)
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public Wrapper<? extends OCamlFloat> getWrapper() {
059        return OCamlFloat.WRAPPER;
060    } // end method 'getWrapper()'
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public Wrapper<? extends OCamlValue> getWrapper(final int idx) {
067        return OCamlUnit.WRAPPER;
068    } // end method 'getWrapper(int)'
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public byte byteValue() {
075        return (byte) this.value.asDouble();
076    } // end method 'byteValue()'
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public double doubleValue() {
083        return this.value.asDouble();
084    } // end method 'doubleValue()'
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public float floatValue() {
091        return (float) this.value.asDouble();
092    } // end method 'floatValue()'
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public int intValue() {
099        return (int) this.value.asDouble();
100    } // end method 'intValue()'
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public long longValue() {
107        return (long) this.value.asDouble();
108    } // end method 'longValue()'
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public short shortValue() {
115        return (short) this.value.asDouble();
116    } // end method 'shortValue()'
117
118    /**
119     * {@inheritDoc}
120     */
121    @Override
122    public int hashCode() {
123        final long bits = Double.doubleToLongBits(this.value.asDouble());
124        return (int)(bits ^ (bits >>> 32));
125    } // end method 'hashCode()'
126
127    /**
128     * {@inheritDoc}
129     */
130    @Override
131    public boolean equals(final Object obj) {
132        if (obj instanceof OCamlFloat) {
133            final OCamlFloat that = (OCamlFloat) obj;
134            return this.value.asDouble() == that.value.asDouble();
135        } else {
136            return false;
137        } // end if/else
138    } // end method 'equals(Object)'
139
140    /**
141     * {@inheritDoc}
142     */
143    @Override
144    public String toString() {
145        final StringBuilder sb = new StringBuilder();
146        sb.append("OCamlFloat(");
147        sb.append(this.value.asDouble());
148        sb.append(")");
149        return sb.toString();
150    } // end method 'toString()'
151
152    /**
153     * Constructs a new {@code float} value, and wraps it.
154     * @param v value to wrap
155     * @return a new {@code OCamlFloat} instance wrapping the passed value
156     */
157    public static OCamlFloat create(final double v) {
158        return new OCamlFloat(Value.createDouble(v));
159    } // end method 'create(double)'
160
161    /**
162     * Wraps the passed value.
163     * @param v value to wrap - should not be {@code null}
164     * @return a new {@code OCamlFloat} instance wrapping the passed value
165     */
166    public static OCamlFloat wrap(final Value v) {
167        assert v != null : "null v";
168        return new OCamlFloat(v);
169    } // end method 'wrap(Value)'
170
171    /**
172     * Returns a wrapper for {@code OCamlFloat} values.
173     * @return a wrapper for {@code OCamlFloat} values
174     */
175    public static Wrapper<? extends OCamlFloat> wrapper() {
176        return OCamlFloat.WRAPPER;
177    } // end method 'wrapper()'
178
179} // end class 'OCamlFloat'