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 Isbn10ChecksumLogic
025        extends BaseChecksumLogic
026        implements ItemAliasChecksumInterface {
027
028    protected Isbn10ChecksumLogic() {
029        super();
030    }
031
032    private int getIsbn10CheckDigit(String alias, int offset) {
033        var result = -1;
034        var digit = alias.charAt(offset);
035
036        if(digit >= '0' && digit <= '9') {
037            result = digit - '0';
038        } else if(digit == 'X') {
039            result = 10;
040        }
041
042        return result;
043    }
044
045    @Override
046    public void checkChecksum(final ExecutionErrorAccumulator eea, final String alias) {
047        if(alias.length() == 10) {
048            var hasCharacterError = false;
049            var runningTotal = 0;
050            var checksum = 0;
051
052            for(var i = 0; i < 9; i++) {
053                var digit = getDigit(alias, i);
054
055                if(digit == -1) {
056                    hasCharacterError = true;
057                    break;
058                } else {
059                    runningTotal += digit;
060                    checksum += runningTotal;
061                }
062            }
063
064            if(!hasCharacterError) {
065                var checkDigit = getIsbn10CheckDigit(alias, 9);
066
067                hasCharacterError = checkDigit == -1;
068
069                if(!hasCharacterError) {
070                    runningTotal += checkDigit;
071                    checksum += runningTotal;
072
073                    if(checksum % 11 != 0) {
074                        eea.addExecutionError(ExecutionErrors.IncorrectIsbn10Checksum.name(), alias);
075                    }
076                }
077            }
078
079            if(hasCharacterError) {
080                eea.addExecutionError(ExecutionErrors.IncorrectIsbn10Character.name(), alias);
081            }
082        } else {
083            eea.addExecutionError(ExecutionErrors.IncorrectIsbn10Length.name(), alias);
084        }
085    }
086
087}