Skip to content

Commit

Permalink
refactor: Refactoring Implementation smells
Browse files Browse the repository at this point in the history
- Attributes: Extract method and Decompose conditional
- Range and SharedConstants: Introduce explaining variable
  • Loading branch information
Bhishman committed Mar 18, 2024
1 parent 3e626d5 commit e3dff9a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/jsoup/internal/SharedConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public final class SharedConstants {
public static final String EndRangeKey = "jsoup.end";

public static final int DefaultBufferSize = 1024 * 32;
public static final int HashPrime = 31;

private SharedConstants() {}
}
32 changes: 23 additions & 9 deletions src/main/java/org/jsoup/nodes/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,20 +555,34 @@ public int deduplicate(ParseSettings settings) {
return 0;
boolean preserve = settings.preserveAttributeCase();
int dupes = 0;
OUTER: for (int i = 0; i < keys.length; i++) {
for (int j = i + 1; j < keys.length; j++) {
if (keys[j] == null)
continue OUTER; // keys.length doesn't shrink when removing, so re-test
if ((preserve && keys[i].equals(keys[j])) || (!preserve && keys[i].equalsIgnoreCase(keys[j]))) {
dupes++;
remove(j);
j--;
}

for (int i = 0; i < keys.length; i++) {
dupes += removeDuplicatesFrom(i, preserve);
}

return dupes;
}

private int removeDuplicatesFrom(int index, boolean preserve) {
int dupes = 0;

for (int j = index + 1; j < keys.length; j++) {
if (keys[j] == null)
break; // keys.length doesn't shrink when removing, so re-test

if (isDuplicate(keys[index], keys[j], preserve)) {
dupes++;
remove(j);
j--;
}
}
return dupes;
}

private boolean isDuplicate(String key1, String key2, boolean preserve) {
return preserve ? key1.equals(key2) : key1.equalsIgnoreCase(key2);
}

private static class Dataset extends AbstractMap<String, String> {
private final Attributes attributes;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jsoup/nodes/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
int result = start.hashCode();
result = 31 * result + end.hashCode();
result = HashPrime * result + end.hashCode();
return result;
}

Expand Down Expand Up @@ -199,8 +199,8 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
int result = pos;
result = 31 * result + lineNumber;
result = 31 * result + columnNumber;
result = HashPrime * result + lineNumber;
result = HashPrime * result + columnNumber;
return result;
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ public Range valueRange() {

@Override public int hashCode() {
int result = nameRange.hashCode();
result = 31 * result + valueRange.hashCode();
result = HashPrime * result + valueRange.hashCode();
return result;
}
}
Expand Down

0 comments on commit e3dff9a

Please sign in to comment.