From 27dc7057c9a154bb46efbfa771e55452477b9597 Mon Sep 17 00:00:00 2001 From: jaemin7666 Date: Sat, 6 Jun 2020 20:36:50 +0900 Subject: [PATCH] Refactoring operation name : extract class Refactoring object : MakeAlphaNumericToSegment Reason : makeAlphanumeric, changeAlphaNumericStringToSegment has independent role. So, make these function to class(MakeAlphaNumericToSegment). --- .../qrcodegen/MakeAlphaNumericToSegment.java | 34 +++++++++++++++++++ .../java/io/nayuki/qrcodegen/QrSegment.java | 29 ---------------- 2 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 java/src/main/java/io/nayuki/qrcodegen/MakeAlphaNumericToSegment.java diff --git a/java/src/main/java/io/nayuki/qrcodegen/MakeAlphaNumericToSegment.java b/java/src/main/java/io/nayuki/qrcodegen/MakeAlphaNumericToSegment.java new file mode 100644 index 0000000..66bd854 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/MakeAlphaNumericToSegment.java @@ -0,0 +1,34 @@ +import java.util.Objects; + +public class MakeAlphaNumericToSegment implements MakeSegment { + + /** + * Returns a segment representing the specified text string encoded in alphanumeric mode. + * The characters allowed are: 0 to 9, A to Z (uppercase only), space, + * dollar, percent, asterisk, plus, hyphen, period, slash, colon. + * @param text the text (not {@code null}), with only certain characters allowed + * @return a segment (not {@code null}) containing the text + * @throws NullPointerException if the string is {@code null} + * @throws IllegalArgumentException if the string contains non-encodable characters + */ + public QrSegment excute(String text) { + Objects.requireNonNull(text); + if (!QrSegment.ALPHANUMERIC_REGEX.matcher(text).matches()) + throw new IllegalArgumentException("String contains unencodable characters in alphanumeric mode"); + + BitBuffer bitBuffer = new BitBuffer(); + changeAlphaNumericStringToSegment(text, bitBuffer); + return new QrSegment(QrSegment.Mode.ALPHANUMERIC, text.length(), bitBuffer); + } + + public static void changeAlphaNumericStringToSegment(String text, BitBuffer bitBuffer) { + int i; + for (i = 0; i <= text.length() - 2; i += 2) { // Process groups of 2 + int temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45; + temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1)); + bitBuffer.appendBits(temp, 11); + } + if (i < text.length()) // 1 character remaining + bitBuffer.appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/QrSegment.java b/java/src/main/java/io/nayuki/qrcodegen/QrSegment.java index 4b12881..3d261f7 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/QrSegment.java +++ b/java/src/main/java/io/nayuki/qrcodegen/QrSegment.java @@ -49,35 +49,6 @@ public final class QrSegment { /*---- Static factory functions (mid level) ----*/ - /** - * Returns a segment representing the specified text string encoded in alphanumeric mode. - * The characters allowed are: 0 to 9, A to Z (uppercase only), space, - * dollar, percent, asterisk, plus, hyphen, period, slash, colon. - * @param text the text (not {@code null}), with only certain characters allowed - * @return a segment (not {@code null}) containing the text - * @throws NullPointerException if the string is {@code null} - * @throws IllegalArgumentException if the string contains non-encodable characters - */ - public QrSegment makeAlphanumeric(String text) { - Objects.requireNonNull(text); - if (!QrSegment.ALPHANUMERIC_REGEX.matcher(text).matches()) - throw new IllegalArgumentException("String contains unencodable characters in alphanumeric mode"); - - BitBuffer bitBuffer = new BitBuffer(); - changeAlphaNumericStringToSegment(text, bitBuffer); - return new QrSegment(QrSegment.Mode.ALPHANUMERIC, text.length(), bitBuffer); - } - - public static void changeAlphaNumericStringToSegment(String text, BitBuffer bitBuffer) { - int i; - for (i = 0; i <= text.length() - 2; i += 2) { // Process groups of 2 - int temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45; - temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1)); - bitBuffer.appendBits(temp, 11); - } - if (i < text.length()) // 1 character remaining - bitBuffer.appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6); - } /** * Returns a list of zero or more segments to represent the specified Unicode text string. * The result may use various segment modes and switch modes to optimize the length of the bit stream.