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

org.freedesktop.dbus.ArrayFrob Maven / Gradle / Ivy

Go to download

Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/).

There is a newer version: 3.3.2
Show newest version
/*
   D-Bus Java Implementation
   Copyright (c) 2005-2006 Matthew Johnson

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the COPYING file with this program.
*/
package org.freedesktop.dbus;

import static org.freedesktop.dbus.Gettext.t;

import java.lang.reflect.Array;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;

import org.slf4j.LoggerFactory;

final class ArrayFrob {
    private static Hashtable, Class> PRIMITIVE_TO_WRAPPER = new Hashtable, Class>();
    private static Hashtable, Class> WRAPPER_TO_PRIMITIVE = new Hashtable, Class>();
    static {
        PRIMITIVE_TO_WRAPPER.put(Boolean.TYPE, Boolean.class);
        PRIMITIVE_TO_WRAPPER.put(Byte.TYPE, Byte.class);
        PRIMITIVE_TO_WRAPPER.put(Short.TYPE, Short.class);
        PRIMITIVE_TO_WRAPPER.put(Character.TYPE, Character.class);
        PRIMITIVE_TO_WRAPPER.put(Integer.TYPE, Integer.class);
        PRIMITIVE_TO_WRAPPER.put(Long.TYPE, Long.class);
        PRIMITIVE_TO_WRAPPER.put(Float.TYPE, Float.class);
        PRIMITIVE_TO_WRAPPER.put(Double.TYPE, Double.class);
        WRAPPER_TO_PRIMITIVE.put(Boolean.class, Boolean.TYPE);
        WRAPPER_TO_PRIMITIVE.put(Byte.class, Byte.TYPE);
        WRAPPER_TO_PRIMITIVE.put(Short.class, Short.TYPE);
        WRAPPER_TO_PRIMITIVE.put(Character.class, Character.TYPE);
        WRAPPER_TO_PRIMITIVE.put(Integer.class, Integer.TYPE);
        WRAPPER_TO_PRIMITIVE.put(Long.class, Long.TYPE);
        WRAPPER_TO_PRIMITIVE.put(Float.class, Float.TYPE);
        WRAPPER_TO_PRIMITIVE.put(Double.class, Double.TYPE);

    }

    private ArrayFrob() {
    }

    @SuppressWarnings("unchecked")
    public static  T[] wrap(Object o) throws IllegalArgumentException {
        Class ac = o.getClass();
        if (!ac.isArray()) {
            throw new IllegalArgumentException(t("Not an array"));
        }
        Class cc = ac.getComponentType();
        Class ncc = PRIMITIVE_TO_WRAPPER.get(cc);
        if (null == ncc) {
            throw new IllegalArgumentException(t("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 ac = (Class) ns.getClass();
        Class cc = (Class) ac.getComponentType();
        Class ncc = WRAPPER_TO_PRIMITIVE.get(cc);
        if (null == ncc) {
            throw new IllegalArgumentException(t("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(t("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 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) o, (Class) c.getComponentType());
            }

            // List -> Integer[]
            if (o instanceof List && c.isArray()) {
                return delist((List) o, (Class) c.getComponentType());
            }

            if (o.getClass().isArray() && c.isArray()) {
                return type((Object[]) o, (Class) c.getComponentType());
            }

        } catch (Exception e) {
            if (AbstractConnection.EXCEPTION_DEBUG) {
                LoggerFactory.getLogger(ArrayFrob.class).error("", e);
            }
            throw new IllegalArgumentException(e);
        }

        throw new IllegalArgumentException(MessageFormat.format(t("Not An Expected Convertion type from {0} to {1}"), new Object[] {
                o.getClass(), c
        }));
    }

    public static Object[] type(Object[] old, Class c) {
        Object[] ns = (Object[]) Array.newInstance(c, old.length);
        for (int i = 0; i < ns.length; i++) {
            ns[i] = old[i];
        }
        return ns;
    }
}