Search in sources :

Example 1 with CommandTranscoder

use of com.canoo.dp.impl.remoting.codec.encoders.CommandTranscoder in project dolphin-platform by canoo.

the class OptimizedJsonCodec method encode.

@Override
@SuppressWarnings("unchecked")
public String encode(final List<? extends Command> commands) {
    Assert.requireNonNull(commands, "commands");
    LOG.debug("Encoding command list with {} commands", commands.size());
    final StringBuilder builder = new StringBuilder("[");
    for (final Command command : commands) {
        if (command == null) {
            throw new IllegalArgumentException("Command list contains a null command: " + command);
        } else {
            LOG.trace("Encoding command of type {}", command.getClass());
            final CommandTranscoder encoder = transcoders.get(command.getId());
            if (encoder == null) {
                throw new RuntimeException("No encoder for command type " + command.getClass() + " found");
            }
            final JsonObject jsonObject = encoder.encode(command);
            GSON.toJson(jsonObject, builder);
            builder.append(",");
        }
    }
    if (!commands.isEmpty()) {
        final int length = builder.length();
        builder.delete(length - 1, length);
    }
    builder.append("]");
    if (LOG.isTraceEnabled()) {
        LOG.trace("Encoded message: {}", builder.toString());
    }
    return builder.toString();
}
Also used : Command(com.canoo.dp.impl.remoting.legacy.communication.Command) JsonObject(com.google.gson.JsonObject) AbstractCommandTranscoder(com.canoo.dp.impl.remoting.codec.encoders.AbstractCommandTranscoder) CommandTranscoder(com.canoo.dp.impl.remoting.codec.encoders.CommandTranscoder)

Aggregations

AbstractCommandTranscoder (com.canoo.dp.impl.remoting.codec.encoders.AbstractCommandTranscoder)1 CommandTranscoder (com.canoo.dp.impl.remoting.codec.encoders.CommandTranscoder)1 Command (com.canoo.dp.impl.remoting.legacy.communication.Command)1 JsonObject (com.google.gson.JsonObject)1