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 org.jgrapes.io.util.ManagedBuffer;
023
024/**
025 * This event signals that a new chunk of data has successfully been obtained
026 * from some source. This type of event is commonly
027 * used for data flowing into the application.
028 * 
029 * @param <T> the type of data used in this event
030 */
031public final class Input<T extends Buffer> extends IOEvent<T> {
032
033    private Input(ManagedBuffer<T> buffer, boolean endOfRecord) {
034        super(buffer, endOfRecord);
035    }
036
037    /**
038     * Create a new event with the given buffer. The buffer must
039     * have been prepared for invoking `get`-methods.
040     * 
041     * @param buffer the buffer with the data
042     * @param endOfRecord if the event ends a data record
043     */
044    public static <B extends Buffer> Input<B> fromSource(
045            ManagedBuffer<B> buffer, boolean endOfRecord) {
046        return new Input<>(buffer, endOfRecord);
047    }
048
049    /**
050     * Create a new event with the given buffer. Creating the event
051     * flips the buffer, which is assumed to have been used for
052     * collecting data up to now.
053     * 
054     * @param buffer the buffer with the data
055     * @param endOfRecord if the event ends a data record
056     */
057    public static <B extends Buffer> Input<B> fromSink(
058            ManagedBuffer<B> buffer, boolean endOfRecord) {
059        buffer.flip();
060        return new Input<>(buffer, endOfRecord);
061    }
062}