Skip to content

Commit d74eb11

Browse files
committed
first commit
1 parent c71de3d commit d74eb11

20 files changed

+839
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
> [!NOTE]
2+
> I accept help to make the version of the other programming languages.
3+
4+
# Multiform-validator
5+
6+
## KOTLIN
7+
8+
In coming ...
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.multiform_validator;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public class Ascii {
6+
private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty.";
7+
8+
private Ascii() {
9+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
10+
}
11+
12+
/**
13+
* Check if a string is ASCII.
14+
*
15+
* @param value The string to check.
16+
* @return True if the string is ASCII, false otherwise.
17+
* @throws IllegalArgumentException If the input value is empty.
18+
*/
19+
public static boolean isAscii(@NotNull String value) {
20+
if (value.isBlank()) {
21+
throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY);
22+
}
23+
24+
return value.chars().allMatch(c -> c < 128);
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.multiform_validator;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public class Base64 {
6+
7+
private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty.";
8+
9+
private Base64() {
10+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
11+
}
12+
13+
/**
14+
* Check if a string is Base64.
15+
*
16+
* @param value The string to check.
17+
* @return True if the string is Base64, false otherwise.
18+
* @throws IllegalArgumentException If the input value is empty.
19+
*/
20+
public static boolean isBase64(@NotNull String value) {
21+
if (value.isBlank()) {
22+
throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY);
23+
}
24+
25+
return value.matches("^[a-zA-Z0-9+/]*={0,2}$");
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.multiform_validator;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public class CEP {
6+
private CEP() {
7+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
8+
}
9+
10+
/**
11+
* Verifica se um CEP é válido.
12+
*
13+
* @param cep CEP a ser verificado.
14+
* @return true se o CEP é válido, false caso contrário.
15+
*/
16+
public static boolean isCEP(@NotNull String cep) {
17+
if (cep.length() < 8 || cep.length() > 10) {
18+
return false;
19+
}
20+
21+
final String cepString = cep.replaceAll("\\D", "");
22+
23+
if (cepString.length() != 8) {
24+
return false;
25+
}
26+
27+
try {
28+
Integer.parseInt(cepString);
29+
} catch (NumberFormatException e) {
30+
return false;
31+
}
32+
33+
return true;
34+
}
35+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.multiform_validator
2+
3+
import java.util.*
4+
5+
class Cnpj private constructor() {
6+
init {
7+
throw IllegalStateException("Utility class")
8+
}
9+
10+
companion object {
11+
private const val INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."
12+
13+
/**
14+
* Validate CNPJ
15+
*
16+
* @param cnpj CNPJ to be validated
17+
* @return true if CNPJ is valid, false otherwise
18+
*/
19+
fun isValid(cnpj: String): Boolean {
20+
require(!cnpj.isBlank()) { INPUT_VALUE_CANNOT_BE_EMPTY }
21+
22+
if (cnpj.length != 14 && cnpj.length != 18) {
23+
return false
24+
}
25+
26+
try {
27+
val cnpjClean = cnpj.replace("\\D".toRegex(), "")
28+
29+
val cnpjArray = cnpjClean.chars().map { codePoint: Int -> Character.getNumericValue(codePoint) }.toArray()
30+
31+
val firstVerifier = calculateFirstVerifier(Arrays.copyOfRange(cnpjArray, 0, 12))
32+
val secondVerifier = calculateSecondVerifier(Arrays.copyOfRange(cnpjArray, 0, 13), firstVerifier)
33+
34+
return cnpjArray[12] == firstVerifier && cnpjArray[13] == secondVerifier
35+
} catch (e: Exception) {
36+
return false
37+
}
38+
}
39+
40+
private fun calculateFirstVerifier(cnpjBase: IntArray): Int {
41+
var sum = 0
42+
val weights = intArrayOf(5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2)
43+
44+
for (i in 0..11) {
45+
sum += cnpjBase[i] * weights[i]
46+
}
47+
48+
val remainder = sum % 11
49+
50+
return if (remainder < 2) 0 else 11 - remainder
51+
}
52+
53+
private fun calculateSecondVerifier(cnpjBase: IntArray, firstVerifier: Int): Int {
54+
var sum = 0
55+
val weights = intArrayOf(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2)
56+
57+
for (i in 0..12) {
58+
sum += cnpjBase[i] * weights[i]
59+
}
60+
61+
sum += firstVerifier * weights[12]
62+
val remainder = sum % 11
63+
64+
return if (remainder < 2) 0 else 11 - remainder
65+
}
66+
}
67+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.multiform_validator
2+
3+
class Cpf private constructor() {
4+
init {
5+
throw IllegalStateException("Utility class")
6+
}
7+
8+
companion object {
9+
private const val INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."
10+
11+
/**
12+
* Validate a CPF number.
13+
*
14+
* @param cpf The CPF number to be validated.
15+
* @return True if the CPF number is valid, false otherwise.
16+
*/
17+
fun isValid(cpf: String): Boolean {
18+
require(!cpf.isBlank()) { INPUT_VALUE_CANNOT_BE_EMPTY }
19+
20+
try {
21+
var baseNumber = 10
22+
var baseNumber2 = 11
23+
24+
var somaTotal = 0
25+
var somaTotal2 = 0
26+
27+
if (cpf.length != 11 && cpf.length != 14) {
28+
return false
29+
}
30+
31+
val cpfClean = cpf.replace("\\D".toRegex(), "")
32+
33+
if (cpfClean.chars().allMatch { c: Int -> c == cpfClean[0].code }) {
34+
return false
35+
}
36+
37+
var firstVerifier = 0
38+
var secondVerifier = 0
39+
40+
val ninthCharAsInt = cpfClean[9].toString().toInt()
41+
val tenthCharAsInt = cpfClean[10].toString().toInt()
42+
43+
for (repeater in 0..10) {
44+
val currentCharAsInt = cpfClean[repeater].toString().toInt()
45+
46+
val multiplicative = currentCharAsInt * baseNumber
47+
48+
baseNumber--
49+
somaTotal += multiplicative
50+
51+
val multiplicative2 = currentCharAsInt * baseNumber2
52+
53+
baseNumber2--
54+
somaTotal2 += multiplicative2
55+
56+
val verificationValue = somaTotal - ninthCharAsInt
57+
val verificationValue2 = somaTotal2 - tenthCharAsInt
58+
firstVerifier = 11 - (verificationValue % 11)
59+
secondVerifier = 11 - (verificationValue2 % 11)
60+
}
61+
if (firstVerifier > 9) {
62+
firstVerifier = 0
63+
}
64+
if (secondVerifier > 9) {
65+
secondVerifier = 0
66+
}
67+
68+
return firstVerifier == ninthCharAsInt && secondVerifier == tenthCharAsInt
69+
} catch (e: Exception) {
70+
return false
71+
}
72+
}
73+
}
74+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.multiform_validator;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.regex.Pattern;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
public class CreditCard {
9+
private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty.";
10+
11+
private CreditCard() {
12+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
13+
}
14+
15+
/**
16+
* Validate a credit card number.
17+
*
18+
* @param cardNumber The credit card number to be validated.
19+
* @return True if the credit card number is valid, false otherwise.
20+
*/
21+
public static boolean isCreditCardValid(@NotNull String cardNumber) {
22+
if (cardNumber.isBlank()) {
23+
throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY);
24+
}
25+
26+
final String cleanedCreditCardInput = cardNumber.replaceAll("\\D", "");
27+
28+
if (cleanedCreditCardInput.isBlank() || !Number.isNumber(cleanedCreditCardInput)) {
29+
return false;
30+
}
31+
32+
final int[] creditCardDigits =
33+
cleanedCreditCardInput.chars().map(Character::getNumericValue).toArray();
34+
final int creditCardLength = creditCardDigits.length;
35+
36+
int sum = 0;
37+
boolean isEven = false;
38+
39+
for (int i = creditCardLength - 1; i >= 0; i--) {
40+
int digit = creditCardDigits[i];
41+
42+
if (isEven) {
43+
digit *= 2;
44+
45+
if (digit > 9) {
46+
digit -= 9;
47+
}
48+
}
49+
50+
sum += digit;
51+
isEven = !isEven;
52+
}
53+
54+
return sum % 10 == 0;
55+
}
56+
57+
/**
58+
* Identify the flag of a credit card.
59+
*
60+
* @param cardNumber The credit card number to be identified.
61+
* @return The flag of the credit card.
62+
*/
63+
public static String identifyFlagCard(@NotNull String cardNumber) {
64+
if (cardNumber.isBlank()) {
65+
throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY);
66+
}
67+
68+
Map<String, Pattern> flags = new HashMap<>();
69+
flags.put("Visa", Pattern.compile("^4"));
70+
flags.put("Mastercard", Pattern.compile("^5[1-5]"));
71+
flags.put("American Express", Pattern.compile("^3[47]"));
72+
flags.put("Discover", Pattern.compile("^6(?:011|5)"));
73+
flags.put("JCB", Pattern.compile("^(?:2131|1800|35\\d{3})"));
74+
flags.put("Diners Club", Pattern.compile("^3(?:0[0-5]|[68])"));
75+
flags.put("Maestro", Pattern.compile("^(?:5[0678]\\d\\d|6304|6390|67\\d\\d)"));
76+
flags.put("UnionPay", Pattern.compile("^(62|88)"));
77+
flags.put("Elo", Pattern.compile("^63[789]"));
78+
flags.put("Hipercard", Pattern.compile("^(3841|60)"));
79+
80+
return flags.entrySet().stream()
81+
.filter(flag -> flag.getValue().matcher(cardNumber).find())
82+
.findFirst()
83+
.map(Map.Entry::getKey)
84+
.orElse("Unknown");
85+
}
86+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.multiform_validator;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.time.format.DateTimeParseException;
6+
import java.util.regex.Pattern;
7+
8+
public class Date {
9+
private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty";
10+
11+
private Date() {
12+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
13+
}
14+
15+
/**
16+
* Check if the given string is a valid date
17+
*
18+
* @param date The date string to be validated
19+
* @return true if the given string is a valid date, false otherwise
20+
*/
21+
public static boolean isDate(@NotNull String date) {
22+
if (date.isBlank()) {
23+
throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY);
24+
}
25+
26+
try {
27+
final Pattern dateStringRegex = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");
28+
return dateStringRegex.matcher(date).matches();
29+
} catch (DateTimeParseException e) {
30+
return false;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)