Made DataContainer non Serializable, getValue instead of getUnsafe in UsersTable - both attempt to fix #673 - 1

This commit is contained in:
Rsl1122 2018-08-08 10:58:18 +03:00
parent 886de6fe4d
commit 91aa730673
2 changed files with 50 additions and 63 deletions

View File

@ -6,6 +6,7 @@ import com.djrapitops.plan.data.store.mutators.formatting.Formatter;
import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.TimeAmount;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -17,16 +18,18 @@ import java.util.function.Supplier;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
public class DataContainer extends HashMap<Key, Supplier> { public class DataContainer {
private final Map<Key, Supplier> map;
private long timeToLive; private long timeToLive;
public DataContainer() { public DataContainer() {
timeToLive = TimeAmount.SECOND.ms() * 30L; this(TimeAmount.SECOND.ms() * 30L);
} }
public DataContainer(long timeToLive) { public DataContainer(long timeToLive) {
this.timeToLive = timeToLive; this.timeToLive = timeToLive;
map = new HashMap<>();
} }
/** /**
@ -44,11 +47,11 @@ public class DataContainer extends HashMap<Key, Supplier> {
if (supplier == null) { if (supplier == null) {
return; return;
} }
super.put(key, new CachingSupplier<>(supplier, timeToLive)); map.put(key, new CachingSupplier<>(supplier, timeToLive));
} }
public <T> Supplier<T> getSupplier(Key<T> key) { public <T> Supplier<T> getSupplier(Key<T> key) {
return (Supplier<T>) super.get(key); return (Supplier<T>) map.get(key);
} }
/** /**
@ -59,7 +62,7 @@ public class DataContainer extends HashMap<Key, Supplier> {
* @return true if found, false if not. * @return true if found, false if not.
*/ */
public <T> boolean supports(Key<T> key) { public <T> boolean supports(Key<T> key) {
return containsKey(key); return map.containsKey(key);
} }
/** /**
@ -87,7 +90,7 @@ public class DataContainer extends HashMap<Key, Supplier> {
} }
public <T> T getUnsafe(Key<T> key) { public <T> T getUnsafe(Key<T> key) {
Supplier supplier = super.get(key); Supplier supplier = map.get(key);
if (supplier == null) { if (supplier == null) {
throw new IllegalArgumentException("Unsupported Key: " + key.getKeyName()); throw new IllegalArgumentException("Unsupported Key: " + key.getKeyName());
} }
@ -104,30 +107,15 @@ public class DataContainer extends HashMap<Key, Supplier> {
return formatter.apply(value); return formatter.apply(value);
} }
/** public void putAll(Map<Key, Supplier> toPut) {
* Normal put method. map.putAll(toPut);
*
* @param key Key.
* @param value Supplier
* @return the previous value.
* @deprecated Use putSupplier instead for type safety.
*/
@Override
@Deprecated
public Supplier put(Key key, Supplier value) {
return super.put(key, value);
} }
/** public void putAll(DataContainer dataContainer) {
* Normal get method. putAll(dataContainer.map);
* }
* @param key Key.
* @return Supplier public void clear() {
* @deprecated Use getSupplier instead for types. map.clear();
*/
@Override
@Deprecated
public Supplier get(Object key) {
return super.get(key);
} }
} }

View File

@ -16,7 +16,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.function.Supplier;
/** /**
* Table that is in charge of storing common player data for all servers. * Table that is in charge of storing common player data for all servers.
@ -402,45 +401,45 @@ public class UsersTable extends UserIDTable {
} }
public DataContainer getUserInformation(UUID uuid) { public DataContainer getUserInformation(UUID uuid) {
Key<DataContainer> key = new Key<>(DataContainer.class, "plan_users_data"); Key<DataContainer> user_data = new Key<>(DataContainer.class, "plan_users_data");
DataContainer returnValue = new DataContainer(); DataContainer returnValue = new DataContainer();
Supplier<DataContainer> usersTableResults = () -> { returnValue.putSupplier(user_data, () -> getUserInformationDataContainer(uuid));
String sql = "SELECT * FROM " + tableName + " WHERE " + Col.UUID + "=?";
return query(new QueryStatement<DataContainer>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public DataContainer processResults(ResultSet set) throws SQLException {
DataContainer container = new DataContainer();
if (set.next()) {
long registered = set.getLong(Col.REGISTERED.get());
String name = set.getString(Col.USER_NAME.get());
int timesKicked = set.getInt(Col.TIMES_KICKED.get());
container.putRawData(PlayerKeys.REGISTERED, registered);
container.putRawData(PlayerKeys.NAME, name);
container.putRawData(PlayerKeys.KICK_COUNT, timesKicked);
}
return container;
}
});
};
returnValue.putSupplier(key, usersTableResults);
returnValue.putRawData(PlayerKeys.UUID, uuid); returnValue.putRawData(PlayerKeys.UUID, uuid);
returnValue.putSupplier(PlayerKeys.REGISTERED, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.REGISTERED)); returnValue.putSupplier(PlayerKeys.REGISTERED, () -> returnValue.getUnsafe(user_data).getValue(PlayerKeys.REGISTERED).orElse(null));
returnValue.putSupplier(PlayerKeys.NAME, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.NAME)); returnValue.putSupplier(PlayerKeys.NAME, () -> returnValue.getUnsafe(user_data).getValue(PlayerKeys.NAME).orElse(null));
returnValue.putSupplier(PlayerKeys.KICK_COUNT, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.KICK_COUNT)); returnValue.putSupplier(PlayerKeys.KICK_COUNT, () -> returnValue.getUnsafe(user_data).getValue(PlayerKeys.KICK_COUNT).orElse(null));
return returnValue; return returnValue;
} }
private DataContainer getUserInformationDataContainer(UUID uuid) {
String sql = "SELECT * FROM " + tableName + " WHERE " + Col.UUID + "=?";
return query(new QueryStatement<DataContainer>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public DataContainer processResults(ResultSet set) throws SQLException {
DataContainer container = new DataContainer();
if (set.next()) {
long registered = set.getLong(Col.REGISTERED.get());
String name = set.getString(Col.USER_NAME.get());
int timesKicked = set.getInt(Col.TIMES_KICKED.get());
container.putRawData(PlayerKeys.REGISTERED, registered);
container.putRawData(PlayerKeys.NAME, name);
container.putRawData(PlayerKeys.KICK_COUNT, timesKicked);
}
return container;
}
});
}
public enum Col implements Column { public enum Col implements Column {
ID("id"), ID("id"),
UUID("uuid"), UUID("uuid"),