File tree Expand file tree Collapse file tree 9 files changed +139
-25
lines changed
main/java/com/thealgorithms/others/cn
test/java/com/thealgorithms/others/cn Expand file tree Collapse file tree 9 files changed +139
-25
lines changed Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+ public class ComputeHammingHandler extends HammingHandler {
3
+
4
+ @ Override
5
+ public Object handle (String bitsStrA , String bitsStrB ) {
6
+ int totalErrorBitCount = 0 ;
7
+ for (int i = 0 ; i < bitsStrA .length (); i ++) {
8
+ totalErrorBitCount += bitsStrA .charAt (i ) == bitsStrB .charAt (i ) ? 0 : 1 ;
9
+ }
10
+ return totalErrorBitCount ;
11
+ }
12
+ }
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .others .cn ;
2
2
3
3
public final class HammingDistance {
4
- private HammingDistance () {
5
- }
6
4
7
- private static void checkChar (char inChar ) {
8
- if (inChar != '0' && inChar != '1' ) {
9
- throw new IllegalArgumentException ("Input must be a binary string." );
10
- }
11
- }
5
+ private HammingDistance () {
6
+ }
12
7
13
- public static int compute (char charA , char charB ) {
14
- checkChar (charA );
15
- checkChar (charB );
16
- return charA == charB ? 0 : 1 ;
17
- }
8
+ public static int compute (String bitsStrA , String bitsStrB ) {
9
+ HammingHandler chain = new ValidateBinaryHandler ();
10
+ chain .setNext (new ValidateLengthHandler ()).setNext (new ComputeHammingHandler ());
18
11
19
- public static int compute (String bitsStrA , String bitsStrB ) {
20
- if (bitsStrA .length () != bitsStrB .length ()) {
21
- throw new IllegalArgumentException ("Input strings must have the same length." );
22
- }
23
-
24
- int totalErrorBitCount = 0 ;
25
-
26
- for (int i = 0 ; i < bitsStrA .length (); i ++) {
27
- totalErrorBitCount += compute (bitsStrA .charAt (i ), bitsStrB .charAt (i ));
28
- }
29
-
30
- return totalErrorBitCount ;
31
- }
12
+ return (int ) chain .handle (bitsStrA , bitsStrB );
13
+ }
32
14
}
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+ public abstract class HammingHandler {
3
+ protected HammingHandler next ;
4
+
5
+ public HammingHandler setNext (HammingHandler next ) {
6
+ this .next = next ;
7
+ return next ;
8
+ }
9
+
10
+ public abstract Object handle (String bitsStrA , String bitsStrB );
11
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+ public class ValidateBinaryHandler extends HammingHandler {
3
+
4
+ private void checkBinary (String str ) {
5
+ for (char c : str .toCharArray ()) {
6
+ if (c != '0' && c != '1' ) {
7
+ throw new IllegalArgumentException ("Input must be a binary string." );
8
+ }
9
+ }
10
+ }
11
+
12
+ @ Override
13
+ public Object handle (String bitsStrA , String bitsStrB ) {
14
+ checkBinary (bitsStrA );
15
+ checkBinary (bitsStrB );
16
+ return next .handle (bitsStrA , bitsStrB );
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+ public class ValidateLengthHandler extends HammingHandler {
3
+
4
+ @ Override
5
+ public Object handle (String bitsStrA , String bitsStrB ) {
6
+ if (bitsStrA .length () != bitsStrB .length ()) {
7
+ throw new IllegalArgumentException ("Input strings must have the same length." );
8
+ }
9
+ return next .handle (bitsStrA , bitsStrB );
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+
3
+ import org .assertj .core .api .Assertions ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ public class ComputeHammingHandlerTest {
7
+
8
+ private final ComputeHammingHandler handler = new ComputeHammingHandler ();
9
+
10
+ @ Test
11
+ public void testHammingDistanceComputation () {
12
+ Object result = handler .handle ("1101" , "1001" );
13
+ Assertions .assertThat (result ).isEqualTo (1 );
14
+ }
15
+
16
+ @ Test
17
+ public void identicalStringsShouldReturnZero () {
18
+ Object result = handler .handle ("1111" , "1111" );
19
+ Assertions .assertThat (result ).isEqualTo (0 );
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+ public class MockHammingHandler extends HammingHandler {
3
+ private final Object returnValue ;
4
+
5
+ public MockHammingHandler (Object returnValue ) {
6
+ this .returnValue = returnValue ;
7
+ }
8
+
9
+ @ Override
10
+ public Object handle (String a , String b ) {
11
+ return returnValue ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+
3
+ import org .assertj .core .api .Assertions ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ public class ValidateBinaryHandlerTest {
7
+
8
+ private final ValidateBinaryHandler handler = new ValidateBinaryHandler ();
9
+
10
+ @ Test
11
+ public void validBinaryStringsShouldPass () {
12
+ handler .setNext (new MockHammingHandler ("PASSED" ));
13
+ Object result = handler .handle ("1010" , "0110" );
14
+ Assertions .assertThat (result ).isEqualTo ("PASSED" );
15
+ }
16
+
17
+ @ Test
18
+ public void invalidBinaryShouldThrowException () {
19
+ Assertions .assertThatThrownBy (() -> handler .handle ("10A0" , "0110" ))
20
+ .isInstanceOf (IllegalArgumentException .class )
21
+ .hasMessageContaining ("binary string" );
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .others .cn ;
2
+
3
+ import org .assertj .core .api .Assertions ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ public class ValidateLengthHandlerTest {
7
+
8
+ private final ValidateLengthHandler handler = new ValidateLengthHandler ();
9
+
10
+ @ Test
11
+ public void validLengthShouldPass () {
12
+ handler .setNext (new MockHammingHandler ("LENGTH_OK" ));
13
+ Object result = handler .handle ("1010" , "0110" );
14
+ Assertions .assertThat (result ).isEqualTo ("LENGTH_OK" );
15
+ }
16
+
17
+ @ Test
18
+ public void mismatchedLengthShouldThrowException () {
19
+ Assertions .assertThatThrownBy (() -> handler .handle ("101" , "10" ))
20
+ .isInstanceOf (IllegalArgumentException .class )
21
+ .hasMessageContaining ("same length" );
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments