formatting for compressed data tree

This commit is contained in:
Sen 2025-05-28 18:12:28 +02:00
parent 815c52e5d7
commit b01b602728
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
16 changed files with 78 additions and 19 deletions

View file

@ -8,8 +8,12 @@ abstract class Tag {
abstract void write(DataOutput output) throws IOException;
abstract void read(DataInput input, int depth, SizeTracker tracker) throws IOException;
public abstract String toString();
protected abstract TagType getType();
abstract TagType getType();
public abstract Tag copy();
String toString(int indent, int depth) {
return this.toString();
}
public boolean equals(Object other) {
return other instanceof Tag && this.getType() == ((Tag)other).getType();

View file

@ -23,7 +23,7 @@ class TagBool extends Tag {
this.data = input.readBoolean();
}
protected TagType getType() {
TagType getType() {
return TagType.BOOLEAN;
}

View file

@ -23,7 +23,7 @@ class TagByte extends Tag {
this.data = input.readByte();
}
protected TagType getType() {
TagType getType() {
return TagType.BYTE;
}

View file

@ -28,7 +28,7 @@ class TagByteArray extends Tag {
input.readFully(this.data);
}
protected TagType getType() {
TagType getType() {
return TagType.BYTE_ARRAY;
}

View file

@ -23,7 +23,7 @@ class TagChar extends Tag {
this.data = input.readChar();
}
protected TagType getType() {
TagType getType() {
return TagType.CHAR;
}

View file

@ -23,7 +23,7 @@ class TagDouble extends Tag {
this.data = input.readDouble();
}
protected TagType getType() {
TagType getType() {
return TagType.DOUBLE;
}

View file

@ -23,7 +23,7 @@ class TagFloat extends Tag {
this.data = input.readFloat();
}
protected TagType getType() {
TagType getType() {
return TagType.FLOAT;
}

View file

@ -23,7 +23,7 @@ class TagInt extends Tag {
this.data = input.readInt();
}
protected TagType getType() {
TagType getType() {
return TagType.INT;
}

View file

@ -32,7 +32,7 @@ class TagIntArray extends Tag {
}
}
protected TagType getType() {
TagType getType() {
return TagType.INT_ARRAY;
}

View file

@ -5,6 +5,7 @@ import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import common.util.Util;
class TagList extends Tag {
private List<TagObject> list;
@ -37,7 +38,7 @@ class TagList extends Tag {
}
}
protected TagType getType() {
TagType getType() {
return TagType.LIST;
}
@ -46,11 +47,22 @@ class TagList extends Tag {
for(int z = 0; z < this.list.size(); z++) {
if(z != 0)
sb.append(',');
sb.append(z).append(':').append(this.list.get(z));
sb.append(this.list.get(z).toString());
}
return sb.append(']').toString();
}
String toString(int indent, int depth) {
StringBuilder sb = new StringBuilder("[");
String prefix = Util.repeatString(' ', indent * (depth + 1));
for(int z = 0; z < this.list.size(); z++) {
if(z != 0)
sb.append(',');
sb.append('\n').append(prefix).append(this.list.get(z).toString(indent, depth + 1));
}
return sb.append('\n').append(Util.repeatString(' ', indent * depth)).append('}').toString();
}
public Tag copy() {
TagList list = new TagList(new ArrayList<TagObject>(this.list.size()));
for(TagObject tag : this.list) {

View file

@ -23,7 +23,7 @@ class TagLong extends Tag {
this.data = input.readLong();
}
protected TagType getType() {
TagType getType() {
return TagType.LONG;
}

View file

@ -10,14 +10,18 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import common.collect.Lists;
import common.collect.Maps;
import common.util.Util;
import java.util.Set;
import java.util.function.Function;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@ -60,7 +64,7 @@ public class TagObject extends Tag {
return this.tags.keySet();
}
protected TagType getType() {
TagType getType() {
return TagType.OBJECT;
}
@ -248,17 +252,39 @@ public class TagObject extends Tag {
public void remove(String key) {
this.tags.remove(key);
}
private List<Entry<String, Tag>> getSortedEntries() {
List<Entry<String, Tag>> list = Lists.newArrayList(this.tags.entrySet());
Collections.sort(list, (e1, e2) -> e1.getValue().getType() == TagType.OBJECT && e2.getValue().getType() != TagType.OBJECT ? 1 : (
e2.getValue().getType() == TagType.OBJECT && e1.getValue().getType() != TagType.OBJECT ? -1 : (
(e1.getValue().getType() == TagType.LIST || e1.getValue().getType() == TagType.STRING_ARRAY) &&
e2.getValue().getType() != TagType.LIST && e2.getValue().getType() != TagType.STRING_ARRAY ? 1 : (
(e2.getValue().getType() == TagType.LIST || e2.getValue().getType() == TagType.STRING_ARRAY) &&
e1.getValue().getType() != TagType.LIST && e1.getValue().getType() != TagType.STRING_ARRAY ? -1 : e1.getKey().compareTo(e2.getKey())))));
return list;
}
public String toString() {
StringBuilder sb = new StringBuilder("{");
for(Entry<String, Tag> entry : this.tags.entrySet()) {
for(Entry<String, Tag> entry : this.getSortedEntries()) {
if(sb.length() != 1)
sb.append(',');
sb.append(entry.getKey()).append(':').append(entry.getValue());
sb.append(entry.getKey()).append(':').append(entry.getValue().toString());
}
return sb.append('}').toString();
}
String toString(int indent, int depth) {
StringBuilder sb = new StringBuilder("{");
String prefix = Util.repeatString(' ', indent * (depth + 1));
for(Entry<String, Tag> entry : this.getSortedEntries()) {
if(sb.length() != 1)
sb.append(',');
sb.append('\n').append(prefix).append(entry.getKey()).append(": ").append(entry.getValue().toString(indent, depth + 1));
}
return sb.append('\n').append(Util.repeatString(' ', indent * depth)).append('}').toString();
}
public boolean isEmpty() {
return this.tags.isEmpty();
}
@ -301,6 +327,10 @@ public class TagObject extends Tag {
}
}
}
public String format(int indent) {
return indent == 0 ? this.toString() : this.toString(indent, 0);
}
public static void write(TagObject tag, DataOutput out) throws IOException {
tag.write(out);

View file

@ -23,7 +23,7 @@ class TagShort extends Tag {
this.data = input.readShort();
}
protected TagType getType() {
TagType getType() {
return TagType.SHORT;
}

View file

@ -27,7 +27,7 @@ class TagString extends Tag {
tracker.read(2 * this.data.length());
}
protected TagType getType() {
TagType getType() {
return TagType.STRING;
}

View file

@ -5,6 +5,8 @@ import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import common.util.Util;
class TagStringArray extends Tag {
private String[] data;
@ -32,7 +34,7 @@ class TagStringArray extends Tag {
}
}
protected TagType getType() {
TagType getType() {
return TagType.STRING_ARRAY;
}
@ -46,6 +48,17 @@ class TagStringArray extends Tag {
return sb.append("]").toString();
}
String toString(int indent, int depth) {
StringBuilder sb = new StringBuilder("[");
String prefix = Util.repeatString(' ', indent * (depth + 1));
for(int z = 0; z < this.data.length; z++) {
if(z != 0)
sb.append(',');
sb.append('\n').append(prefix).append("\"" + this.data[z].replace("\"", "\\\"") + "\"");
}
return sb.append('\n').append(Util.repeatString(' ', indent * depth)).append("]").toString();
}
public Tag copy() {
String[] data = new String[this.data.length];
System.arraycopy(this.data, 0, data, 0, this.data.length);