com.ptsmods.mysqlw.collection.DbList 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.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.ptsmods.mysqlw.Database;
import com.ptsmods.mysqlw.query.QueryCondition;
import com.ptsmods.mysqlw.query.QueryConditions;
import com.ptsmods.mysqlw.query.QueryOrder;
import com.ptsmods.mysqlw.query.SelectResults;
import com.ptsmods.mysqlw.table.ColumnType;
import com.ptsmods.mysqlw.table.TablePreset;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
public class DbList extends AbstractList implements DbCollection {
private static final TablePreset preset = TablePreset.create("list_")
.putColumn("id", ColumnType.INT.createStructure()
.satiateSupplier(sup -> sup.apply(null))
.setAutoIncrement(true)
.setNullAllowed(false)
.setPrimary(true))
.putColumn("val", ColumnType.TEXT.createStructure());
private static final Map> cache = new HashMap<>();
private final Database db;
private final String table;
private final String name;
private final BiFunction elementToString;
private final BiFunction elementFromString;
/**
* Parses a String representation of a DbList into a DbList.
* @param db The database this list belongs to. Used when creating a new map.
* @param s The String to parse.
* @param elementToString The function used to convert an element of this list into a String. Used when creating a new list.
* @param elementFromString The function used to convert an element of this list into a String. Used when creating a new list.
* @param The type of the elements in this set.
* @return A new DbList or a cached one if available.
*/
public static DbList parseString(Database db, String s, BiFunction elementToString, BiFunction elementFromString) {
return s.startsWith("DbList[name=") ? getList(db, Database.readQuotedString(s.substring("DbList[name=".length())), elementToString, elementFromString) : null;
}
/**
* Parses a String representation of a DbList into a DbList.
* @param db The database this list belongs to. Used when creating a new map.
* @param name The name of this list.
* @param type The Class of type E if you've registered a type converter on {@link DbCF}. Used when creating a new list.
* @param The type of the elements in this set.
* @return A new DbList or a cached one if available.
*/
public static DbList getList(Database db, String name, Class type) {
return getList(db, name, DbCF.getTo(type), DbCF.getFrom(type));
}
/**
* Parses a String representation of a DbList into a DbList.
* @param db The database this list belongs to. Used when creating a new map.
* @param name The name of this list.
* @param elementToString The function used to convert an element of this list into a String. Used when creating a new list.
* @param elementFromString The function used to convert an element of this list into a String. Used when creating a new list.
* @param The type of the elements in this set.
* @return A new DbList or a cached one if available.
*/
public static DbList getList(Database db, String name, BiFunction elementToString, BiFunction elementFromString) {
if (cache.containsKey(name))
try {
return (DbList) cache.get(name);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Wrong type! Cached DbList with the given name has a different type than requested.", e);
}
else return new DbList<>(db, name, elementToString, elementFromString);
}
private DbList(Database db, String name, BiFunction elementToString, BiFunction elementFromString) {
if (cache.containsKey(name)) throw new IllegalArgumentException("A DbList by this name already exists.");
Preconditions.checkNotNull(db, "database");
Preconditions.checkNotNull(elementToString, "elementToString");
Preconditions.checkNotNull(elementFromString, "elementFromString");
this.db = db;
this.table = "list_" + name;
this.name = name;
preset.setName(table);
if (db.getType() == Database.RDBMS.SQLite) preset.getColumns().get("id").setTypeString(db.getType() == Database.RDBMS.SQLite ? "INTEGER" : "INT");
preset.create(db);
this.elementToString = elementToString;
this.elementFromString = elementFromString;
cache.put(name, this);
}
@Override
public int size() {
return db.count(table, "val", null);
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean contains(Object o) {
return db.select(table, "val", QueryCondition.equals("val", elementToString.apply((E) o, this)), null, null).size() > 0;
}
@Nonnull
@Override
public Iterator iterator() {
return toArrayList().iterator();
}
@Nonnull
@Override
public Object[] toArray() {
return toArrayList().toArray();
}
@Nonnull
@Override
public T[] toArray(@Nonnull T[] a) {
return toArrayList().toArray(a);
}
@Override
public boolean add(E e) {
return db.insert(table, "val", elementToString.apply(e, this)) == 1;
}
@Override
public boolean remove(Object o) {
boolean b = db.delete(table, QueryCondition.equals("val", elementToString.apply((E) o, this))) > 0;
fixIndexes();
return b;
}
@Override
public boolean containsAll(@Nonnull Collection> c) {
QueryConditions condition = QueryConditions.create();
for (Object element : c)
condition.or(QueryCondition.equals("val", elementToString.apply((E) element, this)));
return db.select(table, new String[] {"val"}, condition, QueryOrder.by("id"), null).size() == c.size();
}
@Override
public boolean addAll(@Nonnull Collection extends E> c) {
List