com.ptsmods.mysqlw.collection.DbCF Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of MySQLw Show documentation
Show all versions of MySQLw Show documentation
A wrapper for MySQL connections
package com.ptsmods.mysqlw.collection;
import com.ptsmods.mysqlw.Database;
import com.ptsmods.mysqlw.Pair;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
* Database Collection Functions
* Functions used to convert instances of various classes to and from Strings.
*/
public class DbCF {
// Basic type converters
// Self-explanatory, really.
public static final BiFunction strFunc = (s, c) -> s;
public static final BiFunction toByteFunc = (s, c) -> Byte.parseByte(s);
public static final BiFunction fromByteFunc = (b, c) -> String.valueOf(b);
public static final BiFunction toShortFunc = (s, c) -> Short.parseShort(s);
public static final BiFunction fromShortFunc = (s, c) -> String.valueOf(s);
public static final BiFunction toIntFunc = (s, c) -> Integer.parseInt(s);
public static final BiFunction fromIntFunc = (i, c) -> String.valueOf(i);
public static final BiFunction toLongFunc = (s, c) -> Long.parseLong(s);
public static final BiFunction fromLongFunc = (l, c) -> String.valueOf(l);
public static final BiFunction toFloatFunc = (s, c) -> Float.parseFloat(s);
public static final BiFunction fromFloatFunc = (f, c) -> String.valueOf(f);
public static final BiFunction toDoubleFunc = (s, c) -> Double.parseDouble(s);
public static final BiFunction fromDoubleFunc = (d, c) -> String.valueOf(d);
private static final Map, Pair, BiFunction>> converters = new HashMap<>();
static {
registerConverters(String.class, strFunc, strFunc);
registerConverters(Byte.class, fromByteFunc, toByteFunc);
registerConverters(Short.class, fromShortFunc, toShortFunc);
registerConverters(Integer.class, fromIntFunc, toIntFunc);
registerConverters(Long.class, fromLongFunc, toLongFunc);
registerConverters(Float.class, fromFloatFunc, toFloatFunc);
registerConverters(Double.class, fromDoubleFunc, toDoubleFunc);
}
// DbCollection type converters
/**
* @param The type of the elements in this list.
* @return A bifunction to convert a DbList into a String which can be read by {@link #dbListFromStringFunc(BiFunction, BiFunction)} and {@link #dbListFromStringFunc(Class)}.
* @see #dbListFromStringFunc(BiFunction, BiFunction)
* @see #dbListFromStringFunc(Class)
*/
public static BiFunction, DbCollection, String> dbListToStringFunc() {
return (l, c) -> "DbList[name=" + Database.enquote(l.getName()) + "]";
}
/**
* @param type The class of the elements in this list. Used to get the type converters registered with {@link #registerConverters(Class, BiFunction, BiFunction)}.
* @param The type of the elements in this list.
* @return A bifunction to read the String representation of a DbList produced by {@link #dbListToStringFunc()}.
* @see #dbListToStringFunc()
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction> dbListFromStringFunc(Class type) {
return dbListFromStringFunc(getTo(type), getFrom(type));
}
/**
* @param elementToString The bifunction to convert the elements in this list to Strings.
* @param elementFromString The bifunction to convert Strings to elements fit for this list.
* @param The type of the elements in this list.
* @return A bifunction to read the String representation of a DbList produced by {@link #dbListToStringFunc()}.
* @see #dbListToStringFunc()
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction> dbListFromStringFunc(BiFunction elementToString, BiFunction elementFromString) {
return (s, c) -> DbList.parseString(c.getDb(), s, elementToString, elementFromString);
}
/**
* @param The type of the elements in this set.
* @return A bifunction to convert a DbSet into a String which can be read by {@link #dbSetFromStringFunc(BiFunction, BiFunction)} and {@link #dbSetFromStringFunc(Class)}.
* @see #dbSetFromStringFunc(BiFunction, BiFunction)
* @see #dbSetFromStringFunc(Class)
*/
public static BiFunction, DbCollection, String> dbSetToStringFunc() {
return (s, c) -> "DbSet[name=" + Database.enquote(s.getName()) + "]";
}
/**
* @param type The class of the elements in this set. Used to get the type converters registered with {@link #registerConverters(Class, BiFunction, BiFunction)}.
* @param The type of the elements in this set.
* @return A bifunction to read the String representation of a DbSet produced by {@link #dbSetToStringFunc()}.
* @see #dbSetToStringFunc()
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction> dbSetFromStringFunc(Class type) {
return dbSetFromStringFunc(getTo(type), getFrom(type));
}
/**
* @param elementToString The bifunction to convert the elements in this list to Strings.
* @param elementFromString The bifunction to convert Strings to elements fit for this list.
* @param The type of the elements in this list.
* @return A bifunction to read the String representation of a DbList produced by {@link #dbListToStringFunc()}.
* @see #dbListToStringFunc()
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction> dbSetFromStringFunc(BiFunction elementToString, BiFunction elementFromString) {
return (s, c) -> DbSet.parseString(c.getDb(), s, elementToString, elementFromString);
}
/**
* @param The type of the keys in this map.
* @param The type of the values in this map.
* @return A bifunction to convert a DbMap into a String which can be read by {@link #dbMapFromStringFunc(BiFunction, BiFunction, BiFunction, BiFunction)} and {@link #dbMapFromStringFunc(Class, Class)}.
* @see #dbMapFromStringFunc(BiFunction, BiFunction, BiFunction, BiFunction)
* @see #dbMapFromStringFunc(Class, Class)
*/
public static BiFunction, DbCollection, String> dbMapToStringFunc() {
return (child, parent) -> "DbMap[name=" + Database.enquote(child.getName()) + "]";
}
/**
* @param keyType The class of the keys in this map. Used to get the type converters registered with {@link #registerConverters(Class, BiFunction, BiFunction)}.
* @param valueType The class of the values in this map. Used to get the type converters registered with {@link #registerConverters(Class, BiFunction, BiFunction)}.
* @param The type of the keys in this map.
* @param The type of the values in this map.
* @return A bifunction to read the String representation of a DbMap produced by {@link #dbMapToStringFunc()}.
* @see #dbMapToStringFunc()
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction> dbMapFromStringFunc(Class keyType, Class valueType) {
return dbMapFromStringFunc(getTo(keyType), getTo(valueType), getFrom(keyType), getFrom(valueType));
}
/**
*
* @param keyToString The bifunction to convert the keys in this map to Strings.
* @param valueToString The bifunction to convert the values in this map to Strings.
* @param keyFromString The bifunction to convert Strings to keys fit for this map.
* @param valueFromString The bifunction to convert Strings to values fit for this map.
* @param The type of the keys in this map.
* @param The type of the values in this map.
* @return A bifunction to read the String representation of a DbMap produced by {@link #dbMapToStringFunc()}.
* @see #dbMapToStringFunc()
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction> dbMapFromStringFunc(BiFunction keyToString, BiFunction valueToString, BiFunction keyFromString, BiFunction valueFromString) {
return (s, c) -> DbMap.parseString(c.getDb(), s, keyToString, valueToString, keyFromString, valueFromString);
}
// Custom type converters
/**
* Used to register a bifunction to convert Objects of type T to Strings and a bifunction to convert Strings to Objects of type T.
* @param type The class of type T
* @param toString The bifunction to convert Objects of type T to Strings.
* @param fromString The bifunction to convert Strings to objects of type T.
* @param The type these functions can convert to and from.
* @see #getTo(Class)
* @see #getFrom(Class)
*/
public static void registerConverters(Class type, BiFunction toString, BiFunction fromString) {
converters.put(type, new Pair<>(toString, fromString));
}
/**
* @param type The class of type T.
* @param The type of Objects you wish to convert.
* @return The bifunction to convert Objects of type T to Strings, registered with {@link #registerConverters(Class, BiFunction, BiFunction)}.
* @see #getFrom(Class)
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction getTo(Class type) {
return get(type).getLeft();
}
/**
* @param type The class of type T.
* @param The type of Objects you wish to convert to.
* @return The bifunction to convert Strings to Objects of type T, registered with {@link #registerConverters(Class, BiFunction, BiFunction)}.
* @see #getTo(Class)
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static BiFunction getFrom(Class type) {
return get(type).getRight();
}
/**
* @param type The class of type T.
* @param The type of Objects you wish to convert to and from.
* @return A pair of both {@link #getTo(Class)} and {@link #getFrom(Class)}.
* @see #getTo(Class)
* @see #getFrom(Class)
* @see #registerConverters(Class, BiFunction, BiFunction)
*/
public static Pair, BiFunction> get(Class type) {
if (!converters.containsKey(type)) return new Pair<>(null, null);
Pair, BiFunction> pair = converters.get(type);
return new Pair<>((BiFunction) pair.getLeft(), (BiFunction) pair.getRight());
}
}