Skip to content

Commit 8538d27

Browse files
committed
Avoid unnecessary object creations
1 parent 3ccb913 commit 8538d27

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Copyright (c) 1998-2019, Brian Wellington
2+
Copyright (c) 2005 VeriSign. All rights reserved.
23
Copyright (c) 2019-2021, dnsjava authors
34

45
All rights reserved.

src/main/java/org/xbill/DNS/dnssec/ByteArrayComparator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
// Copyright (c) 2013-2021 Ingo Bauersachs
44
package org.xbill.DNS.dnssec;
55

6-
import java.util.Comparator;
6+
import lombok.experimental.UtilityClass;
77

88
/**
99
* This class implements a basic comparator for byte arrays. It is primarily useful for comparing
1010
* RDATA portions of DNS records in doing DNSSEC canonical ordering.
1111
*
1212
* @since 3.5
1313
*/
14-
final class ByteArrayComparator implements Comparator<Object> {
14+
@UtilityClass
15+
final class ByteArrayComparator {
1516
private static final int MAX_BYTE = 0xFF;
1617

1718
/** {@inheritDoc} */

src/main/java/org/xbill/DNS/dnssec/NSEC3ValUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,16 @@ private boolean nsec3Covers(NSEC3Record nsec3, Name zonename, byte[] hash) {
195195
byte[] next = nsec3.getNext();
196196

197197
// This is the "normal case: owner < next and owner < hash < next
198-
ByteArrayComparator bac = new ByteArrayComparator();
199-
if (bac.compare(owner, hash) < 0 && bac.compare(hash, next) < 0) {
198+
if (ByteArrayComparator.compare(owner, hash) < 0
199+
&& ByteArrayComparator.compare(hash, next) < 0) {
200200
return true;
201201
}
202202

203203
// this is the end of zone case: next <= owner AND (hash > owner OR hash < next)
204204
// Otherwise, the NSEC3 does not cover the hash.
205-
return bac.compare(next, owner) <= 0
206-
&& (bac.compare(hash, owner) > 0 || bac.compare(hash, next) < 0);
205+
return ByteArrayComparator.compare(next, owner) <= 0
206+
&& (ByteArrayComparator.compare(hash, owner) > 0
207+
|| ByteArrayComparator.compare(hash, next) < 0);
207208
}
208209

209210
/**
@@ -385,7 +386,6 @@ private boolean validIterations(SRRset nsec, KeyCache keyCache) {
385386
*/
386387
public boolean allNSEC3sIgnoreable(List<SRRset> nsec3s, KeyCache dnskeyRrset) {
387388
Map<Name, NSEC3Record> foundNsecs = new HashMap<>();
388-
ByteArrayComparator comp = new ByteArrayComparator();
389389
for (SRRset set : nsec3s) {
390390
for (Record r : set.rrs()) {
391391
NSEC3Record current = (NSEC3Record) r;
@@ -405,7 +405,7 @@ public boolean allNSEC3sIgnoreable(List<SRRset> nsec3s, KeyCache dnskeyRrset) {
405405
}
406406

407407
if (current.getSalt() != null
408-
&& comp.compare(current.getSalt(), previous.getSalt()) != 0) {
408+
&& ByteArrayComparator.compare(current.getSalt(), previous.getSalt()) != 0) {
409409
return true;
410410
}
411411
} else {

src/test/java/org/xbill/DNS/dnssec/TestByteArrayComparator.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@
66
import org.junit.jupiter.api.Test;
77

88
class TestByteArrayComparator {
9-
private final ByteArrayComparator c = new ByteArrayComparator();
109
private final byte[] b1 = new byte[] {0};
1110
private final byte[] b2 = new byte[] {0};
1211
private final byte[] b3 = new byte[] {1};
1312
private final byte[] b4 = new byte[] {1, 0};
1413

1514
@Test
1615
void testEquals() {
17-
assertEquals(0, c.compare(b1, b2));
16+
assertEquals(0, ByteArrayComparator.compare(b1, b2));
1817
}
1918

2019
@Test
2120
void testLessThan() {
22-
assertEquals(-1, c.compare(b2, b3));
23-
assertEquals(-1, c.compare(b1, b4));
21+
assertEquals(-1, ByteArrayComparator.compare(b2, b3));
22+
assertEquals(-1, ByteArrayComparator.compare(b1, b4));
2423
}
2524

2625
@Test
2726
void testGreaterThan() {
28-
assertEquals(1, c.compare(b3, b2));
29-
assertEquals(1, c.compare(b4, b1));
27+
assertEquals(1, ByteArrayComparator.compare(b3, b2));
28+
assertEquals(1, ByteArrayComparator.compare(b4, b1));
3029
}
3130
}

0 commit comments

Comments
 (0)