// Path: src/main/java/org/tools4j/hoverraft/command/Command.java
// public interface Command extends DirectPayload, Event {
// 
//     CommandKey commandKey();
// 
//     CommandPayload commandPayload();
// 
//     default Command sourceId(final int sourceId) {
//         commandKey().sourceId(sourceId);
//         return this;
//     }
// 
//     default Command commandIndex(final long commandIndex) {
//         commandKey().commandIndex(commandIndex);
//         return this;
//     }
// 
//     default void copyFrom(final Command command) {
//         writeBufferOrNull().putBytes(offset(), command.readBufferOrNull(), command.offset(), command.byteLength());
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onCommand(serverContext, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/CommandKey.java
// public interface CommandKey extends Comparable<CommandKey> {
//     Comparator<CommandKey> COMPARATOR = (k1, k2) -> {
//         final int sourceCompare = Integer.compare(k1.sourceId(), k2.sourceId());
//         return sourceCompare == 0 ? Long.compare(k1.commandIndex(), k2.commandIndex()) : sourceCompare;
//     };
// 
//     int sourceId();
//     CommandKey sourceId(int sourceId);
// 
//     long commandIndex();
//     CommandKey commandIndex(long commandIndex);
// 
//     @Override
//     default int compareTo(final CommandKey other) {
//         return COMPARATOR.compare(this, other);
//     }
// 
//     default boolean containedIn(final CommandLog commandLog) {
//         return commandLog.contains(this);
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/CommandPayload.java
// public interface CommandPayload {
//     int commandByteLength();
//     void bytesFrom(byte[] bytes, int offset, int length);
//     void bytesFrom(ByteBuffer bytes, int offset, int length);
//     void bytesFrom(DirectBuffer bytes, int offset, int length);
//     void bytesTo(byte[] bytes, int offset);
//     void bytesTo(ByteBuffer bytes, int offset);
//     void bytesTo(MutableDirectBuffer bytes, int offset);
//     void copyFrom(CommandPayload commandPayload);
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/direct/AbstractDirectPayload.java
// abstract public class AbstractDirectPayload implements DirectPayload {
// 
//     protected DirectBuffer readBuffer;
//     protected MutableDirectBuffer writeBuffer;
//     protected int offset;
// 
//     @Override
//     public int offset() {
//         return offset;
//     }
// 
//     @Override
//     public DirectBuffer readBufferOrNull() {
//         return readBuffer;
//     }
// 
//     @Override
//     public MutableDirectBuffer writeBufferOrNull() {
//         return writeBuffer;
//     }
// 
//     public void wrap(final DirectBuffer buffer, final int offset) {
//         this.readBuffer = Objects.requireNonNull(buffer);
//         this.writeBuffer = null;
//         this.offset = offset;
//     }
// 
//     public void wrap(final MutableDirectBuffer buffer, final int offset) {
//         this.readBuffer = Objects.requireNonNull(buffer);
//         this.writeBuffer = buffer;
//         this.offset = offset;
//     }
// 
//     public void unwrap() {
//         this.readBuffer = null;
//         this.writeBuffer = null;
//         this.offset = 0;
//     }
// 
//     protected void copyFrom(final DirectPayload directPayload) {
//         this.writeBuffer.putBytes(offset, directPayload.readBufferOrNull(), directPayload.offset(), directPayload.byteLength());
//     }
// }


import org.agrona.DirectBuffer;
import org.agrona.MutableDirectBuffer;
import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.command.CommandKey;
import org.tools4j.hoverraft.command.CommandPayload;
import org.tools4j.hoverraft.direct.AbstractDirectPayload;
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 hover-raft (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.tools4j.hoverraft.message.direct;


public final class DirectCommand extends AbstractDirectPayload implements Command {

    private static final int COMMAND_KEY_OFF = 0;
    private static final int COMMAND_KEY_LEN = DirectCommandKey.BYTE_LENGTH;
    private static final int COMMAND_PAYLOAD_OFF = COMMAND_KEY_OFF + COMMAND_KEY_LEN;

    public static final int EMPTY_COMMAND_BYTE_LENGTH = COMMAND_KEY_LEN + DirectCommandPayload.EMPTY_PAYLOAD_BYTE_LENGTH;

    private final DirectCommandKey commandKey = new DirectCommandKey();
    private final DirectCommandPayload commandPayload = new DirectCommandPayload();

    @Override
    public int byteLength() {
        return COMMAND_KEY_LEN + commandPayload.byteLength();
    }

    @Override
    public CommandKey commandKey() {
        return commandKey;
    }

    @Override
// Path: src/main/java/org/tools4j/hoverraft/command/Command.java
// public interface Command extends DirectPayload, Event {
// 
//     CommandKey commandKey();
// 
//     CommandPayload commandPayload();
// 
//     default Command sourceId(final int sourceId) {
//         commandKey().sourceId(sourceId);
//         return this;
//     }
// 
//     default Command commandIndex(final long commandIndex) {
//         commandKey().commandIndex(commandIndex);
//         return this;
//     }
// 
//     default void copyFrom(final Command command) {
//         writeBufferOrNull().putBytes(offset(), command.readBufferOrNull(), command.offset(), command.byteLength());
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onCommand(serverContext, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/CommandKey.java
// public interface CommandKey extends Comparable<CommandKey> {
//     Comparator<CommandKey> COMPARATOR = (k1, k2) -> {
//         final int sourceCompare = Integer.compare(k1.sourceId(), k2.sourceId());
//         return sourceCompare == 0 ? Long.compare(k1.commandIndex(), k2.commandIndex()) : sourceCompare;
//     };
// 
//     int sourceId();
//     CommandKey sourceId(int sourceId);
// 
//     long commandIndex();
//     CommandKey commandIndex(long commandIndex);
// 
//     @Override
//     default int compareTo(final CommandKey other) {
//         return COMPARATOR.compare(this, other);
//     }
// 
//     default boolean containedIn(final CommandLog commandLog) {
//         return commandLog.contains(this);
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/CommandPayload.java
// public interface CommandPayload {
//     int commandByteLength();
//     void bytesFrom(byte[] bytes, int offset, int length);
//     void bytesFrom(ByteBuffer bytes, int offset, int length);
//     void bytesFrom(DirectBuffer bytes, int offset, int length);
//     void bytesTo(byte[] bytes, int offset);
//     void bytesTo(ByteBuffer bytes, int offset);
//     void bytesTo(MutableDirectBuffer bytes, int offset);
//     void copyFrom(CommandPayload commandPayload);
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/direct/AbstractDirectPayload.java
// abstract public class AbstractDirectPayload implements DirectPayload {
// 
//     protected DirectBuffer readBuffer;
//     protected MutableDirectBuffer writeBuffer;
//     protected int offset;
// 
//     @Override
//     public int offset() {
//         return offset;
//     }
// 
//     @Override
//     public DirectBuffer readBufferOrNull() {
//         return readBuffer;
//     }
// 
//     @Override
//     public MutableDirectBuffer writeBufferOrNull() {
//         return writeBuffer;
//     }
// 
//     public void wrap(final DirectBuffer buffer, final int offset) {
//         this.readBuffer = Objects.requireNonNull(buffer);
//         this.writeBuffer = null;
//         this.offset = offset;
//     }
// 
//     public void wrap(final MutableDirectBuffer buffer, final int offset) {
//         this.readBuffer = Objects.requireNonNull(buffer);
//         this.writeBuffer = buffer;
//         this.offset = offset;
//     }
// 
//     public void unwrap() {
//         this.readBuffer = null;
//         this.writeBuffer = null;
//         this.offset = 0;
//     }
// 
//     protected void copyFrom(final DirectPayload directPayload) {
//         this.writeBuffer.putBytes(offset, directPayload.readBufferOrNull(), directPayload.offset(), directPayload.byteLength());
//     }
// }


// Path: src/main/java/org/tools4j/hoverraft/message/direct/DirectCommand.java
import org.agrona.DirectBuffer;
import org.agrona.MutableDirectBuffer;
import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.command.CommandKey;
import org.tools4j.hoverraft.command.CommandPayload;
import org.tools4j.hoverraft.direct.AbstractDirectPayload;

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 hover-raft (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.tools4j.hoverraft.message.direct;


public final class DirectCommand extends AbstractDirectPayload implements Command {

    private static final int COMMAND_KEY_OFF = 0;
    private static final int COMMAND_KEY_LEN = DirectCommandKey.BYTE_LENGTH;
    private static final int COMMAND_PAYLOAD_OFF = COMMAND_KEY_OFF + COMMAND_KEY_LEN;

    public static final int EMPTY_COMMAND_BYTE_LENGTH = COMMAND_KEY_LEN + DirectCommandPayload.EMPTY_PAYLOAD_BYTE_LENGTH;

    private final DirectCommandKey commandKey = new DirectCommandKey();
    private final DirectCommandPayload commandPayload = new DirectCommandPayload();

    @Override
    public int byteLength() {
        return COMMAND_KEY_LEN + commandPayload.byteLength();
    }

    @Override
    public CommandKey commandKey() {
        return commandKey;
    }

    @Override
    public CommandPayload commandPayload() {