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