Skip to content

Commit

Permalink
feature: Added LuhnModnSnippet class to Algorithm package
Browse files Browse the repository at this point in the history
* Added LuhnModnSnippet class

* Added LuhnModnSnippetTest class

* Added LuhnModnSnippet class to README.md
  • Loading branch information
Karim-Ashraf1 committed Dec 3, 2024
1 parent 2205c4f commit 2900bfc
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 0 deletions.
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,134 @@ public class SieveOfEratosthenesSnippet {
}
}
```
### Luhn Mod N


```java
public class LuhnModnSnippet {


private LuhnModnSnippet() {
throw new IllegalStateException("LuhnModnSnippet class");
}


private static final String CODE_POINTS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


/**
* Generates a check character using the Luhn mod N algorithm.
*
* @param character the input string consisting of valid alphanumeric characters
* @return the generated check character
* @throws IllegalArgumentException if the input contains invalid characters
*/
public static int codePointFromCharacter(char character) {
int index = CODE_POINTS.indexOf(character);
if (index == -1) {
throw new IllegalArgumentException("Invalid character: " + character);
}
return index;
}


/**
* Converts a code point to its corresponding character.
*
* @param codePoint the code point to be converted
* @return the character representation of the code point
* @throws IllegalArgumentException if the code point is out of range.
*/
public static char characterFromCodePoint(int codePoint) {
if (codePoint < 0 || codePoint >= CODE_POINTS.length()) {
throw new IllegalArgumentException("Invalid code point: " + codePoint);
}
return CODE_POINTS.charAt(codePoint);
}


public static int numberOfValidInputCharacters() {
return CODE_POINTS.length();
}


/**
* Generates a check character for the given input string using the Luhn mod N algorithm.
*
* @param input the input string (non-empty)
* @return the generated check character
* @throws IllegalArgumentException if the input is null or empty
*/
public static char generateCheckCharacter(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}


int factor = 2;
int sum = 0;
int n = numberOfValidInputCharacters();


for (int i = input.length() - 1; i >= 0; i--) {
int codePoint = codePointFromCharacter(input.charAt(i));
int addend = factor * codePoint;


factor = (factor == 2) ? 1 : 2;


addend = (addend / n) + (addend % n);
sum += addend;
}


int remainder = sum % n;
int checkCodePoint = (n - remainder) % n;


return characterFromCodePoint(checkCodePoint);
}


/**
* Validates a check character by applying the Luhn mod N algorithm.
*
* @param input the input string (including the check character)
* @return true if the input passes validation, false otherwise
* @throws IllegalArgumentException if the input is null or empty
*/
public static boolean validateCheckCharacter(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}


int factor = 1;
int sum = 0;
int n = numberOfValidInputCharacters();


for (int i = input.length() - 1; i >= 0; i--) {
int codePoint = codePointFromCharacter(input.charAt(i));
int addend = factor * codePoint;


factor = (factor == 2) ? 1 : 2;


addend = (addend / n) + (addend % n);
sum += addend;
}


int remainder = sum % n;


return (remainder == 0);
}
}
```

## Array

Expand Down
154 changes: 154 additions & 0 deletions src/main/java/algorithm/LuhnModnSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/


package algorithm;


/**
* LuhnModnSnippet.
*/
public class LuhnModnSnippet {


private LuhnModnSnippet() {
throw new IllegalStateException("LuhnModnSnippet class");
}


private static final String CODE_POINTS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


/**
* Generates a check character using the Luhn mod N algorithm.
*
* @param character the input string consisting of valid alphanumeric characters
* @return the generated check character
* @throws IllegalArgumentException if the input contains invalid characters
*/
public static int codePointFromCharacter(char character) {
int index = CODE_POINTS.indexOf(character);
if (index == -1) {
throw new IllegalArgumentException("Invalid character: " + character);
}
return index;
}


/**
* Converts a code point to its corresponding character.
*
* @param codePoint the code point to be converted
* @return the character representation of the code point
* @throws IllegalArgumentException if the code point is out of range.
*/
public static char characterFromCodePoint(int codePoint) {
if (codePoint < 0 || codePoint >= CODE_POINTS.length()) {
throw new IllegalArgumentException("Invalid code point: " + codePoint);
}
return CODE_POINTS.charAt(codePoint);
}


public static int numberOfValidInputCharacters() {
return CODE_POINTS.length();
}


/**
* Generates a check character for the given input string using the Luhn mod N algorithm.
*
* @param input the input string (non-empty)
* @return the generated check character
* @throws IllegalArgumentException if the input is null or empty
*/
public static char generateCheckCharacter(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}


int factor = 2;
int sum = 0;
int n = numberOfValidInputCharacters();


for (int i = input.length() - 1; i >= 0; i--) {
int codePoint = codePointFromCharacter(input.charAt(i));
int addend = factor * codePoint;


factor = (factor == 2) ? 1 : 2;


addend = (addend / n) + (addend % n);
sum += addend;
}


int remainder = sum % n;
int checkCodePoint = (n - remainder) % n;


return characterFromCodePoint(checkCodePoint);
}


/**
* Validates a check character by applying the Luhn mod N algorithm.
*
* @param input the input string (including the check character)
* @return true if the input passes validation, false otherwise
* @throws IllegalArgumentException if the input is null or empty
*/
public static boolean validateCheckCharacter(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}


int factor = 1;
int sum = 0;
int n = numberOfValidInputCharacters();


for (int i = input.length() - 1; i >= 0; i--) {
int codePoint = codePointFromCharacter(input.charAt(i));
int addend = factor * codePoint;


factor = (factor == 2) ? 1 : 2;


addend = (addend / n) + (addend % n);
sum += addend;
}


int remainder = sum % n;


return (remainder == 0);
}
}
98 changes: 98 additions & 0 deletions src/test/java/algorithm/LuhnModnSnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/


package algorithm;


import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;






/**
* Tests for {@link LuhnModnSnippet#generateCheckCharacter(String)} and
* {@link LuhnModnSnippet#validateCheckCharacter(String)}.
*/
class LuhnModnSnippetTest {


/**
* Tests the full process of generating and validating a check character.
*/


@ParameterizedTest
@MethodSource("validInputProvider")
public void testGenerateCheckCharacter(String input) {
char checkCharacter = LuhnModnSnippet.generateCheckCharacter(input);
String fullInput = input + checkCharacter;
assertTrue(LuhnModnSnippet.validateCheckCharacter(fullInput),
"Validation should pass for the generated check character.");
}


@ParameterizedTest
@MethodSource("invalidInputProvider")
public void testInvalidInputs(String input, Character checkCharacter, boolean throwException) {
if (throwException) {
assertThrows(IllegalArgumentException.class, () ->
LuhnModnSnippet.generateCheckCharacter(input),
"Exception should be thrown for invalid input.");
assertThrows(IllegalArgumentException.class, () ->
LuhnModnSnippet.validateCheckCharacter(input),
"Exception should be thrown for invalid input.");
} else {
String fullInput = input + checkCharacter;
assertFalse(LuhnModnSnippet.validateCheckCharacter(fullInput),
"Validation should fail for a mismatched check character.");
}
}


private static Stream<String> validInputProvider() {
return Stream.of(
"HELLO",
"12345",
"A1B2C3"
);
}


private static Stream<Arguments> invalidInputProvider() {
return Stream.of(
Arguments.of("", null, true),
Arguments.of("WORLD", 'A', false)
);
}
}

0 comments on commit 2900bfc

Please sign in to comment.