isbnvalidator.java
来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 138 行
JAVA
138 行
package de.fhm.jkf.bookshop.clsv;
import java.util.StringTokenizer;
/*
* <br><br><center><table border="1" width="80%"><hr>
* <strong>
* <a href="http://jkf.sourceforge.net">The JKF Project</a>
* </strong>
* <p>
* Copyright (C) 2003 by Theodor Willax
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
* GNU Lesser General Public License</a> along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
* <hr></table></center>
*/
/**
* Checks if a given ISBN is correct.
* <br>
* What is the format of a ISBN?<br>
* Every ISBN consists of ten digits and whenever
* it is printed it is preceded by the letters ISBN.
* The ten-digit number is divided into four parts
* of variable length, each part separated by a hyphen.
* See <a href="http://www.isbn.org/standards/home/isbn/us/isbnqa.asp#Q3">
* www.isbn.org</a> for detailed information about a isbn.
* <br>
* The preceding letters "ISBN" will be left an it would be an error
* if the are contained in the string which should be checked.
*
* @version $$Revision: 1.1 $$
* @author Theodor Willax
*/
public class IsbnValidator {
/**
* Validates if the given <code>String</code> is a valid ISBN.
*
* @return IsbnValidatorResult type
* @see de.fhm.jkf.bookshop.clsv.IsbnValidatorResult
*/
public IsbnValidatorResult isIsbn(String isbn) {
if (isbn == null || !this.isLengthOk(isbn)) {
return IsbnValidatorResult.VALIDATION_ERROR_LENGTH;
}
if (!this.isHyphenCountOk(isbn)) {
return IsbnValidatorResult.VALIDATION_ERROR_HYPHEN_COUNT;
}
if (!this.isPartCompositionOk(isbn)) {
return IsbnValidatorResult.VALIDATION_ERROR_PART_COMPOSITION;
}
return IsbnValidatorResult.VALIDATION_SUCCESS;
}
/**
* Validates if the length of the given ISBN is ok.
* It must have 10 digits plus 3 hyphens (= 13).
*
* @return true if length is ok, false otherwise.
*/
private boolean isLengthOk(String isbn) {
return isbn.length() == 13;
}
/**
* ISBN's must have exactly 3 hyphens. Hyphens can not be
* side by side, there is always at least one digit between.
*
* returns true if number and position of hyphens is ok, false otherwise.
*/
private boolean isHyphenCountOk(String isbn) {
// do we have 3 hyphens
int idx = 0, idxOld = 0, hyphenCount = 0;
while ((idxOld = isbn.indexOf('-', idx)) != -1) {
// 2 hyphens side by side are not allowed
if (idxOld + 1 == idx) {
hyphenCount = 0;
break;
}
idx = idxOld + 1;
hyphenCount++;
}
return hyphenCount == 3;
}
/**
* A ISBN consists of four valid parts, each separated
* by a hyphen.
*
* @return true if all parts are ok, false otherwise.
*/
private boolean isPartCompositionOk(String isbn) {
boolean parts = false;
int i = 0;
StringTokenizer tokenizer = new StringTokenizer(isbn, "-");
while (tokenizer.hasMoreElements()) {
i++;
String token = tokenizer.nextToken();
if (i != 4) {
try {
Integer.parseInt(token);
} catch (NumberFormatException e) {
return false;
}
} else {
if (token.length() != 1) {
return false;
}
// the last token can be one digit or 'X'
if (token.equals("X")) {
return true;
}
try {
Integer.parseInt(token);
} catch (NumberFormatException e) {
return false;
}
return true;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?