/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io;

import java.io.Serializable;
import java.util.Arrays;
import net.ranides.assira.events.Event;

/**
 *
 * @author ranides
 */
@SuppressWarnings("PMD")
public abstract class IORequest implements Event, Serializable {

    private static final long serialVersionUID = 1L;

    private static final class Li { // NOPMD lazy init idiom
        static final Close CLOSE = new Close();
        static final Flush FLUSH = new Flush();
    }

    public static Close close() {
        return Li.CLOSE;
    }

    public static Flush flush() {
        return Li.FLUSH;
    }

    public static WriteByte write(int value) {
        return new WriteByte(value);
    }

    public static WriteByteArray write(byte[] array) {
        return new WriteByteArray(Arrays.copyOf(array, array.length));
    }

    public static WriteByteArray write(byte[] array, int offset, int length) {
        return new WriteByteArray(Arrays.copyOfRange(array, offset, offset + length));
    }


    public static class Close extends IORequest {

        private static final long serialVersionUID = 1L;

        protected Close() {
            // do nothing
        }

        @Override
        public String toString() {
            return "IORequest.Close";
        }
    }

    public static class Flush extends IORequest {

        private static final long serialVersionUID = 1L;

        protected Flush() {
            // do nothing
        }

        @Override
        public String toString() {
            return "IORequest.Flush";
        }
    }

    public static class WriteByte extends IORequest {

        private static final long serialVersionUID = 1L;

        private final int data;

        protected WriteByte(int data) {
            this.data = data;
        }

        public final int data() {
            return data;
        }

        @Override
        public String toString() {
            return "IORequest.WriteByte: " + data;
        }
    }

    public static class WriteByteArray extends IORequest {

        private static final long serialVersionUID = 1L;

        private final byte[] data;

        protected WriteByteArray(byte[] data) {
            this.data = data;
        }

        public byte[] data() {
            return data;
        }

        @Override
        public String toString() {
            return "IORequest.WriteByteArray: " + data.length;
        }
    }

}
