
org.freedesktop.dbus.ArrayFrob Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbus-java-core Show documentation
Show all versions of dbus-java-core Show documentation
Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/).
The newest version!
package org.freedesktop.dbus;
import org.freedesktop.dbus.utils.PrimitiveUtils;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public final class ArrayFrob {
private ArrayFrob() {
}
/**
* @deprecated use {@link PrimitiveUtils#getPrimitiveToWrapperTypes()}
* @return Map with primitive type and wrapper types
*/
@Deprecated(since = "5.1.1 - 2024-09-15")
public static Map, Class>> getPrimitiveToWrapperTypes() {
return PrimitiveUtils.getPrimitiveToWrapperTypes();
}
/**
* @deprecated use {@link PrimitiveUtils#getWrapperToPrimitiveTypes()}
* @return Map with wrapper type and primitive type
*/
@Deprecated(since = "5.1.1 - 2024-09-15")
public static Map, Class>> getWrapperToPrimitiveTypes() {
return PrimitiveUtils.getWrapperToPrimitiveTypes();
}
@SuppressWarnings("unchecked")
public static T[] wrap(Object _o) throws IllegalArgumentException {
Class extends Object> ac = _o.getClass();
if (!ac.isArray()) {
throw new IllegalArgumentException("Not an array");
}
Class extends Object> cc = ac.getComponentType();
Class extends Object> ncc = PrimitiveUtils.getPrimitiveToWrapperTypes().get(cc);
if (null == ncc) {
throw new IllegalArgumentException("Not a primitive type");
}
T[] ns = (T[]) Array.newInstance(ncc, Array.getLength(_o));
for (int i = 0; i < ns.length; i++) {
ns[i] = (T) Array.get(_o, i);
}
return ns;
}
@SuppressWarnings("unchecked")
public static Object unwrap(T[] _ns) throws IllegalArgumentException {
Class extends T[]> ac = (Class extends T[]>) _ns.getClass();
Class cc = (Class) ac.getComponentType();
Class extends Object> ncc = PrimitiveUtils.getWrapperToPrimitiveTypes().get(cc);
if (null == ncc) {
throw new IllegalArgumentException("Not a wrapper type");
}
Object o = Array.newInstance(ncc, _ns.length);
for (int i = 0; i < _ns.length; i++) {
Array.set(o, i, _ns[i]);
}
return o;
}
public static List listify(T[] _ns) throws IllegalArgumentException {
return Arrays.asList(_ns);
}
@SuppressWarnings("unchecked")
public static List listify(Object _o) throws IllegalArgumentException {
if (_o instanceof Object[]) {
return listify((T[]) _o);
}
if (!_o.getClass().isArray()) {
throw new IllegalArgumentException("Not an array");
}
List l = new ArrayList<>(Array.getLength(_o));
for (int i = 0; i < Array.getLength(_o); i++) {
l.add((T) Array.get(_o, i));
}
return l;
}
@SuppressWarnings("unchecked")
public static T[] delist(List _l, Class _c) throws IllegalArgumentException {
return _l.toArray((T[]) Array.newInstance(_c, 0));
}
public static Object delistprimitive(List _l, Class _c) throws IllegalArgumentException {
Object o = Array.newInstance(_c, _l.size());
for (int i = 0; i < _l.size(); i++) {
Array.set(o, i, _l.get(i));
}
return o;
}
@SuppressWarnings("unchecked")
public static Object convert(Object _o, Class extends Object> _c) throws IllegalArgumentException {
/* Possible Conversions:
*
** List -> List
** List -> int[]
** List -> Integer[]
** int[] -> int[]
** int[] -> List
** int[] -> Integer[]
** Integer[] -> Integer[]
** Integer[] -> int[]
** Integer[] -> List
*/
try {
// List -> List
if (List.class.equals(_c) && _o instanceof List) {
return _o;
}
// int[] -> List
// Integer[] -> List
if (List.class.equals(_c) && _o.getClass().isArray()) {
return listify(_o);
}
// int[] -> int[]
// Integer[] -> Integer[]
if (_o.getClass().isArray() && _c.isArray() && _o.getClass().getComponentType().equals(_c.getComponentType())) {
return _o;
}
// int[] -> Integer[]
if (_o.getClass().isArray() && _c.isArray() && _o.getClass().getComponentType().isPrimitive()) {
return wrap(_o);
}
// Integer[] -> int[]
if (_o.getClass().isArray() && _c.isArray() && _c.getComponentType().isPrimitive()) {
return unwrap((Object[]) _o);
}
// List -> int[]
if (_o instanceof List && _c.isArray() && _c.getComponentType().isPrimitive()) {
return delistprimitive((List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy