|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.validator.routines.checkdigit; |
| 18 | + |
| 19 | +import org.apache.commons.logging.Log; |
| 20 | +import org.apache.commons.logging.LogFactory; |
| 21 | + |
| 22 | +/** |
| 23 | + * German Steuer-Identifikationsnummer (TIN – in short: IdNr.) is available since 2008, |
| 24 | + * has a length of 11 digits and it applies to individual persons. |
| 25 | + * |
| 26 | + * <p> |
| 27 | + * See <a href="https://download.elster.de/download/schnittstellen/Pruefung_der_Steuer_und_Steueridentifikatsnummer.pdf">Prüfung der Steuer- und Steueridentifikationsnummer (de)</a> |
| 28 | + * for more details. |
| 29 | + * </p> |
| 30 | + * |
| 31 | + * @since 1.10.0 |
| 32 | + */ |
| 33 | +public final class TidDECheckDigit extends Modulus11TenCheckDigit { |
| 34 | + |
| 35 | + private static final long serialVersionUID = 4222306963463322195L; |
| 36 | + private static final Log LOG = LogFactory.getLog(TidDECheckDigit.class); |
| 37 | + |
| 38 | + /** Singleton Check Digit instance */ |
| 39 | + private static final TidDECheckDigit INSTANCE = new TidDECheckDigit(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets the singleton instance of this validator. |
| 43 | + * @return A singleton instance of the class. |
| 44 | + */ |
| 45 | + public static CheckDigit getInstance() { |
| 46 | + return INSTANCE; |
| 47 | + } |
| 48 | + |
| 49 | + private TidDECheckDigit() { |
| 50 | + super(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritDoc} <p> |
| 55 | + * Override to count ciphers to and check the criteria: |
| 56 | + * </p> |
| 57 | + * <ul> |
| 58 | + * <li>The first 10 digits of the identification number must contain |
| 59 | + * exactly one duplicate or triplicate digit. |
| 60 | + * </li> |
| 61 | + * <li>Are there three identical digits in positions 1 to 10, |
| 62 | + * these identical digits must never be directly next to each other. |
| 63 | + * </li> |
| 64 | + * </ul> |
| 65 | + */ |
| 66 | + @Override |
| 67 | + protected int calculateModulus(final String code, final boolean includesCheckDigit) throws CheckDigitException { |
| 68 | + int product = MODULUS_10; |
| 69 | + int sum = 0; |
| 70 | + final int[] anzahl = new int[10]; // CHECKSTYLE IGNORE MagicNumber |
| 71 | + final int[] fstpos = new int[10]; // CHECKSTYLE IGNORE MagicNumber |
| 72 | + for (int i = 0; i < code.length() - (includesCheckDigit ? 1 : 0); i++) { |
| 73 | + final int leftPos = i + 1; |
| 74 | + final int rightPos = -1; // rightPos param not used |
| 75 | + final int cipher = toInt(code.charAt(i), leftPos, rightPos); |
| 76 | + anzahl[cipher]++; // Ziffern zählen |
| 77 | + if (fstpos[cipher] == 0) { |
| 78 | + fstpos[cipher] = i; |
| 79 | + } |
| 80 | + sum = cipher + product; |
| 81 | + sum = sum % MODULUS_10; |
| 82 | + product = 2 * (sum == 0 ? MODULUS_10 : sum) % MODULUS_11; |
| 83 | + } |
| 84 | + LOG.debug(code + ": Zifferanzahl auswerten"); |
| 85 | + int doppelt = 0; |
| 86 | + int dreifach = 0; |
| 87 | + int dreifachz = -1; |
| 88 | + for (int i = 0; i < anzahl.length; i++) { |
| 89 | + if (anzahl[i] == 2) { // CHECKSTYLE IGNORE MagicNumber |
| 90 | + doppelt++; |
| 91 | + } |
| 92 | + if (anzahl[i] == 3) { // CHECKSTYLE IGNORE MagicNumber |
| 93 | + dreifach++; |
| 94 | + dreifachz = i; |
| 95 | + } |
| 96 | + } |
| 97 | + if (doppelt == 0 && dreifach == 0) { |
| 98 | + LOG.warn(code + ": keine doppelt und keine dreifach"); |
| 99 | + throw new CheckDigitException("Invalid code, keine doppelt und keine dreifach"); |
| 100 | + } else if (doppelt > 1) { |
| 101 | + LOG.warn(code + ": mehrere doppelt"); |
| 102 | + throw new CheckDigitException("Invalid code, mehrere doppelt"); |
| 103 | + } else if (dreifach > 1) { |
| 104 | + LOG.warn(code + ": mehrere dreifach"); |
| 105 | + throw new CheckDigitException("Invalid code, mehrere dreifach"); |
| 106 | + } else if (dreifach == 1) { |
| 107 | + final int i = code.indexOf("" + dreifachz); |
| 108 | + if (dreifachz == toInt(code.charAt(i + 1), i + 1, -1) |
| 109 | + && dreifachz == toInt(code.charAt(i + 2), i + 2, -1)) { |
| 110 | + LOG.warn(code + ": dreifach direkt hintereinander Ziffer:" + dreifachz); |
| 111 | + throw new CheckDigitException("Invalid code, dreifach direkt hintereinander"); |
| 112 | + } |
| 113 | + } |
| 114 | + final int pruefZiffer = MODULUS_11 - product; |
| 115 | + return pruefZiffer == MODULUS_10 ? 0 : pruefZiffer; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments