com.javonet.core.handler.ArrayGetItemHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javonet-java-sdk Show documentation
Show all versions of javonet-java-sdk Show documentation
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/
package com.javonet.core.handler;
import com.javonet.utils.Command;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.List;
import java.util.Map;
public class ArrayGetItemHandler extends AbstractHandler {
@Override
Object process(Command command) throws Exception {
if (command.getPayload()[0] instanceof Object[]) {
return getArrayElement(command);
} else if (command.getPayload()[0] instanceof List) {
return getListElement(command);
} else if (command.getPayload()[0] instanceof Map) {
return getMapElement(command);
} else if (command.getPayload()[0] instanceof Dictionary) {
return getDictionaryElement(command);
} else {
throw new Exception(String.format("Cannot get element from %s", command.getPayload()[0]));
}
}
private Object getArrayElement(Command command) throws Exception {
Object[] indexes = Arrays.stream(command.getPayload()).skip(1).toArray();
Object[] indexes_copy = new Object[] {};
if (indexes[0] instanceof Object[]) {
indexes_copy = new Object[((Object[]) indexes[0]).length];
System.arraycopy(((Object[]) indexes[0]), 0, indexes_copy, 0, ((Object[]) indexes[0]).length);
} else {
indexes_copy = indexes;
}
switch (indexes_copy.length) {
case 1:
return ((Object[]) command.getPayload()[0])[(Integer) indexes_copy[0]];
case 2:
return ((Object[][]) command.getPayload()[0])[(Integer) indexes_copy[0]][(Integer) indexes_copy[1]];
case 3:
return ((Object[][][]) command.getPayload()[0])[(Integer) indexes_copy[0]][(Integer) indexes_copy[1]][(Integer) indexes_copy[2]];
default:
throw new Exception(String.format("Cannot get element from %s", command.getPayload()[0]));
}
}
private Object getListElement(Command command) {
List list = (List) command.getPayload()[0];
Integer index = (Integer) command.getPayload()[1];
return list.get(index);
}
private Object getMapElement(Command command) {
Map map = (Map) command.getPayload()[0];
Object key = command.getPayload()[1];
return map.get(key);
}
private Object getDictionaryElement(Command command) {
Dictionary dict = (Dictionary) command.getPayload()[0];
Object key = command.getPayload()[1];
return dict.get(key);
}
}