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