Skip to content

Commit 4e8b5d3

Browse files
Fixed Multiple If-else statements in BloomFilter.java using reflection api
1 parent f5ec12d commit 4e8b5d3

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ private static class Hash<T> {
117117
* @return the computed hash value
118118
*/
119119
public int compute(T key) {
120+
return index*contentHash(key);
121+
}
122+
123+
/**
124+
* Computes the ASCII value sum of the characters in a string.
125+
* <p>
126+
* This method iterates through each character of the string and accumulates
127+
* their ASCII values to produce a single integer value.
128+
* </p>
129+
*
130+
* @param word the string to compute
131+
* @return the sum of ASCII values of the characters in the string
132+
*/
133+
private int asciiString(String word) {
134+
int sum = 0;
135+
for (char c : word.toCharArray()) {
136+
sum += c;
137+
}
138+
return sum;
139+
}
140+
/**
141+
* Computes a content-based hash for arrays; falls back to ASCII-sum of String value otherwise.
142+
*/
143+
private int contentHash(Object key) {
120144
String keyString;
121145
if (key != null && key.getClass().isArray()) {
122146
if (key instanceof Object[] objects) {
@@ -137,25 +161,7 @@ public int compute(T key) {
137161
} else {
138162
keyString = String.valueOf(key);
139163
}
140-
return index * asciiString(String.valueOf(keyString));
141-
}
142-
143-
/**
144-
* Computes the ASCII value sum of the characters in a string.
145-
* <p>
146-
* This method iterates through each character of the string and accumulates
147-
* their ASCII values to produce a single integer value.
148-
* </p>
149-
*
150-
* @param word the string to compute
151-
* @return the sum of ASCII values of the characters in the string
152-
*/
153-
private int asciiString(String word) {
154-
int sum = 0;
155-
for (char c : word.toCharArray()) {
156-
sum += c;
157-
}
158-
return sum;
164+
return asciiString(String.valueOf(keyString));
159165
}
160166
}
161167
}

0 commit comments

Comments
 (0)