Skip to content

Commit 12424db

Browse files
committed
use Attributes.empty singleton when reading XML elements with no attributes
1 parent a7d4bd2 commit 12424db

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

src/main/java/org/xmlobjects/stream/XMLReader.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,13 @@ public Attributes getAttributes() throws XMLReadException {
328328
throw new XMLReadException("Illegal to call getAttributes when event is not START_ELEMENT.");
329329
}
330330

331+
int count = reader.getAttributeCount();
332+
if (count == 0) {
333+
return Attributes.empty();
334+
}
335+
331336
Attributes attributes = new Attributes();
332-
for (int i = 0; i < reader.getAttributeCount(); i++) {
337+
for (int i = 0; i < count; i++) {
333338
attributes.add(reader.getAttributeName(i), reader.getAttributeValue(i));
334339
}
335340

@@ -389,7 +394,7 @@ public String getMixedContent() throws XMLReadException {
389394

390395
public <T> ObjectBuilder<T> getOrCreateBuilder(Class<? extends ObjectBuilder<T>> type) throws ObjectBuildException {
391396
ObjectBuilder<?> cachedBuilder = builderCache.get(type);
392-
if (cachedBuilder != null && type.isAssignableFrom(cachedBuilder.getClass())) {
397+
if (cachedBuilder != null) {
393398
return type.cast(cachedBuilder);
394399
} else {
395400
try {

src/main/java/org/xmlobjects/xml/Attributes.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,36 @@
1212
import java.util.Map;
1313

1414
public class Attributes {
15-
private final Map<String, Map<String, TextContent>> attributes = new HashMap<>();
15+
private static final Attributes EMPTY = new Attributes(Map.of()) {
16+
@Override
17+
public void add(String namespaceURI, String localName, TextContent value) {
18+
throw new UnsupportedOperationException("Attributes.empty() is immutable.");
19+
}
20+
21+
@Override
22+
public void addAll(String namespaceURI, Map<String, TextContent> attributes) {
23+
throw new UnsupportedOperationException("Attributes.empty() is immutable.");
24+
}
25+
26+
@Override
27+
public boolean isImmutable() {
28+
return true;
29+
}
30+
};
31+
32+
private final Map<String, Map<String, TextContent>> attributes;
33+
34+
public static Attributes empty() {
35+
return EMPTY;
36+
}
37+
38+
public Attributes() {
39+
this(new HashMap<>());
40+
}
41+
42+
private Attributes(Map<String, Map<String, TextContent>> attributes) {
43+
this.attributes = attributes;
44+
}
1645

1746
public void add(String namespaceURI, String localName, TextContent value) {
1847
attributes.computeIfAbsent(namespaceURI, v -> new HashMap<>()).put(localName, value);
@@ -78,9 +107,7 @@ public boolean isEmpty() {
78107
return attributes.isEmpty();
79108
}
80109

81-
public Attributes copy() {
82-
Attributes copy = new Attributes();
83-
copy.attributes.putAll(attributes);
84-
return copy;
110+
public boolean isImmutable() {
111+
return false;
85112
}
86113
}

0 commit comments

Comments
 (0)