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