Skip to content

Commit

Permalink
Fix issue with serializables
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinFactory committed Jun 12, 2020
1 parent 621de4e commit 314aa84
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.javafactorydev</groupId>
<artifactId>lightningstorage</artifactId>
<version>3.1.4-SNAPSHOT</version>
<version>3.1.4</version>
<name>LightningStorage</name>
<description>Store data in a better way</description>
<url>https://github.com/JavaFactoryDev/LightningStorage</url>
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/de/leonhard/storage/internal/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ default <T> Optional<T> find(final String key, final Class<T> type) {
* @param key The key your value should be associated with.
* @param value The value you want to set in your data-structure.
*/
default void setSerializable(final String key, final Object value) {
final Object data = LightningSerializer.deserialize(value);
default <T> void setSerializable(final String key, final T value) {
setSerializable(key, value, (Class<T>) value.getClass());
}

default <T> void setSerializable(final String key, final T value, Class<T> clazz) {
final Object data = LightningSerializer.deserialize(value, clazz);
set(key, data);
}

Expand Down Expand Up @@ -207,7 +211,8 @@ default List<Long> getLongList(final String key) {
* @param <E> EnumType
* @return Serialized Enum
*/
default <E extends Enum<E>> E getEnum(final String key,
default <E extends Enum<E>> E getEnum(
final String key,
final Class<E> enumType) {
final Object object = get(key);
Valid.checkBoolean(object instanceof String,
Expand All @@ -226,7 +231,7 @@ default <T> T getSerializable(final String key, final Class<T> clazz) {
if (!contains(key)) {
return null;
}
return LightningSerializer.serialize(get(key), clazz);
return LightningSerializer.deserialize(get(key), clazz);
}

// ----------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
import java.util.List;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Nullable;

/**
* Class to register serializable's
*/
@UtilityClass
public class LightningSerializer {

private final List<LightningSerializable<?>> serializables = Collections.synchronizedList(new ArrayList<>());
private final List<LightningSerializable<?>> serializables = Collections
.synchronizedList(new ArrayList<>());

public boolean isSerializable(final Class<?> clazz) {
return findSerializable(clazz) != null;
}

/**
* Register a serializable to our list
Expand All @@ -27,6 +33,7 @@ public void registerSerializable(@NonNull final LightningSerializable<?> seriali
serializables.add(serializable);
}

@Nullable
public LightningSerializable<?> findSerializable(final Class<?> clazz) {
for (final LightningSerializable<?> serializable : serializables) {
if (serializable.getClazz().equals(clazz)) {
Expand All @@ -37,19 +44,23 @@ public LightningSerializable<?> findSerializable(final Class<?> clazz) {
}

@SuppressWarnings("ALL")
public <T> T serialize(final Object obj, final Class<T> clazz) {
final LightningSerializable serializable = findSerializable(clazz);
/**e
* Method to save an object
*/
public Object serialize(final Object obj) {
final LightningSerializable serializable = findSerializable(obj.getClass());

Valid.notNull(
serializable,
"No serializable found for '" + clazz.getSimpleName() + "'");
return (T) serializable.serialize(obj);
"No serializable found for '" + obj.getClass().getSimpleName() + "'");
return serializable.serialize(obj);
}

public Object deserialize(final Object obj) {
final LightningSerializable<?> serializable = findSerializable(obj.getClass());
public <T> T deserialize(final Object raw, Class<T> type) {
final LightningSerializable<?> serializable = findSerializable(type);
Valid.notNull(
serializable,
"No serializable found for '" + obj.getClass().getSimpleName() + "'");
return serializable.deserialize(obj);
"No serializable found for '" + type.getSimpleName() + "'");
return (T) serializable.deserialize(raw);
}
}

0 comments on commit 314aa84

Please sign in to comment.