com.javonet.core.handler.ArraySetItemHandler 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.Dictionary;
import java.util.List;
import java.util.Map;
public class ArraySetItemHandler extends AbstractHandler {
@Override
Object process(Command command) throws Exception {
if (command.getPayload()[0] instanceof Object[]) {
return setArrayElement(command);
} else if (command.getPayload()[0] instanceof List) {
return setListElement(command);
} else if (command.getPayload()[0] instanceof Map) {
return setMapElement(command);
} else if (command.getPayload()[0] instanceof Dictionary) {
return setDictionaryElement(command);
} else {
throw new Exception(String.format("Cannot set element from %s", command.getPayload()[0]));
}
}
private Object setArrayElement(Command command) throws Exception {
Object array = command.getPayload()[0];
Object[] indexes;
if (command.getPayload()[1] instanceof Object[]) {
indexes = (Object[]) command.getPayload()[1];
} else {
indexes = new Object[]{command.getPayload()[1]};
}
Object value = command.getPayload()[2];
switch (indexes.length) {
case 1:
((Object[]) array)[(Integer) indexes[0]] = value;
break;
case 2:
((Object[][]) array)[(Integer) indexes[0]][(Integer) indexes[1]] = value;
break;
case 3:
((Object[][][]) array)[(Integer) indexes[0]][(Integer) indexes[1]][(Integer) indexes[2]] = value;
break;
default:
throw new Exception("ArrayGetItemHandler exception");
}
return 0;
}
private Object setListElement(Command command) {
List list = (List) command.getPayload()[0];
Integer index = (Integer) command.getPayload()[1];
Object value = command.getPayload()[2];
return list.set(index, value);
}
private Object setMapElement(Command command) {
Map map = (Map) command.getPayload()[0];
Object key = command.getPayload()[1];
Object value = command.getPayload()[2];
return map.put(key, value);
}
private Object setDictionaryElement(Command command) {
Dictionary dict = (Dictionary) command.getPayload()[0];
Object key = command.getPayload()[1];
Object value = command.getPayload()[2];
return dict.put(key, value);
}
}