Made a couple of utility classes for a project I'm working on I'm sure someone with find good use for these for RSPS or just help them save time.
AttributeManager.java
[Description]
This class acts as a manager that is declared for a Type to stores object for that Type for temporary use.
Code:
package com.anezka.util;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Anežka Lane
*
* This is a utility class that minipulates a {@code HashMap} to purpose
* temporary {@code Object}(s) for an existing {@code Objects}.
*/
public class AttributeManager {
/**
* A {@code HashMap} that contains the temporay attributes.
*/
private final Map<String, Object> attributes = new HashMap<String, Object>();
/**
* Creates a new attribute if the {@code HashMap}'s {@value key} doesn't already exist.
* @param key The key to access the attribute.
* @param value The value of the object.
*/
public void create(String key, Object value) {
this.attributes.putIfAbsent(key, value);
}
/**
* Minipulates the value of an existing {@code HashMap}'s {@value key}.
* @param key The key to access the attribute.
* @param value The new value of the object.
*/
public void setValueOf(String key, Object value) {
if(!attributes.containsKey(key)) {
System.out.println("[Attribute Manager] detected null key query. inquery denied.");
return;
}
attributes.put(key, value);
}
/**
* Removes a existing attribute from the {@code HashMap}.
* @param key The key for the entry to be removed.
*/
public void remove(String key) {
if(!attributes.containsKey(key)) {
System.out.println("[Attribute Manager] detected null key query. inquery denied.");
return;
}
this.attributes.remove(key);
}
/**
* A query for an attribute.
* @param key The key for the attribute being queried.
* @return The Attribute.
*/
public Object get(String key) {
if(!attributes.containsKey(key)) {
System.out.println("[Attribute Manager] detected null key query. inquery denied.");
return null;
}
return attributes.get(key);
}
}
RandomUtil.java
[Description]
Contains some useful functional methods using the random generator.
Code:
package com.anezka.util;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
*
* @author Anežka Lane
*
* A utility class which contains functionality and output
* using the random utility.
*/
public class RandomUtil {
private static Random random = new Random();
public static int randomInt() {
return random.nextInt();
}
public static int randomMaximumInt(int max) {
return random.nextInt(max);
}
public static int randomMinimumInt(int min) {
return (random.nextInt() + min);
}
public static int randomWithinRangeInt(int min, int max) {
return (random.nextInt(max) + min);
}
public static double randomDouble() {
return random.nextDouble();
}
public static double randomMaximumDouble(double max) {
double value = random.nextDouble();
return (value > max ? max : value);
}
public static double randomMinimumDouble(double min) {
return (random.nextDouble() + min);
}
public static double randomWithinRangeDouble(double min, double max) {
return (randomMaximumDouble(max) + min);
}
public static boolean randomFlag() {
return random.nextBoolean();
}
public static <T> T randomElement(T[] array) {
return array[random.nextInt(array.length)];
}
public static <T> T randomElement(List<T> list) {
if(list.isEmpty()) {
return null;
}
return list.get(random.nextInt(list.size()));
}
public static Object randomEntryKey(Map<?, ?> map) {
if(map.isEmpty()) {
return null;
}
Object[] keys = map.keySet().toArray();
return keys[random.nextInt(keys.length)];
}
public static Object randomEntryValue(Map<?, ?> map) {
if(map.isEmpty()) {
return null;
}
Object[] values = map.values().toArray();
return values[random.nextInt(values.length)];
}
}