package pl.shockah;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class Helper {
public static final Charset utf8 = Charset.forName("UTF-8");
@SafeVarargs
public static <T> boolean equalsOR(T instance, T... compareWith) {
for (T t : compareWith) if (instance.equals(t)) return true;
return false;
}
@SafeVarargs
public static <T> boolean equalsAND(T instance, T... compareWith) {
for (T t : compareWith) if (!instance.equals(t)) return false;
return true;
}
@SuppressWarnings("unchecked") public static <T, S extends T> ArrayList<S> getAllOfType(ArrayList<T> list, Class<S> type) {
ArrayList<S> newList = new ArrayList<S>();
for (T object : list) if (type.isInstance(object)) newList.add((S)object);
return newList;
}
public static <T, S extends T> ArrayList<T> removeAllOfType(ArrayList<T> list, Class<S> type) {
ArrayList<T> newList = new ArrayList<T>();
for (T object : list) if (!type.isInstance(object)) newList.add(object);
return newList;
}
public static int countCharOccurences(String string, char needle) {
int count = 0;
for (int i = 0; i < string.length(); i++) if (string.charAt(i) == needle) count++;
return count;
}
public static String getFilenameExt(File file) {
String name = file.getName();
if (name.contains(".")) {
int index = name.lastIndexOf(".");
return name.substring(index+1).toLowerCase();
}
return "";
}
public static void sleep(long ms) {
try {
Thread.sleep(ms);
} catch (Exception e) {e.printStackTrace();}
}
}