Skip to content

#6438 Improvement of Hamming Distance class #6447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.thealgorithms.others.cn;
public class ComputeHammingHandler extends HammingHandler {

@Override
public Object handle(String bitsStrA, String bitsStrB) {
int totalErrorBitCount = 0;
for (int i = 0; i < bitsStrA.length(); i++) {
totalErrorBitCount += bitsStrA.charAt(i) == bitsStrB.charAt(i) ? 0 : 1;
}
return totalErrorBitCount;
}
}
26 changes: 4 additions & 22 deletions src/main/java/com/thealgorithms/others/cn/HammingDistance.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
package com.thealgorithms.others.cn;

public final class HammingDistance {
private HammingDistance() {
}

private static void checkChar(char inChar) {
if (inChar != '0' && inChar != '1') {
throw new IllegalArgumentException("Input must be a binary string.");
}
}

public static int compute(char charA, char charB) {
checkChar(charA);
checkChar(charB);
return charA == charB ? 0 : 1;
private HammingDistance() {
}

public static int compute(String bitsStrA, String bitsStrB) {
if (bitsStrA.length() != bitsStrB.length()) {
throw new IllegalArgumentException("Input strings must have the same length.");
}

int totalErrorBitCount = 0;

for (int i = 0; i < bitsStrA.length(); i++) {
totalErrorBitCount += compute(bitsStrA.charAt(i), bitsStrB.charAt(i));
}
HammingHandler chain = new ValidateBinaryHandler();
chain.setNext(new ValidateLengthHandler()).setNext(new ComputeHammingHandler());

return totalErrorBitCount;
return (int) chain.handle(bitsStrA, bitsStrB);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/thealgorithms/others/cn/HammingHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thealgorithms.others.cn;
public abstract class HammingHandler {
protected HammingHandler next;

public HammingHandler setNext(HammingHandler next) {
this.next = next;
return next;
}

public abstract Object handle(String bitsStrA, String bitsStrB);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.thealgorithms.others.cn;
public class ValidateBinaryHandler extends HammingHandler {

private void checkBinary(String str) {
for (char c : str.toCharArray()) {
if (c != '0' && c != '1') {
throw new IllegalArgumentException("Input must be a binary string.");
}
}
}

@Override
public Object handle(String bitsStrA, String bitsStrB) {
checkBinary(bitsStrA);
checkBinary(bitsStrB);
return next.handle(bitsStrA, bitsStrB);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thealgorithms.others.cn;
public class ValidateLengthHandler extends HammingHandler {

@Override
public Object handle(String bitsStrA, String bitsStrB) {
if (bitsStrA.length() != bitsStrB.length()) {
throw new IllegalArgumentException("Input strings must have the same length.");
}
return next.handle(bitsStrA, bitsStrB);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.thealgorithms.others.cn;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class ComputeHammingHandlerTest {

private final ComputeHammingHandler handler = new ComputeHammingHandler();

@Test
public void testHammingDistanceComputation() {
Object result = handler.handle("1101", "1001");
Assertions.assertThat(result).isEqualTo(1);
}

@Test
public void identicalStringsShouldReturnZero() {
Object result = handler.handle("1111", "1111");
Assertions.assertThat(result).isEqualTo(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.thealgorithms.others.cn;
public class MockHammingHandler extends HammingHandler {
private final Object returnValue;

public MockHammingHandler(Object returnValue) {
this.returnValue = returnValue;
}

@Override
public Object handle(String a, String b) {
return returnValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.thealgorithms.others.cn;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class ValidateBinaryHandlerTest {

private final ValidateBinaryHandler handler = new ValidateBinaryHandler();

@Test
public void validBinaryStringsShouldPass() {
handler.setNext(new MockHammingHandler("PASSED"));
Object result = handler.handle("1010", "0110");
Assertions.assertThat(result).isEqualTo("PASSED");
}

@Test
public void invalidBinaryShouldThrowException() {
Assertions.assertThatThrownBy(() -> handler.handle("10A0", "0110")).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("binary string");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.thealgorithms.others.cn;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class ValidateLengthHandlerTest {

private final ValidateLengthHandler handler = new ValidateLengthHandler();

@Test
public void validLengthShouldPass() {
handler.setNext(new MockHammingHandler("LENGTH_OK"));
Object result = handler.handle("1010", "0110");
Assertions.assertThat(result).isEqualTo("LENGTH_OK");
}

@Test
public void mismatchedLengthShouldThrowException() {
Assertions.assertThatThrownBy(() -> handler.handle("101", "10")).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("same length");
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy