Skip to content

Commit

Permalink
misc optimization (#37)
Browse files Browse the repository at this point in the history
various cleanup and optimizations. Remove most of the switch by string, use a identifier for known elements. Lazily create the attribute value.
  • Loading branch information
syjer authored Jun 16, 2024
1 parent 07351f9 commit a35bcb6
Show file tree
Hide file tree
Showing 32 changed files with 2,374 additions and 1,201 deletions.
19 changes: 14 additions & 5 deletions src/main/java/ch/digitalfondue/jfiveparse/AttributeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class AttributeNode {

String name;
String originalName;
String value;

private String value;
private ResizableCharBuilder valueBuilder;

//
String prefix;
Expand All @@ -36,8 +38,9 @@ public AttributeNode(String name, String value) {
this.value = value;
}

AttributeNode(String name, String originalName, String value, int attributeQuoteType) {
this(name, value);
AttributeNode(String name, String originalName, ResizableCharBuilder value, int attributeQuoteType) {
this.name = name;
this.valueBuilder = value;
this.originalName = originalName;
this.attributeQuoteType = attributeQuoteType;
}
Expand All @@ -46,6 +49,7 @@ public AttributeNode(String name, String value) {
this.name = a.name;
this.originalName = a.originalName;
this.value = a.value;
this.valueBuilder = a.valueBuilder;
this.prefix = a.prefix;
this.namespace = a.namespace;
this.attributeQuoteType = a.attributeQuoteType;
Expand All @@ -58,10 +62,15 @@ public AttributeNode(String name, String value, String prefix, String namespace)
}

public String getValue() {
if (value == null && valueBuilder != null) {
value = valueBuilder.asString();
valueBuilder = null;
}
return value;
}

public void setValue(String value) {
this.valueBuilder = null;
this.value = value;
}

Expand All @@ -79,7 +88,7 @@ public String getName() {

@Override
public int hashCode() {
return Objects.hash(name, value, prefix, namespace);
return Objects.hash(name, getValue(), prefix, namespace);
}

@Override
Expand All @@ -91,7 +100,7 @@ public boolean equals(Object obj) {
if (obj instanceof AttributeNode) {
AttributeNode other = (AttributeNode) obj;
return name.equals(other.name) && //
value.equals(other.value) && //
getValue().equals(other.getValue()) && //
Objects.equals(prefix, other.prefix) && //
Objects.equals(namespace, other.namespace);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/digitalfondue/jfiveparse/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ public Iterator<AttributeNode> iterator() {
}

public String getNamedItem(String name) {
return attributes != null && attributes.containsKey(name) ? attributes.get(name).value : null;
return attributes != null && attributes.containsKey(name) ? attributes.get(name).getValue() : null;
}
}
10 changes: 10 additions & 0 deletions src/main/java/ch/digitalfondue/jfiveparse/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,33 @@
*/
public class Comment extends Node {

ResizableCharBuilder dataBuilder;
private String data;

public Comment(String data) {
this.data = data;
}

Comment(ResizableCharBuilder dataBuilder) {
this.dataBuilder = dataBuilder;
}

@Override
public byte getNodeType() {
return COMMENT_NODE;
}

public String getData() {
if (data == null && dataBuilder != null) {
data = dataBuilder.asString();
dataBuilder = null;
}
return data;
}

public void setData(String data) {
this.data = data;
this.dataBuilder = null;
}

/**
Expand Down
Loading

0 comments on commit a35bcb6

Please sign in to comment.