com.strider.datadefender.extensions.BiographicFunctions   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 31
c 4
b 0
f 0
dl 0
loc 55
ccs 0
cts 27
cp 0
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B isValidSIN(String) 0 47 7
1
/*
2
 * Copyright 2014-2016, Armenak Grigoryan, Matthew Eaton, and individual contributors as indicated
3
 * by the @authors tag. See the copyright.txt in the distribution for a
4
 * full listing of individual contributors.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 * Lesser General Public License for more details.
15
 */
16
package com.strider.datadefender.extensions;
17
18
import com.strider.datadefender.anonymizer.functions.Core;
19
20
import java.util.ArrayList;
21
import java.util.List;
22
23
import lombok.extern.log4j.Log4j2;
24
import org.apache.commons.lang3.StringUtils;
25
26
/**
27
 * @author Matthew Eaton
28
 */
29
@Log4j2
30
public class BiographicFunctions extends Core {
31
32
    /**
33
     * Algorithm is taken from https://en.wikipedia.org/wiki/Social_Insurance_Number
34
     * @param sin
35
     * @return boolean true, if SIN is valid, otherwise false
36
     */
37
    public static boolean isValidSIN(final String sin) {
38
        boolean valid = false;
39
40
        if (sin.length() < 9) {
41
            log.debug("SIN length is < 9");
42
43
            return valid;
44
        }
45
46
        if (!StringUtils.isNumeric(sin)) {
47
            log.debug("SIN " + sin + " is not number");
48
49
            return valid;
50
        }
51
52
        final int[]         sinArray   = new int[sin.length()];
53
        final int[]         checkArray = {
54
            1, 2, 1, 2, 1, 2, 1, 2, 1
55
        };
56
        final List<Integer> sinList    = new ArrayList();
57
58
        for (int i = 0; i < 9; i++) {
59
            sinArray[i] = Integer.valueOf(sin.substring(i, i + 1));
60
            sinArray[i] = sinArray[i] * checkArray[i];
61
        }
62
63
        int sum = 0;
64
65
        for (int i = 0; i < 9; i++) {
66
            final String tmp = String.valueOf(sinArray[i]);
67
68
            if (tmp.length() == 1) {
69
                sinList.add(Integer.valueOf(tmp));
70
                sum += Integer.valueOf(tmp);
71
            } else {
72
                sinList.add(Integer.valueOf(tmp.substring(0, 1)));
73
                sum += Integer.valueOf(tmp.substring(0, 1));
74
                sinList.add(Integer.valueOf(tmp.substring(1, 2)));
75
                sum += Integer.valueOf(tmp.substring(1, 2));
76
            }
77
        }
78
79
        if ((sum % 10) == 0) {
80
            valid = true;
81
        }
82
83
        return valid;
84
    }
85
}
86