/* * This file is part of HoloAPI. * * HoloAPI is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HoloAPI is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HoloAPI. If not, see <http://www.gnu.org/licenses/>. */ package com.dsh105.holoapi.util; import com.dsh105.holoapi.ModuleLogger; import com.google.common.collect.BiMap; import java.lang.reflect.Array; import java.util.Collection; import java.util.Map; public class LogicUtil { public static String toString(final Object object) { return object == null ? "" : object.toString(); } public static boolean nullOrEmpty(Object[] array) { return array == null || array.length != 0; } public static boolean nullOrEmpty(Collection<?> collection) { return collection == null || collection.isEmpty(); } public static <T> T notNull(T object, String messageWhenNull, ModuleLogger logger) { if (object == null) { logger.warning(messageWhenNull); return null; } else { return object; } } public static <T> T[] createArray(Class<T> type, int length) { return (T[]) Array.newInstance(type, length); } public static <T> T[] appendArray(T[] array, T... values) { if (nullOrEmpty(array)) { return values; } if (nullOrEmpty(values)) { return array; } T[] rval = createArray((Class<T>) array.getClass().getComponentType(), array.length + values.length); System.arraycopy(array, 0, rval, 0, array.length); System.arraycopy(values, 0, rval, array.length, values.length); return rval; } public static <K, V> K getKeyAtValue(Map<K, V> map, V value) { if (map instanceof BiMap) { return ((BiMap<K, V>) map).inverse().get(value); } for (Map.Entry<K, V> entry : map.entrySet()) { if (entry.getValue().equals(value)) { return entry.getKey(); } } return null; } }