Skip to content

Commit d7747b2

Browse files
committed
Merge branch 'release/v0.0.3'
2 parents 91a3e8c + ac6381e commit d7747b2

File tree

6 files changed

+290
-13
lines changed

6 files changed

+290
-13
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Build Status](https://travis-ci.org/DonMat/PolishIdentifiersUtils-kotlin.svg?branch=master)](https://travis-ci.org/DonMat/PolishIdentifiersUtils-kotlin)
44

55

6-
This is simple lightweight library written in Kotlin to manage and validates polish identification numbers like: NIP, PESEL, REGON (9-digit and 14-digit).
6+
This is simple lightweight library written in Kotlin to validate polish identification numbers like: NIP, PESEL, REGON (9-digit and 14-digit).
77

88
## Purpose of project
99
It is easier to maintain validators and other utilities functions as one additional library instead of coppy&pase the same code between projects.
@@ -26,7 +26,7 @@ allprojects {
2626
Step 2. Add the dependency
2727
```gradle
2828
dependencies {
29-
compile 'com.github.DonMat:PolishIdentifiersUtils-kotlin:v0.0.2'
29+
compile 'com.github.DonMat:PolishIdentifiersUtils-kotlin:v0.0.3'
3030
}
3131
```
3232

@@ -36,11 +36,11 @@ dependencies {
3636
* getBirthDate()
3737
* getGender()
3838

39-
- NipUtil
40-
* IN PROGRESS
39+
- NipValidator
40+
* isValid()
4141

42-
- RegonUtil
43-
* IN PROGRESS
42+
- RegonValidator
43+
* isValid()
4444

4545
License
4646
-------
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2017 Mateusz Utkała
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.utkala.polishidentifiersutils
18+
19+
class NipValidator(private val nip: String) {
20+
21+
private val WEIGHTS = intArrayOf(6, 5, 7, 2, 3, 4, 5, 6, 7)
22+
var error: ValidatorError? = null
23+
private var valid = false
24+
25+
fun isValid(): Boolean {
26+
return when {
27+
valid -> true
28+
checkInput() -> validate()
29+
else -> false
30+
}
31+
}
32+
33+
private fun checkInput(): Boolean {
34+
val nipNumbers = trimNotDigits(nip)
35+
36+
return if (nipNumbers.length != 10) {
37+
error = ValidatorError.WRONG_LENGTH
38+
false
39+
} else if (!nipNumbers.matches(Regex("^\\d{10}$"))) {
40+
error = ValidatorError.INVALID_INPUT
41+
false
42+
} else
43+
true
44+
}
45+
46+
private fun validate(): Boolean {
47+
val nipNumbers = trimNotDigits(nip)
48+
val sum = (0 until nipNumbers.length - 1).sumBy { (nipNumbers[it].toString().toInt() * WEIGHTS[it]) }
49+
50+
return if (sum % 11 == nipNumbers.last().toString().toInt()) {
51+
valid = true
52+
true
53+
} else {
54+
error = ValidatorError.INVALID
55+
false
56+
}
57+
}
58+
59+
private fun trimNotDigits(input: String): String {
60+
return if (input.isBlank()) "" else input.replace(Regex("[PL\\s-]"), "")
61+
}
62+
}

polishIdentifiersUtils/src/main/java/pl/utkala/polishidentifiersutils/PeselUtil.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ package pl.utkala.polishidentifiersutils
1919
class PeselUtil(private val pesel: String) {
2020
enum class Gender { MALE, FEMALE }
2121

22-
private val WEIGHTS = listOf(1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1)
22+
private val WEIGHTS = intArrayOf(1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1)
2323
var error: ValidatorError? = null
2424
private var valid = false
2525

2626
fun isValid(): Boolean {
27-
return if (valid || checkInput())
28-
validate()
29-
else
30-
false
27+
return when {
28+
valid -> true
29+
checkInput() -> validate()
30+
else -> false
31+
}
3132
}
3233

3334
fun getGender(): Gender? {
@@ -84,8 +85,7 @@ class PeselUtil(private val pesel: String) {
8485
}
8586

8687
private fun validate(): Boolean {
87-
var sum = 0
88-
pesel.forEachIndexed { index, value -> sum += (value.toInt() * WEIGHTS[index]) }
88+
val sum = (0 until pesel.length).sumBy { pesel[it].toInt() * WEIGHTS[it] }
8989
return if (sum % 10 == 0) {
9090
valid = true
9191
true
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2017 Mateusz Utkała
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.utkala.polishidentifiersutils
18+
19+
class RegonValidator(private val regon: String) {
20+
21+
var error: ValidatorError? = null
22+
private var valid = false
23+
24+
fun isValid(): Boolean {
25+
return when {
26+
valid -> true
27+
checkInput() -> validate()
28+
else -> false
29+
}
30+
}
31+
32+
private fun checkInput(): Boolean {
33+
val regonNumbers = trimNotDigits(regon)
34+
35+
return if (regonNumbers.length != 9 && regonNumbers.length != 14) {
36+
error = ValidatorError.WRONG_LENGTH
37+
false
38+
} else if (!regonNumbers.matches(Regex("^\\d{9}$")) && !regonNumbers.matches(Regex("^\\d{14}$"))) {
39+
error = ValidatorError.INVALID_INPUT
40+
false
41+
} else
42+
true
43+
}
44+
45+
private fun validate(): Boolean {
46+
val regonNumbers = trimNotDigits(regon)
47+
return if (regonNumbers.length == 9) validateRegon9(regonNumbers) else validateRegon14(regonNumbers)
48+
}
49+
50+
private fun validateRegon9(regonNumbers: String): Boolean {
51+
val weights = intArrayOf(8, 9, 2, 3, 4, 5, 6, 7)
52+
val sum = (0 until regonNumbers.length - 1).sumBy { (regonNumbers[it].toString().toInt() * weights[it]) }
53+
return if (sum % 11 == regonNumbers.last().toString().toInt()) {
54+
valid = true
55+
true
56+
} else {
57+
error = ValidatorError.INVALID
58+
false
59+
}
60+
}
61+
62+
private fun validateRegon14(regonNumbers: String): Boolean {
63+
val weights = intArrayOf(2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8)
64+
val sum = (0 until regonNumbers.length - 1).sumBy { (regonNumbers[it].toString().toInt() * weights[it]) }
65+
return if (sum % 11 == regonNumbers.last().toString().toInt()) {
66+
valid = true
67+
true
68+
} else {
69+
error = ValidatorError.INVALID
70+
false
71+
}
72+
}
73+
74+
private fun trimNotDigits(input: String): String {
75+
return if (input.isBlank()) "" else input.replace(Regex("[\\s-]"), "")
76+
}
77+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2017 Mateusz Utkała
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.utkala.polishidentifiersutils
18+
19+
import org.junit.Assert.assertEquals
20+
import org.junit.Test
21+
22+
23+
class NipValidatorTest {
24+
@Test
25+
fun validNipTest() {
26+
var nip = NipValidator("3790494818")
27+
assertEquals(nip.isValid(), true)
28+
assertEquals(nip.error, null)
29+
30+
nip = NipValidator("PL 106-00-00-062")
31+
assertEquals(nip.isValid(), true)
32+
assertEquals(nip.error, null)
33+
}
34+
35+
@Test
36+
fun invalidNipTest() {
37+
val nip = NipValidator("3790494811")
38+
assertEquals(nip.isValid(), false)
39+
assertEquals(nip.error, ValidatorError.INVALID)
40+
}
41+
42+
@Test
43+
fun emptyNipTest() {
44+
val nip = NipValidator("")
45+
assertEquals(nip.isValid(), false)
46+
assertEquals(nip.error, ValidatorError.WRONG_LENGTH)
47+
}
48+
49+
@Test
50+
fun shortNipTest() {
51+
val nip = NipValidator("1234")
52+
assertEquals(nip.isValid(), false)
53+
assertEquals(nip.error, ValidatorError.WRONG_LENGTH)
54+
}
55+
56+
@Test
57+
fun longNipTest() {
58+
val nip = NipValidator("123412341234")
59+
assertEquals(nip.isValid(), false)
60+
assertEquals(nip.error, ValidatorError.WRONG_LENGTH)
61+
}
62+
63+
@Test
64+
fun invalidInputTest() {
65+
val nip = NipValidator("EN 106-00-00-062")
66+
assertEquals(nip.isValid(), false)
67+
assertEquals(nip.error, ValidatorError.WRONG_LENGTH)
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2017 Mateusz Utkała
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.utkala.polishidentifiersutils
18+
19+
import org.junit.Assert
20+
import org.junit.Test
21+
22+
class RegonValidatorTest {
23+
24+
@Test
25+
fun validRegonTest() {
26+
var regon = RegonValidator("739941302")
27+
Assert.assertEquals(regon.isValid(), true)
28+
Assert.assertEquals(regon.error, null)
29+
30+
regon = RegonValidator("457280735")
31+
Assert.assertEquals(regon.isValid(), true)
32+
Assert.assertEquals(regon.error, null)
33+
}
34+
35+
@Test
36+
fun invalidRegonTest() {
37+
val regon = RegonValidator("457280733")
38+
Assert.assertEquals(regon.isValid(), false)
39+
Assert.assertEquals(regon.error, ValidatorError.INVALID)
40+
}
41+
42+
@Test
43+
fun emptyRegonTest() {
44+
val regon = RegonValidator("")
45+
Assert.assertEquals(regon.isValid(), false)
46+
Assert.assertEquals(regon.error, ValidatorError.WRONG_LENGTH)
47+
}
48+
49+
@Test
50+
fun shortRegonTest() {
51+
val regon = RegonValidator("1234")
52+
Assert.assertEquals(regon.isValid(), false)
53+
Assert.assertEquals(regon.error, ValidatorError.WRONG_LENGTH)
54+
}
55+
56+
@Test
57+
fun longRegonTest() {
58+
val regon = RegonValidator("123412341234")
59+
Assert.assertEquals(regon.isValid(), false)
60+
Assert.assertEquals(regon.error, ValidatorError.WRONG_LENGTH)
61+
}
62+
63+
@Test
64+
fun invalidInputTest() {
65+
val regon = RegonValidator("PL457280735")
66+
Assert.assertEquals(regon.isValid(), false)
67+
Assert.assertEquals(regon.error, ValidatorError.WRONG_LENGTH)
68+
}
69+
}

0 commit comments

Comments
 (0)