Refactoring operation name : extract class

Refactoring object : MakeBytesToSegment

Reason : makeNumeric, changeNumericToSegment, containNonNumericCharaters
has independent role.
So, make these function to class(MakeNumericToSegment).
pull/90/head^2
jaemin7666 5 years ago
parent 33c8042f00
commit 937a2dfc53

@ -0,0 +1,33 @@
import java.util.Objects;
public class MakeNumericToSegment implements MakeSegment {
/**
* Returns a segment representing the specified string of decimal digits encoded in numeric mode.
* @param digits the text (not {@code null}), with only digits from 0 to 9 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-digit characters
*/
public QrSegment excute(String digits) {
Objects.requireNonNull(digits);
if (containNonNumericCharaters(digits))
throw new IllegalArgumentException("String contains non-numeric characters");
BitBuffer bitBuffer = new BitBuffer();
changeNumericToSegment(digits, bitBuffer);
return new QrSegment(QrSegment.Mode.NUMERIC, digits.length(), bitBuffer);
}
public static void changeNumericToSegment(String digits, BitBuffer bitBuffer) {
for (int i = 0; i < digits.length(); ) { // Consume up to 3 digits per iteration
int n = Math.min(digits.length() - i, 3);
bitBuffer.appendBits(Integer.parseInt(digits.substring(i, i + n)), n * 3 + 1);
i += n;
}
}
public static boolean containNonNumericCharaters(String digits) {
return !QrSegment.NUMERIC_REGEX.matcher(digits).matches();
}
}

@ -49,36 +49,6 @@ public final class QrSegment {
/*---- Static factory functions (mid level) ----*/ /*---- Static factory functions (mid level) ----*/
/**
* Returns a segment representing the specified string of decimal digits encoded in numeric mode.
* @param digits the text (not {@code null}), with only digits from 0 to 9 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-digit characters
*/
public QrSegment makeNumeric(String digits) {
Objects.requireNonNull(digits);
if (containNonNumericCharaters(digits))
throw new IllegalArgumentException("String contains non-numeric characters");
BitBuffer bitBuffer = new BitBuffer();
changeNumericToSegment(digits, bitBuffer);
return new QrSegment(QrSegment.Mode.NUMERIC, digits.length(), bitBuffer);
}
public static void changeNumericToSegment(String digits, BitBuffer bitBuffer) {
for (int i = 0; i < digits.length(); ) { // Consume up to 3 digits per iteration
int n = Math.min(digits.length() - i, 3);
bitBuffer.appendBits(Integer.parseInt(digits.substring(i, i + n)), n * 3 + 1);
i += n;
}
}
public static boolean containNonNumericCharaters(String digits) {
return !QrSegment.NUMERIC_REGEX.matcher(digits).matches();
}
/**
/** /**
* Returns a segment representing the specified text string encoded in alphanumeric mode. * 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, * The characters allowed are: 0 to 9, A to Z (uppercase only), space,

Loading…
Cancel
Save