11package org .teachingextensions .approvals .lite .util ;
22
3+ import java .awt .Image ;
34import java .lang .reflect .Array ;
45import java .lang .reflect .Method ;
6+ import java .net .URL ;
57import java .util .ArrayList ;
68import java .util .Arrays ;
79import java .util .List ;
810
11+ import javax .swing .ImageIcon ;
12+
913/**
1014 * A static class of convenience functions for Manipulating objects
1115 */
1216public class ObjectUtils {
1317
14- /**
15- * A convenience function to check if two objects are equal.
16- *
17- * @param s1 The first object
18- * @param s2 The second object
19- * @return true if Equal.
20- */
21- public static boolean isEqual (Object s1 , Object s2 ) {
22- return s1 == s2 || (s1 != null ) && s1 .equals (s2 );
23- }
18+ /**
19+ * A convenience function to check if two objects are equal.
20+ *
21+ * @param s1
22+ * The first object
23+ * @param s2
24+ * The second object
25+ * @return true if Equal.
26+ */
27+ public static boolean isEqual (Object s1 , Object s2 ) {
28+ return s1 == s2 || (s1 != null ) && s1 .equals (s2 );
29+ }
2430
25- public static boolean isThisInstanceOfThat (Class <?> thiz , Class <?> that ) {
26- return that .isAssignableFrom (thiz );
27- }
31+ public static boolean isThisInstanceOfThat (Class <?> thiz , Class <?> that ) {
32+ return that .isAssignableFrom (thiz );
33+ }
2834
29- public static Error throwAsError (Throwable t ) throws Error {
30- if (t instanceof RuntimeException ) {
31- throw (RuntimeException ) t ;
32- } else if (t instanceof Error ) {
33- throw (Error ) t ;
34- } else {
35- throw new Error (t );
36- }
35+ public static Error throwAsError (Throwable t ) throws Error {
36+ if (t instanceof RuntimeException ) {
37+ throw (RuntimeException ) t ;
38+ } else if (t instanceof Error ) {
39+ throw (Error ) t ;
40+ } else {
41+ throw new Error (t );
3742 }
43+ }
3844
39- /**
40- * @param from the source array
41- * @param methodName the filter method
42- * @return a filtered array
43- */
44- public static Object [] extractArray ( Object [] from , String methodName ) {
45- try {
46- if ( from == null || from . length == 0 ) {
47- return new Object [ 0 ];
48- }
49- Method method = getGreatestCommonDenominator ( from , methodName ) ;
50- Object [] array ;
51- if ( Object . class . isAssignableFrom ( method . getReturnType ())) {
52- array = ( Object []) Array . newInstance ( method . getReturnType (),
53- from . length );
54- } else {
55- array = ( Object []) Array . newInstance (
56- ClassUtils . getWrapperClass ( method . getReturnType ()),
57- from . length );
58- }
59- for ( int i = 0 ; i < from . length ; i ++) {
60- array [ i ] = method . invoke ( from [ i ], ( Object []) null );
61- }
62- return array ;
63- } catch ( Exception e ) {
64- MySystem . warning ( e );
65- throw ObjectUtils . throwAsError (e );
66- }
45+ /**
46+ * @param from
47+ * the source array
48+ * @param methodName
49+ * the filter method
50+ * @return a filtered array
51+ */
52+ public static Object [] extractArray ( Object [] from , String methodName ) {
53+ try {
54+ if ( from == null || from . length == 0 ) {
55+ return new Object [ 0 ] ;
56+ }
57+ Method method = getGreatestCommonDenominator ( from , methodName );
58+ Object [] array ;
59+ if ( Object . class . isAssignableFrom ( method . getReturnType ())) {
60+ array = ( Object []) Array . newInstance ( method . getReturnType (),
61+ from . length );
62+ } else {
63+ array = ( Object []) Array . newInstance (
64+ ClassUtils . getWrapperClass ( method . getReturnType ()), from . length );
65+ }
66+ for ( int i = 0 ; i < from . length ; i ++) {
67+ array [ i ] = method . invoke ( from [ i ], ( Object []) null );
68+ }
69+ return array ;
70+ } catch ( Exception e ) {
71+ MySystem . warning (e );
72+ throw ObjectUtils . throwAsError ( e );
6773 }
74+ }
6875
69- public static Method getGreatestCommonDenominator (Object [] from ,
70- String methodName ) throws SecurityException , NoSuchMethodException {
71- List <Class <?>> classes = new ArrayList <>();
72- ArrayUtils .addArray (classes , getAllCastableClasses (from [0 ]));
73- for (Object o : from ) {
74- for (int i = classes .size () - 1 ; i >= 0 ; i --) {
75- Class clazz = classes .get (i );
76- if (!isThisInstanceOfThat (o .getClass (), clazz )
77- || !ClassUtils .hasMethod (clazz , methodName )) {
78- classes .remove (i );
79- }
80- }
76+ public static Method getGreatestCommonDenominator (Object [] from ,
77+ String methodName ) throws SecurityException , NoSuchMethodException {
78+ List <Class <?>> classes = new ArrayList <>();
79+ ArrayUtils .addArray (classes , getAllCastableClasses (from [0 ]));
80+ for (Object o : from ) {
81+ for (int i = classes .size () - 1 ; i >= 0 ; i --) {
82+ Class clazz = classes .get (i );
83+ if (!isThisInstanceOfThat (o .getClass (), clazz )
84+ || !ClassUtils .hasMethod (clazz , methodName )) {
85+ classes .remove (i );
8186 }
82- return classes .size () == 0 ? null : ArrayUtils .getLast (classes )
83- .getMethod (methodName , (Class []) null );
87+ }
8488 }
89+ return classes .size () == 0 ? null : ArrayUtils .getLast (classes ).getMethod (
90+ methodName , (Class []) null );
91+ }
8592
86- private static Class [] getAllCastableClasses (Object object ) {
87- Class <?> clazz = object .getClass ();
88- ArrayList <Object > list = new ArrayList <>();
89- while (clazz != null ) {
90- list .add (clazz );
91- ArrayUtils .addArray (list , clazz .getInterfaces ());
92- clazz = clazz .getSuperclass ();
93- }
94- Class [] found = list .toArray (new Class [list .size ()]);
95- ArrayUtils .toReverseArray (found );
96- return found ;
93+ private static Class [] getAllCastableClasses (Object object ) {
94+ Class <?> clazz = object .getClass ();
95+ ArrayList <Object > list = new ArrayList <>();
96+ while (clazz != null ) {
97+ list .add (clazz );
98+ ArrayUtils .addArray (list , clazz .getInterfaces ());
99+ clazz = clazz .getSuperclass ();
97100 }
101+ Class [] found = list .toArray (new Class [list .size ()]);
102+ ArrayUtils .toReverseArray (found );
103+ return found ;
104+ }
98105
99- public static void assertInstance (Class classes [], Object object ) {
100- if (object == null ) {
101- throw new NullPointerException ("Expected Object of Type "
102- + Arrays .asList (extractArray (classes , "getName" ))
103- + " but was null" );
104- }
105- for (Class aClass : classes ) {
106- if (ClassUtils .getWrapperClass (aClass ).isInstance (object )) {
107- return ;
108- }
109- }
110- throw new IllegalArgumentException ("Expected Object of Type "
111- + Arrays .asList (extractArray (classes , "getName" )) + " but got "
112- + object .getClass ().getName ());
106+ public static void assertInstance (Class classes [], Object object ) {
107+ if (object == null ) {
108+ throw new NullPointerException ("Expected Object of Type "
109+ + Arrays .asList (extractArray (classes , "getName" )) + " but was null" );
110+ }
111+ for (Class aClass : classes ) {
112+ if (ClassUtils .getWrapperClass (aClass ).isInstance (object )) {
113+ return ;
114+ }
113115 }
116+ throw new IllegalArgumentException ("Expected Object of Type "
117+ + Arrays .asList (extractArray (classes , "getName" )) + " but got "
118+ + object .getClass ().getName ());
119+ }
114120
115- public static String getClassName (Object o ) {
116- return o == null ? "null" : o .getClass ().getName ();
121+ public static String getClassName (Object o ) {
122+ return o == null ? "null" : o .getClass ().getName ();
123+ }
124+
125+ public static Image loadImage (Class <?> type , String name ) {
126+ URL resource = type .getResource (name );
127+ if (resource == null ) {
128+ resource = type .getClassLoader ().getResource (name );
129+ }
130+ if (resource == null ) {
131+ throw new IllegalStateException ("Could not find image: " + name );
117132 }
133+ return new ImageIcon (resource ).getImage ();
134+ }
118135}
0 commit comments