Back to project page Resonos-Android-Framework.
The source code is released under:
Apache License
If you think the Android project Resonos-Android-Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.resonos.apps.library.util; // w w w.ja v a2 s.c o m import java.lang.reflect.Method; /** * This class allows the use of any enum to serve as a parameter list (representing booleans only). * Simply use any enum as the type. * @author Chris * * @param <T> */ public class ParameterList<T extends Enum<?>> { private boolean[] _hasParam = null; /** * Construct the parameter list using an array of your chosen enum. * This allows you to generate a class that accepts any number of parameters * in its constructor or another initialization function as follows: * public Object(Param... params) { * ParameterList<Param> mParams = new ParameterList<Param>(params); * } * @param list, the array of parameters */ @SuppressWarnings("unchecked") public ParameterList(T[] list) { if (list == null) return; Class<T> cl = (Class<T>) list.getClass().getComponentType(); T[] av = (T[]) callPrivateStaticMethod(cl, "values"); _hasParam = new boolean[av.length]; for (int i = 0; i < _hasParam.length; i++) { _hasParam[i] = false; for (int j = 0; j < list.length; j++) { if (list[j] == av[i]) { _hasParam[i] = true; break; } } } } /** * Calls a static method, ignoring access checks * @param Class * @param Method name * @return Method return value */ private Object callPrivateStaticMethod(Class<?> cl, String n) { try { Method m = cl.getDeclaredMethod(n); m.setAccessible(true); return m.invoke(null, (Object[]) null ); } catch (Exception e) { return null; } } /** * Determine whether a parameter was given. * @param The parameter (use an enum constant) * @return true if the parameter was passed */ public boolean has(T t) { if (_hasParam == null) return false; int i = t.ordinal(); return (i >= 0 && i < _hasParam.length) ? _hasParam[i] : false; } /** * Change a parameter value after initialization. * @param The parameter (use an enum constant) * @param the new boolean value */ @SuppressWarnings("unchecked") public void set(T t, boolean b) { if (_hasParam == null) { // if this was constructed with a null, we need to set up the param list Class<T> cl = (Class<T>)t.getClass(); T[] av = (T[]) callPrivateStaticMethod(cl, "values"); _hasParam = new boolean[av.length]; for (int i = 0; i < _hasParam.length; i++) { _hasParam[i] = false; if (t == av[i]) _hasParam[i] = true; } } int i = t.ordinal(); if (i >= 0 && i < _hasParam.length) _hasParam[i] = b; } }