/*
 */
package com.agricraft.agricore.util;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * A class to help with using types.
 *
 * @author RlonRyan
 */
public class TypeHelper {
	
	public static <T> T[] asArray(Collection<T> set, Class<T> type) {
		@SuppressWarnings("unchecked")
		final T[] array = (T[])Array.newInstance(type, set.size());
		return set.toArray(array);
	}
	
	public static <T> List<T> asList(T... elements) {
		return Arrays.asList(elements);
	}
	
	public static <T> Set<T> asSet(T... elements) {
		return new HashSet<>(Arrays.asList(elements));
	}
	
	public static <T, C extends Collection<T>> C addAll(C collection, T... elements) {
		collection.addAll(Arrays.asList(elements));
		return collection;
	}
	
	public static boolean isAllTypes(Object obj, Class... types) {
		return isAllTypes(obj.getClass(), types);
	}
	
	public static boolean isAllTypes(Class clazz, Class... types) {
		for (Class type : types) {
			if (!isType(clazz, type)) {
				return false;
			}
		}
		return true;
	}
	
	public static boolean isAnyType(Object obj, Class... types) {
		return isAnyType(obj.getClass(), types);
	}
	
	public static boolean isAnyType(Class clazz, Class... types) {
		for (Class type : types) {
			if (isType(clazz, type)) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isType(Object obj, Class type) {
		return isType(obj.getClass(), type);
	}
	
	public static boolean isType(Class clazz, Class type) {
		return type.isAssignableFrom(clazz);
	}
	
	public static boolean isNull(Object obj) {
		return obj == null;
	}
	
	public static boolean isNonNull(Object obj) {
		return obj != null;
	}
    
    public static <T> T typeCast(Object obj, Class<T> type) {
        if (type.isAssignableFrom(obj.getClass())) {
            return type.cast(obj);
        } else {
            return null;
        }
    }
	
}

@@ -173,16 +173,43 @@
 l.List;%0A
+import java.util.Optional;%0A
 import j
@@ -1805,15 +1805,21 @@
 %3CT%3E 
-T typeC
+Optional%3CT%3E c
 ast(
@@ -1915,24 +1915,36 @@
      return 
+Optional.of(
 type.cast(ob
@@ -1945,16 +1945,17 @@
 ast(obj)
+)
 ;%0A      
@@ -1984,20 +1984,32 @@
  return 
-null
+Optional.empty()
 ;%0A      

Tweak Type Helper