001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.model.control.item.server.logic.checksum; 018 019import com.echothree.util.common.message.ExecutionErrors; 020import com.echothree.util.server.message.ExecutionErrorAccumulator; 021import javax.enterprise.context.ApplicationScoped; 022 023@ApplicationScoped 024public class Isbn13ChecksumLogic 025 extends BaseChecksumLogic 026 implements ItemAliasChecksumInterface { 027 028 protected Isbn13ChecksumLogic() { 029 super(); 030 } 031 032 @Override 033 public void checkChecksum(final ExecutionErrorAccumulator eea, final String alias) { 034 if(alias.length() == 13) { 035 var hasCharacterError = false; 036 var checksum = 0; 037 038 for(int i = 0; i < 12; i++) { 039 int d = getDigit(alias, i); 040 041 if(d == -1) { 042 hasCharacterError = true; 043 break; 044 } 045 046 // Positions are 1-based in the spec: odd positions weight 1, even positions weight 3. 047 // i is 0-based, so i % 2 == 0 means even position. 048 checksum += i % 2 == 0 ? d : (3 * d); 049 } 050 051 if(!hasCharacterError) { 052 var checkDigit = getDigit(alias, 12); 053 054 hasCharacterError = checkDigit == -1; 055 056 if(!hasCharacterError) { 057 checksum += checkDigit; 058 059 if(!(checksum % 10 == 0)) { 060 eea.addExecutionError(ExecutionErrors.IncorrectIsbn13Checksum.name(), alias); 061 } 062 } 063 } 064 065 if(hasCharacterError) { 066 eea.addExecutionError(ExecutionErrors.IncorrectIsbn13Character.name(), alias); 067 } 068 } else { 069 eea.addExecutionError(ExecutionErrors.IncorrectIsbn13Length.name(), alias); 070 } 071 } 072 073 074}