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 OCamlNativeInt} class is the wrapper class for OCaml values of
025 * type {@code nativeint}.
026 *
027 * @author <a href="mailto:xclerc@ocamljava.org">Xavier Clerc</a>
028 * @version 2.0
029 * @since 2.0
030 */
031public final class OCamlNativeInt extends OCamlValue implements OCamlNumber {
032
033    /** Wrapper for {@code OCamlNativeInt} values. */
034    public static final Wrapper<OCamlNativeInt> WRAPPER = new SimpleWrapper<OCamlNativeInt>() {
035
036        /**
037         * {@inheritDoc}
038         */
039        @Override
040        public OCamlNativeInt wrap(final Value v) {
041            return new OCamlNativeInt(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 OCamlNativeInt(final Value v) {
051        super(v);
052    } // end constructor(Value)
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public Wrapper<? extends OCamlNativeInt> getWrapper() {
059        return OCamlNativeInt.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.asNativeInt();
076    } // end method 'byteValue()'
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public double doubleValue() {
083        return (double) this.value.asNativeInt();
084    } // end method 'doubleValue()'
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public float floatValue() {
091        return (float) this.value.asNativeInt();
092    } // end method 'floatValue()'
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public int intValue() {
099        return (int) this.value.asNativeInt();
100    } // end method 'intValue()'
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public long longValue() {
107        return this.value.asNativeInt();
108    } // end method 'longValue()'
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public short shortValue() {
115        return (short) this.value.asNativeInt();
116    } // end method 'shortValue()'
117
118    /**
119     * {@inheritDoc}
120     */
121    @Override
122    public int hashCode() {
123        return (int) this.value.asNativeInt();
124    } // end method 'hashCode()'
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public boolean equals(final Object obj) {
131        if (obj instanceof OCamlNativeInt) {
132            final OCamlNativeInt that = (OCamlNativeInt) obj;
133            return this.value.asNativeInt() == that.value.asNativeInt();
134        } else {
135            return false;
136        } // end if/else
137    } // end method 'equals(Object)'
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public String toString() {
144        final StringBuilder sb = new StringBuilder();
145        sb.append("OCamlNativeInt(");
146        sb.append(this.value.asNativeInt());
147        sb.append(")");
148        return sb.toString();
149    } // end method 'toString()'
150
151    /**
152     * Constructs a new {@code nativeint} value, and wraps it.
153     * @param v value to wrap
154     * @return a new {@code OCamlNativeInt} instance wrapping the passed value
155     */
156    public static OCamlNativeInt create(final long v) {
157        return new OCamlNativeInt(Value.createNativeInt(v));
158    } // end method 'create(long)'
159
160    /**
161     * Wraps the passed value.
162     * @param v value to wrap - should not be {@code null}
163     * @return a new {@code OCamlNativeInt} instance wrapping the passed value
164     */
165    public static OCamlNativeInt wrap(final Value v) {
166        assert v != null : "null v";
167        return new OCamlNativeInt(v);
168    } // end method 'wrap(Value)'
169
170    /**
171     * Returns a wrapper for {@code OCamlNativeInt} values.
172     * @return a wrapper for {@code OCamlNativeInt} values
173     */
174    public static Wrapper<? extends OCamlNativeInt> wrapper() {
175        return OCamlNativeInt.WRAPPER;
176    } // end method 'wrapper()'
177
178} // end class 'OCamlNativeInt'