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 OCamlValue} class is the superclass of all classes wrapping
025 * OCaml values.
026 *
027 * @author <a href="mailto:xclerc@ocamljava.org">Xavier Clerc</a>
028 * @version 2.0
029 * @since 2.0
030 */
031public class OCamlValue {
032
033    /** Wrapper for {@code OCamlValue} values. */
034    public static final Wrapper<OCamlValue> WRAPPER = new SimpleWrapper<OCamlValue>() {
035
036        /**
037         * {@inheritDoc}
038         */
039        @Override
040        public OCamlValue wrap(final Value v) {
041            return new OCamlValue(v);
042        } // end method 'wrap(Value)'
043
044    }; // end anonymous inner-class
045
046    /** Wrapped value. */
047    protected final Value value;
048
049    /**
050     * Constructs a new instance wrapping the passed value.
051     * @param v value to wrap - should not be {@code null}
052     */
053    protected OCamlValue(final Value v) {
054        assert v != null : "null v";
055        this.value = v;
056    } // end constructor(Value)
057
058    /**
059     * Returns the wrapper used to wrap {@code Value} elements into
060     * instances of the current class.
061     * @return the wrapper used to wrap {@code Value} elements into
062     *         instances of the current class
063     */
064    public Wrapper<? extends OCamlValue> getWrapper() {
065        return OCamlValue.WRAPPER;
066    } // end method 'getWrapper()'
067
068    /**
069     * Returns the wrapper used to wrap {@code Value} elements into
070     * instances of the embedded type at given index.
071     * @param idx index of embedded type
072     * @return the wrapper used to wrap {@code Value} elements into
073     *         instances of the embedded type at given index.
074     */
075    public Wrapper<? extends OCamlValue> getWrapper(final int idx) {
076        return OCamlUnit.WRAPPER;
077    } // end method 'getWrapper(int)'
078
079    /**
080     * Returns the wrapped value.
081     * @return the wrapped value
082     */
083    public Value value() {
084        return this.value;
085    } // end method 'value()'
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public int hashCode() {
092        return super.hashCode();
093    } // end method 'hashCode()'
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public boolean equals(final Object obj) {
100        if (this.getClass().equals(obj.getClass())) {
101            final OCamlValue that = (OCamlValue) obj;
102            return this == that;
103        } else {
104            return false;
105        } // end if/else
106    } // end method 'equals(Object)'
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public String toString() {
113        return super.toString();
114    } // end method 'toString()'
115
116    /**
117     * Wraps the passed value.
118     * @param v value to wrap - should not be {@code null}
119     * @return a new {@code OCamlValue} instance wrapping the passed value
120     */
121    public static OCamlValue wrap(final Value v) {
122        assert v != null : "null v";
123        return new OCamlValue(v);
124    } // end method 'wrap(Value)'
125
126    /**
127     * Returns a wrapper for {@code OCamlValue} values.
128     * @return a wrapper for {@code OCamlValue} values
129     */
130    public static Wrapper<? extends OCamlValue> wrapper() {
131        return OCamlValue.WRAPPER;
132    } // end method 'wrapper()'
133
134} // end class 'OCamlValue'