Skip to content

Commit

Permalink
Implementation of attribute maps (Deprecated)
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Sep 18, 2024
1 parent 9c7a155 commit b01568a
Show file tree
Hide file tree
Showing 6 changed files with 734 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,71 @@
package org.imesense.dynamicspawncontrol.technical.attributefactory;

public class Attribute
/**
*
* @param <T>
*/
public final class Attribute<T>
{
/**
*
*/
private final boolean multiKey;

/**
*
*/
private final AttributeKey<T> key;

/**
*
* @param key
* @param multiKey
*/
public Attribute(AttributeKey<T> key, boolean multiKey)
{
this.key = key;
this.multiKey = multiKey;
}

/**
*
* @param key
* @return
* @param <T>
*/
@SuppressWarnings("unchecked")
public static <T> Attribute<Object> create(AttributeKey<T> key)
{
return (Attribute<Object>) new Attribute<>(key, false);
}

/**
*
* @param key
* @return
* @param <T>
*/
@SuppressWarnings("unchecked")
public static <T> Attribute<Object> createMulti(AttributeKey<T> key)
{
return (Attribute<Object>) new Attribute<>(key, true);
}

/**
*
* @return
*/
public AttributeKey<T> getKey()
{
return this.key;
}

/**
*
* @return
*/
public boolean isMulti()
{
return this.multiKey;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
package org.imesense.dynamicspawncontrol.technical.attributefactory;

public class AttributeKey
import javax.annotation.Nonnull;

public final class AttributeKey<T>
{
private final String name;

private final AttributeType<T> type;

public AttributeKey(@Nonnull AttributeType<T> type,
@Nonnull String name)
{
this.type = type;
this.name = name;
}

@Nonnull
public static <T> AttributeKey<T> create(@Nonnull AttributeType<T> type,
@Nonnull String code)
{
return new AttributeKey<>(type, code);
}

@Nonnull
public AttributeType<T> getType()
{
return this.type;
}

@Nonnull
public String getName()
{
return this.name;
}

@Override
public String toString()
{
return "Key(" + this.name + ')';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,158 @@
package org.imesense.dynamicspawncontrol.technical.attributefactory;

public class AttributeMap
import javax.annotation.Nonnull;
import java.util.*;

/**
*
* @param <T>
*/
public final class AttributeMap<T>
{
/**
*
*/
private final HashMap<AttributeKey<?>, Object> values = new HashMap<>();

/**
*
* @param key
* @return
*/
public boolean has(@Nonnull AttributeKey<?> key)
{
return this.values.containsKey(key);
}

/**
*
* @param key
* @param value
*/
public void set(@Nonnull AttributeKey<T> key, T value)
{
values.put(key, value);
}

/**
*
* @param key
* @param value
*/
public void setNonnull(@Nonnull AttributeKey<T> key, T value)
{
if (value != null)
{
this.values.put(key, value);
}
}

/**
*
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public T get(@Nonnull AttributeKey<?> key)
{
return (T) this.values.get(key);
}

/**
*
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public Optional<T> getOptional(@Nonnull AttributeKey<T> key)
{
return Optional.ofNullable((T) this.values.get(key));
}

/**
*
* @param key
* @param value
*/
@SuppressWarnings("unchecked")
public void addList(@Nonnull AttributeKey<?> key, T value)
{
if (!this.values.containsKey(key))
{
this.values.put(key, new ArrayList<>());
}

List<T> list = (List<T>) this.values.get(key);
list.add(value);
}

/**
*
* @param key
* @param value
*/
@SuppressWarnings("unchecked")
public void addListNonnull(@Nonnull AttributeKey<T> key, T value)
{
if (value == null)
{
return;
}

if (!this.values.containsKey(key))
{
this.values.put(key, new ArrayList<>());
}

List<T> list = (List<T>) this.values.get(key);
list.add(value);
}

/**
*
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public List<String> getList(@Nonnull AttributeKey<?> key)
{
if (!this.values.containsKey(key))
{
return Collections.emptyList();
}

return (List<String>) this.values.get(key);
}

/**
*
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public List<Integer> getListI(@Nonnull AttributeKey<?> key)
{
if (!this.values.containsKey(key))
{
return Collections.emptyList();
}

return (List<Integer>) this.values.get(key);
}

/**
*
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public List<AttributeMap<String>> getListA(@Nonnull AttributeKey<?> key)
{
if (!this.values.containsKey(key))
{
return Collections.emptyList();
}

return (List<AttributeMap<String>>) this.values.get(key);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,121 @@
package org.imesense.dynamicspawncontrol.technical.attributefactory;

public class AttributeMapFactory
import com.google.gson.*;
import org.imesense.dynamicspawncontrol.technical.customlibrary.JsonServices;

import java.util.*;
import javax.annotation.Nonnull;
import java.util.function.Function;

/**
*
* @param <T>
*/
public final class AttributeMapFactory<T>
{
/**
*
*/
private final ArrayList<Attribute<T>> _attributes = new ArrayList<>();

/**
*
* @param attribute
* @return
*/
public AttributeMapFactory<T> attribute(@Nonnull Attribute<T> attribute)
{
this._attributes.add(attribute);
return this;
}

/**
*
* @param element
* @return
*/
@Nonnull
@SuppressWarnings("unchecked")
public AttributeMap<T> parse(@Nonnull JsonElement element)
{
AttributeMap<T> map = new AttributeMap<>();
JsonObject jsonObject = element.getAsJsonObject();

for (Attribute<T> attribute : _attributes)
{
AttributeKey<T> key = attribute.getKey();
AttributeType<T> type = key.getType();

if (attribute.isMulti())
{
Map<AttributeType<T>, Function<JsonElement, Object>> transformers = new HashMap<>();

transformers.put((AttributeType<T>)AttributeType.INTEGER, JsonElement::getAsInt);
transformers.put((AttributeType<T>)AttributeType.FLOAT, JsonElement::getAsFloat);
transformers.put((AttributeType<T>)AttributeType.BOOLEAN, JsonElement::getAsBoolean);
transformers.put((AttributeType<T>)AttributeType.STRING, JsonElement::getAsString);
transformers.put((AttributeType<T>)AttributeType.JSON, JsonElement::toString);

JsonServices.getElement(jsonObject, key.getName())
.ifPresent(e ->
JsonServices.asArrayOrSingle(e)
.map(transformers.getOrDefault(type, x -> "INVALID"))
.forEach(s -> map.addListNonnull(key, (T) s)));
}
else
{
if (type == AttributeType.INTEGER)
{
map.setNonnull(key, (T) JsonServices.parseInt(jsonObject, key.getName()));
}
else if (type == AttributeType.FLOAT)
{
map.setNonnull(key, (T) JsonServices.parseFloat(jsonObject, key.getName()));
}
else if (type == AttributeType.BOOLEAN)
{
map.setNonnull(key, (T) JsonServices.parseBool(jsonObject, key.getName()));
}
else if (type == AttributeType.STRING)
{
if (jsonObject.has(key.getName()))
{
map.setNonnull(key, (T)jsonObject.get(key.getName()).getAsString());
}
}
else if (type == AttributeType.JSON)
{
if (jsonObject.has(key.getName()))
{
JsonElement el = jsonObject.get(key.getName());

if (el.isJsonObject())
{
JsonObject obj = el.getAsJsonObject();
map.setNonnull(key, (T)obj.toString());
}
else if (el.isJsonPrimitive())
{
JsonPrimitive prim = el.getAsJsonPrimitive();

if (prim.isString())
{
map.setNonnull(key, (T)prim.getAsString());
}
else if (prim.isNumber())
{
map.setNonnull(key, (T)("" + prim.getAsInt()));
}
else
{
throw new RuntimeException("Неверный тип для ключа '" + key.getName() + "'!");
}
}
}
}
}
}

return map;
}
}
Loading

0 comments on commit b01568a

Please sign in to comment.