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