All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mmb.engine.MMBUtils Maven / Gradle / Ivy

Go to download

Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world. THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<

The newest version!
/**
 * 
 */
package mmb.engine;

import java.util.function.Consumer;
import java.util.function.Supplier;

import mmb.NN;

/**
 * A set of general-purpose game-wide utility methods
 * @author oskar
 *
 */
public class MMBUtils {
	private MMBUtils() {}
	//Casting
	/**
	 * Switches displayed class types. The types should be variant of the same class type
	 * @param  dest type
	 * @param cls class to cast
	 * @return a casted class
	 */
	@SuppressWarnings("unchecked")
	@NN public static  Class classcast(Class cls){
		return (Class) cls;
	}
	/**
	 * WARNING: THIS METHOD IS UNSAFE AND MISUSE WILL RESULT IN HARD TO TRACE CASTING BUGS, so the name is very long
	 * @param in input
	 * @param  source type
	 * @param  destination type
	 * @return the same object (but it might fail)
	 * @throws ClassCastException randomly, when misused in various methods
	 */
	@SuppressWarnings("unchecked")
	public static  U thisIsAReallyLongNameUnsafeCast(T in) {
		return (U) in;
	}
	/**
	 * WARNING: THIS METHOD IS UNSAFE AND MISUSE WILL RESULT IN HARD TO TRACE CASTING BUGS, so the name is very long
	 * @param in input
	 * @param  source type
	 * @param  destination type
	 * @return the same object (but it might fail)
	 * @throws ClassCastException randomly, when misused in various methods
	 */
	@SuppressWarnings("unchecked")
	@NN public static  U thisIsAReallyLongNameUnsafeCastNN(T in) {
		return (U) in;
	}	
	
	//Boolean to value
	/**
	 * Bool-to-int conversion
	 * @param bool value to convert
	 * @return an integer representation of a boolean
	 */
	public static int bool2int(boolean bool) {
		return bool ? 1 : 0;
	}
	
	//Lambda expressions
	@NN private static final Consumer nothing = v -> {/*empty*/};
	/**
	 * Returns a consumer which does nothing
	 * @param  type of the input
	 * @return a consumer which does nothing.
	 * @apiNote The returned consumers are the same object
	 */
	@SuppressWarnings("unchecked")
	@NN public static  Consumer doNothing(){
		return (Consumer) nothing;
	}	
	/**
	 * Creates a {@link Runnable} which invokes the {@code action} with {@code value}
	 * @param  the data type
	 * @param action consumer to insert into
	 * @param value value to insert
	 * @return the runnable object
	 */
	public static  Runnable loadValue(Consumer action, T value) {
		return () -> action.accept(value);
	}	
	/**
	 * Evaluates the input lazily
	 * @param  type of values
	 * @param sup source of values
	 * @return a lazily evaluated supplier
	 */
	public static  Supplier lazy(Supplier sup){
		return new Supplier<>() {
			boolean isValid;
			T value;
			@Override
			public T get() {
				if(!isValid) {
					value = sup.get();
					isValid = true;
				}
				return value;
			}
			
		};
	}
	
	//Undeclared throws
	/**
	 * Throws given exception without a declaration
	 * @param t the exception to throw
	 */
	public static void shoot(Throwable t) {
		MMBUtils.shoot0(t);
	}
	@SuppressWarnings("unchecked")
	private static  void shoot0(Throwable e) throws T {
		throw (T) e;
	}
}