Skip to content

Commit

Permalink
Default field for Rename and ParsedElement#equals
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale-Ko committed Sep 9, 2023
1 parent b8a97c5 commit 08ab6c9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
14 changes: 13 additions & 1 deletion src/main/java/io/github/kale_ko/bjsl/elements/ParsedArray.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.kale_ko.bjsl.elements;

import io.github.kale_ko.bjsl.BJSL;
import java.util.LinkedList;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import io.github.kale_ko.bjsl.BJSL;

/**
* A wrapper for an ordered map used to represent an Array in most data formats
Expand Down Expand Up @@ -160,6 +160,18 @@ public String toString() {
return this.getClass().getSimpleName() + "=" + BJSL.stringifyJson(this);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof ParsedArray) {
return this.array.equals(((ParsedArray) obj).array);
}

return false;
}

@Override
public int hashCode() {
return this.array.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public abstract class ParsedElement {
@Override
public abstract String toString();

@Override
public abstract boolean equals(Object obj);

@Override
public abstract int hashCode();

Expand Down
18 changes: 14 additions & 4 deletions src/main/java/io/github/kale_ko/bjsl/elements/ParsedObject.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.kale_ko.bjsl.elements;

import io.github.kale_ko.bjsl.BJSL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import io.github.kale_ko.bjsl.BJSL;

/**
* A wrapper for an ordered map used to represent an Object in most data formats
Expand Down Expand Up @@ -130,9 +130,7 @@ public boolean has(@NotNull String key) {
* @since 1.0.0
*/
public void set(@NotNull String key, @NotNull ParsedElement value) {
if (this.object.containsKey(key)) {
this.object.remove(key);
}
this.object.remove(key);
this.object.put(key, value);
}

Expand All @@ -156,6 +154,18 @@ public String toString() {
return this.getClass().getSimpleName() + "=" + BJSL.stringifyJson(this);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof ParsedObject) {
return this.object.equals(((ParsedObject) obj).object);
}

return false;
}

@Override
public int hashCode() {
return this.object.hashCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.kale_ko.bjsl.elements;

import io.github.kale_ko.bjsl.BJSL;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import io.github.kale_ko.bjsl.BJSL;

/**
* A wrapper for a primitive object used to represent String/int/float/etc. values
Expand Down Expand Up @@ -539,6 +539,18 @@ public String toString() {
return this.getClass().getSimpleName() + "=" + BJSL.stringifyJson(this);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof ParsedPrimitive) {
return this.primitive.equals(((ParsedPrimitive) obj).primitive);
}

return false;
}

@Override
public int hashCode() {
return this.primitive != null ? this.primitive.hashCode() : 0;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/github/kale_ko/bjsl/parsers/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected Parser(@NotNull T factory, @NotNull V codec, @Nullable PrettyPrinter p
ObjectNode objectNode = (ObjectNode) node;
ParsedObject objectElement = ParsedObject.create();

objectNode.fieldNames().forEachRemaining((String subKey) -> {
objectNode.fieldNames().forEachRemaining(subKey -> {
toElements(objectElement, subKey, objectNode.get(subKey));
});

Expand All @@ -116,7 +116,7 @@ protected Parser(@NotNull T factory, @NotNull V codec, @Nullable PrettyPrinter p
ArrayNode arrayNode = (ArrayNode) node;
ParsedArray arrayElement = ParsedArray.create();

arrayNode.elements().forEachRemaining((JsonNode subNode) -> {
arrayNode.elements().forEachRemaining(subNode -> {
toElements(arrayElement, "root", subNode);
});

Expand Down Expand Up @@ -310,7 +310,7 @@ protected void toElements(@NotNull ParsedElement element, @NotNull String key, @
arrayElement.add(subElement);
}

objectNode.fieldNames().forEachRemaining((String subKey) -> {
objectNode.fieldNames().forEachRemaining(subKey -> {
toElements(subElement, subKey, objectNode.get(subKey));
});
} else if (node instanceof ArrayNode) {
Expand All @@ -325,7 +325,7 @@ protected void toElements(@NotNull ParsedElement element, @NotNull String key, @
arrayElement.add(subElement);
}

arrayNode.elements().forEachRemaining((JsonNode subNode) -> {
arrayNode.elements().forEachRemaining(subNode -> {
toElements(subElement, key, subNode);
});
} else if (node instanceof TextNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ public boolean getEnableDefaultTypeProcessors() {
} else if (annotation.annotationType() == DontSerialize.class) {
shouldSerialize = false;
} else if (annotation.annotationType() == Rename.class) {
subKey = ((Rename) annotation).name();
subKey = ((Rename) annotation).value();
}
}

Expand Down Expand Up @@ -1590,7 +1590,7 @@ public boolean getEnableDefaultTypeProcessors() {
}
}
} else if (annotation.annotationType() == Rename.class) {
subKey = ((Rename) annotation).name();
subKey = ((Rename) annotation).value();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
*
* @since 1.8.0
*/
String name();
String value();
}

0 comments on commit 08ab6c9

Please sign in to comment.