001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero 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 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.io.events;
020
021import java.nio.Buffer;
022import java.nio.ByteBuffer;
023import java.nio.CharBuffer;
024
025import org.jgrapes.io.util.ManagedBuffer;
026
027/**
028 * This event signals that a new chunk of internally generated data is to be
029 * forwarded to some destination. This type of
030 * event is commonly used for data flowing out of the application.
031 */
032public class Output<T extends Buffer> extends IOEvent<T> {
033
034    /**
035     * Create a new output event with the given buffer and optionally flips
036     * it. Used internally for constructor ("super(...)") invocations that 
037     * don't flip the buffer.
038     * 
039     * @param buffer the buffer with the data
040     * @param flip if the buffer should be flipped
041     * @param endOfRecord if the event ends a data record
042     */
043    private Output(ManagedBuffer<T> buffer, boolean flip, boolean endOfRecord) {
044        super(buffer, endOfRecord);
045        if (flip) {
046            buffer.flip();
047        }
048    }
049
050    /**
051     * Create a new event from an existing event. This constructor
052     * is useful if the data is to be forwarded to another channel
053     * by a new event.
054     * 
055     * The buffer is reused in the new event (the lock count is 
056     * incremented).
057     * 
058     * @param event the existing event
059     */
060    public Output(Output<T> event) {
061        this(event.buffer(), false, event.isEndOfRecord());
062        event.buffer().lockBuffer();
063    }
064
065    /**
066     * Create a new event with the given buffer. The buffer must
067     * have been prepared for invoking `get`-methods.
068     * 
069     * @param buffer the buffer with the data
070     * @param endOfRecord if the event ends a data record
071     */
072    public static <B extends Buffer> Output<B> fromSource(
073            ManagedBuffer<B> buffer, boolean endOfRecord) {
074        return new Output<>(buffer, false, endOfRecord);
075    }
076
077    /**
078     * Create a new event with the given buffer. Creating the event
079     * flips the buffer, which is assumed to have been used for
080     * collecting data up to now.
081     * 
082     * @param buffer the buffer with the data
083     * @param endOfRecord if the event ends a data record
084     */
085    public static <B extends Buffer> Output<B> fromSink(
086            ManagedBuffer<B> buffer, boolean endOfRecord) {
087        return new Output<>(buffer, true, endOfRecord);
088    }
089
090    /**
091     * Convenience method that creates a 
092     * {@code Output<CharBuffer>} event from a {@link String}.
093     * 
094     * @param data the string to wrap
095     * @param endOfRecord if the event ends a data record
096     * @return the event
097     */
098    public static Output<CharBuffer>
099            from(String data, boolean endOfRecord) {
100        return new Output<>(ManagedBuffer.wrap(
101            CharBuffer.wrap(data)), false, endOfRecord);
102    }
103
104    /**
105     * Convenience method that creates a 
106     * {@code Output<ByteBuffer>} event from a `byte[]`.
107     * 
108     * @param data the array to wrap
109     * @param endOfRecord if the event ends a data record
110     * @return the event
111     */
112    public static Output<ByteBuffer>
113            from(byte[] data, boolean endOfRecord) {
114        return new Output<>(ManagedBuffer.wrap(ByteBuffer.wrap(data)),
115            false, endOfRecord);
116    }
117}