Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methods to CustomValue and its implementations #338

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/net/fabricmc/loader/api/metadata/CustomValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package net.fabricmc.loader.api.metadata;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* Represents a custom value in the {@code fabric.mod.json}.
Expand Down Expand Up @@ -91,6 +95,16 @@ interface CvObject extends CustomValue, Iterable<Map.Entry<String, CustomValue>>
* @return the value associated, or {@code null} if no such value is present
*/
CustomValue get(String key);

/**
* Returns a sequential {@link Stream} with this iterable as its source.
*/
Stream<Map.Entry<String, CustomValue>> stream();

/**
* Returns the set of keys in this custom value.
*/
Set<String> keys();
BoogieMonster1O1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -110,6 +124,11 @@ interface CvArray extends CustomValue, Iterable<CustomValue> {
* @throws IndexOutOfBoundsException if the index is not within {{@link #size()}}
*/
CustomValue get(int index);

/**
* Returns a sequential {@link Stream} with this iterable as its source.
*/
Stream<CustomValue> stream();
}

/**
Expand Down
183 changes: 183 additions & 0 deletions src/main/java/net/fabricmc/loader/metadata/CustomValueImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.lib.gson.JsonReader;
Expand Down Expand Up @@ -157,6 +160,55 @@ public CustomValue get(String key) {
public Iterator<Entry<String, CustomValue>> iterator() {
return entries.entrySet().iterator();
}

@Override
public Stream<Entry<String, CustomValue>> stream() {
return StreamSupport.stream(this.spliterator(), false);
}

@Override
public Set<String> keys() {
return this.entries.keySet();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof ObjectImpl)) {
return false;
}

ObjectImpl object = (ObjectImpl) o;
return this.entries.equals(object.entries);
}

@Override
public int hashCode() {
return this.entries.hashCode();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
Iterator<String> itr = this.keys().iterator();

while (itr.hasNext()) {
String key = itr.next();
// Key
sb.append("\"").append(key).append("\"").append(":");
// Value
sb.append(this.get(key).toString());

if (itr.hasNext()) {
sb.append(",");
}
}

sb.append("}");
return sb.toString();
}
}

private static final class ArrayImpl extends CustomValueImpl implements CvArray {
Expand Down Expand Up @@ -185,6 +237,56 @@ public CustomValue get(int index) {
public Iterator<CustomValue> iterator() {
return entries.iterator();
}

@Override
public Stream<CustomValue> stream() {
return entries.stream();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof ArrayImpl)) {
return false;
}

ArrayImpl that = (ArrayImpl) o;
return this.entries.equals(that.entries);
}

@Override
public int hashCode() {
return this.entries.hashCode();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
Iterator<CustomValue> itr = this.iterator();

while (itr.hasNext()) {
CustomValue value = itr.next();

if (value.getType() == CvType.STRING) {
sb.append("\"");
}

sb.append(value.toString());

if (value.getType() == CvType.STRING) {
sb.append("\"");
}

if (itr.hasNext()) {
sb.append(",");
}
}

sb.append("]");
return sb.toString();
}
}

private static final class StringImpl extends CustomValueImpl {
Expand All @@ -198,6 +300,28 @@ public StringImpl(String value) {
public CvType getType() {
return CvType.STRING;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof StringImpl)) {
return false;
}

StringImpl string = (StringImpl) o;
return this.value.equals(string.value);
}

@Override
public int hashCode() {
return this.value.hashCode();
}

@Override
public String toString() {
return this.value;
}
}

private static final class NumberImpl extends CustomValueImpl {
Expand All @@ -211,6 +335,28 @@ public NumberImpl(Number value) {
public CvType getType() {
return CvType.NUMBER;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof NumberImpl)) {
return false;
}

NumberImpl number = (NumberImpl) o;
return this.value.equals(number.value);
}

@Override
public int hashCode() {
return this.value.hashCode();
}

@Override
public String toString() {
return String.valueOf(this.value);
}
}

private static final class BooleanImpl extends CustomValueImpl {
Expand All @@ -224,12 +370,49 @@ public BooleanImpl(boolean value) {
public CvType getType() {
return CvType.BOOLEAN;
}

@Override
public int hashCode() {
return Boolean.hashCode(this.value);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof BooleanImpl)) {
return false;
}

BooleanImpl aBoolean = (BooleanImpl) o;
return this.value == aBoolean.value;
}

@Override
public String toString() {
return String.valueOf(this.value);
}
}

private static final class NullImpl extends CustomValueImpl {
@Override
public CvType getType() {
return CvType.NULL;
}

@Override
public boolean equals(Object obj) {
return this == NULL;
BoogieMonster1O1 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public int hashCode() {
return System.identityHashCode(null);
}

@Override
public String toString() {
return "null";
}
}
}