All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javonet.utils.Command Maven / Gradle / Ivy

Go to download

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. It works on Linux/Windows and MacOS for applications created in JVM, CLR/Netcore, Perl, Python, Ruby, NodeJS, C++ or GoLang and gives you unparalleled freedom and flexibility with native performance in building your mixed-technologies products. Let it be accessing best AI or cryptography libraries, devices SDKs, legacy client modules, internal custom packages or anything from public repositories available on NPM, Nuget, PyPI, Maven/Gradle, RubyGems or GitHub. Get free from programming languages barriers today! For more information check out our guides at https://www.javonet.com/guides/v2/

There is a newer version: 2.4.5
Show newest version
package com.javonet.utils;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;

public class Command {


    private final RuntimeName runtimeName;
    private final CommandType commandType;
    private final Object[] payload;

    public Command(RuntimeName runtimeName, CommandType commandType, Object... payload) {
        this.runtimeName = runtimeName;
        this.commandType = commandType;
        this.payload = payload;
    }

    public int getPayloadSize() {
        return payload.length;
    }

    public String payloadToString() {
        StringBuilder sb = new StringBuilder();
        for (Object payloadItem : this.payload) {
            sb.append((String) payloadItem);
            sb.append(System.lineSeparator());
        }
        return sb.toString();
    }

    public String toString() {
        return this.runtimeName + " " + this.commandType + " " + Arrays.toString(this.payload);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Command that = (Command) o;
        return runtimeName == that.runtimeName && commandType == that.commandType && Arrays.equals(payload, that.payload);
    }

    public static Command createResponse(Object response, RuntimeName runtimeName) {
        return new Command(runtimeName, CommandType.VALUE, new Object[]{response});
    }

    public static Command createReference(Object response, RuntimeName runtimeName) {
        return new Command(runtimeName, CommandType.REFERENCE, new Object[]{response});
    }

    public static Command createArrayResponse(Object[] response, RuntimeName runtimeName) {
        return new Command(runtimeName, CommandType.ARRAY, response);
    }

    @Override
    public int hashCode() {
        return 31 * (Objects.hash(runtimeName, commandType) + Arrays.hashCode(getHashablePayload()));
    }

    private Object[] getHashablePayload() {
        switch (commandType) {
            case VALUE:
            case LOAD_LIBRARY:
            case CREATE_INSTANCE:
            case GET_TYPE:
                return new Object[]{payload[0]};
            case GET_STATIC_FIELD:
            case SET_STATIC_FIELD:
            case GET_MODULE:
            case INVOKE_STATIC_METHOD:
            case INVOKE_INSTANCE_METHOD:
                return new Object[]{payload[0], payload[1]};
            default:
                throw new RuntimeException(commandType + " payload size not implemented");
        }
    }

    public RuntimeName getRuntimeName() {
        return runtimeName;
    }

    public CommandType getCommandType() {
        return commandType;
    }

    public Object[] getPayload() {
        return payload;
    }

    public void setPayload(Object obj, int i) {
        payload[i] = obj;
    }

    public Command dropFirstPayloadArg() {
        return new Command(
                this.runtimeName,
                this.commandType,
                Arrays.stream(this.payload).skip(1).toArray()
        );
    }

    public Command addArgToPayload(Object arg) {
        return new Command(
                this.runtimeName,
                this.commandType,
                Stream.concat(Arrays.stream(this.payload), Stream.of(arg)).toArray()
        );
    }

    public Command prependArgumentToPayload(Object arg) {
        if (arg == null) {
            return new Command(
                    this.runtimeName,
                    this.commandType,
                    this.payload);
        } else {
            return new Command(
                    this.runtimeName,
                    this.commandType,
                    Stream.concat(Stream.of(arg), Arrays.stream(this.payload)).toArray()
            );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy