Skip to content

Commit

Permalink
Merge branch 'refs/heads/dev' into feat-first-integration
Browse files Browse the repository at this point in the history
# Conflicts:
#	common/base/src/main/java/it/angrybear/yagl/utils/ObjectUtils.java
#	item/bukkit/src/test/java/it/angrybear/yagl/listeners/PersistentListenerTest.java
  • Loading branch information
fulminazzo committed Apr 26, 2024
2 parents 827f622 + e45408b commit c625d55
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
47 changes: 47 additions & 0 deletions common/base/src/main/java/it/angrybear/yagl/utils/ObjectUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.angrybear.yagl.utils;

import it.fulminazzo.fulmicollection.objects.Refl;
import it.fulminazzo.fulmicollection.structures.tuples.Tuple;
import it.fulminazzo.fulmicollection.utils.ReflectionUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -20,6 +21,52 @@
@SuppressWarnings("unchecked")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ObjectUtils {
private static final String EMPTY_IDENTIFIER = "";

/**
* Prints the given object in a JSON format.
* If the object (or an object contained in it) is "empty",
* it will be printed as {@link #EMPTY_IDENTIFIER}.
*
* @param object the object
* @return the output
*/
public static String printAsJSON(@Nullable Object object) {
if (object == null) return EMPTY_IDENTIFIER;
else if (object instanceof Enum<?>) return ((Enum<?>) object).name();
else if (object instanceof String) {
String s = object.toString();
if (s.isEmpty()) return EMPTY_IDENTIFIER;
else return String.format("\"%s\"", s);
} else if (object instanceof Number) {
Number n = (Number) object;
if (n.doubleValue() > 0) return n.toString();
else return EMPTY_IDENTIFIER;
} else if (ReflectionUtils.isPrimitiveOrWrapper(object.getClass())) return object.toString();
else if (object instanceof Collection) {
Collection<?> collection = (Collection<?>) object;
String output = collection.stream().map(ObjectUtils::printAsJSON).collect(Collectors.joining(", "));
if (output.matches("(, )*")) return EMPTY_IDENTIFIER;
else return String.format("[%s]", output);
} else if (!(object instanceof Map)) {
Map<Object, Object> map = new LinkedHashMap<>();
Refl<?> refl = new Refl<>(object);
for (final Field field : refl.getNonStaticFields()) {
Object obj = refl.getFieldObject(field);
map.put(field.getName(), obj);
}
object = map;
}
Map<?, ?> map = (Map<?, ?>) object;
StringBuilder output = new StringBuilder();
map.entrySet().stream()
.map(e -> new Tuple<>(printAsJSON(e.getKey()), printAsJSON(e.getValue())))
.filter(t -> !t.getKey().equals(EMPTY_IDENTIFIER) && !t.getValue().equals(EMPTY_IDENTIFIER))
.forEach(t -> output.append(t.getKey()).append(": ").append(t.getValue()).append(", "));
String result = output.toString();
if (result.matches("(: , )*")) return EMPTY_IDENTIFIER;
else return String.format("{%s}", result.substring(0, result.length() - 2));
}

/**
* Copies the given object to a new one.
Expand Down
4 changes: 2 additions & 2 deletions item/base/src/main/java/it/angrybear/yagl/items/ItemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import it.angrybear.yagl.items.fields.ItemFlag;
import it.angrybear.yagl.structures.EnchantmentSet;
import it.angrybear.yagl.utils.MessageUtils;
import it.angrybear.yagl.utils.ObjectUtils;
import it.angrybear.yagl.wrappers.Enchantment;
import it.fulminazzo.fulmicollection.objects.FieldEquable;
import it.fulminazzo.fulmicollection.objects.Printable;
import it.fulminazzo.fulmicollection.utils.ReflectionUtils;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -118,6 +118,6 @@ protected Class<? extends FieldEquable> clazz() {

@Override
public @NotNull String toString() {
return Printable.convertToJson(this);
return ObjectUtils.printAsJSON(this);
}
}
24 changes: 22 additions & 2 deletions item/base/src/test/java/it/angrybear/yagl/items/ItemImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import it.angrybear.yagl.items.fields.ItemField;
import it.angrybear.yagl.items.fields.ItemFlag;
import it.fulminazzo.fulmicollection.objects.Printable;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

class ItemImplTest {
Expand Down Expand Up @@ -41,11 +44,28 @@ void testSimilarity() {

@Test
void testPrint() {
assertDoesNotThrow(new ItemImpl()
Item item = new ItemImpl()
.setMaterial("material")
.setAmount(2)
.setDurability(4)
.setDisplayName("Hello world")
.addLore("lore")
.setCustomModelData(2)
.addEnchantments("enchant1", "enchant2")
.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)::toString);
.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
.setUnbreakable(true);
String output = item.toString();
String expected = Printable.convertToJson(item);
assertEquals(expected, output);
}

@Test
void testStrippedPrint() {
String output = new ItemImpl()
.setMaterial("stone")
.setAmount(2).toString();
for (String s : Arrays.asList("durability", "displayName", "lore", "enchantments", "itemFlags", "customModelData"))
assertFalse(output.contains(s), String.format("'%s' should not contain %s", output, s));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ void testSettersRanges(ConsumerException<MockWrapper> consumer, Integer min, Int

final String message = e.getMessage();
assertNotNull(message, "Error message should have not been null");
System.out.println(message);

if (min != null) {
assertTrue(message.contains(String.valueOf(min)), "Error message should contain min value");
Expand Down

0 comments on commit c625d55

Please sign in to comment.