Tweaked all Java code to replace explicit null checks with calls to Objects.requireNonNull() (requires Java SE 7+).

pull/4/head
Project Nayuki 8 years ago
parent b2e7844a94
commit bd470926ca

@ -25,6 +25,7 @@
package io.nayuki.qrcodegen; package io.nayuki.qrcodegen;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
/** /**
@ -76,8 +77,7 @@ final class BitBuffer {
// Appends the data of the given segment to this bit buffer. // Appends the data of the given segment to this bit buffer.
public void appendData(QrSegment seg) { public void appendData(QrSegment seg) {
if (seg == null) Objects.requireNonNull(seg);
throw new NullPointerException();
ensureCapacity(bitLength + seg.bitLength); ensureCapacity(bitLength + seg.bitLength);
for (int i = 0; i < seg.bitLength; i++, bitLength++) { // Append bit by bit for (int i = 0; i < seg.bitLength; i++, bitLength++) { // Append bit by bit
int bit = (seg.getByte(i >>> 3) >>> (7 - (i & 7))) & 1; int bit = (seg.getByte(i >>> 3) >>> (7 - (i & 7))) & 1;

@ -27,6 +27,7 @@ package io.nayuki.qrcodegen;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
@ -51,8 +52,8 @@ public final class QrCode {
* @throws IllegalArgumentException if the text fails to fit in the largest version QR Code, which means it is too long * @throws IllegalArgumentException if the text fails to fit in the largest version QR Code, which means it is too long
*/ */
public static QrCode encodeText(String text, Ecc ecl) { public static QrCode encodeText(String text, Ecc ecl) {
if (text == null || ecl == null) Objects.requireNonNull(text);
throw new NullPointerException(); Objects.requireNonNull(ecl);
List<QrSegment> segs = QrSegment.makeSegments(text); List<QrSegment> segs = QrSegment.makeSegments(text);
return encodeSegments(segs, ecl); return encodeSegments(segs, ecl);
} }
@ -70,8 +71,8 @@ public final class QrCode {
* @throws IllegalArgumentException if the data fails to fit in the largest version QR Code, which means it is too long * @throws IllegalArgumentException if the data fails to fit in the largest version QR Code, which means it is too long
*/ */
public static QrCode encodeBinary(byte[] data, Ecc ecl) { public static QrCode encodeBinary(byte[] data, Ecc ecl) {
if (data == null || ecl == null) Objects.requireNonNull(data);
throw new NullPointerException(); Objects.requireNonNull(ecl);
QrSegment seg = QrSegment.makeBytes(data); QrSegment seg = QrSegment.makeBytes(data);
return encodeSegments(Arrays.asList(seg), ecl); return encodeSegments(Arrays.asList(seg), ecl);
} }
@ -112,8 +113,8 @@ public final class QrCode {
* &lt; &minus;1 or mask > 7, or if the data is too long to fit in a QR Code at maxVersion at the ECL * &lt; &minus;1 or mask > 7, or if the data is too long to fit in a QR Code at maxVersion at the ECL
*/ */
public static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl, int minVersion, int maxVersion, int mask, boolean boostEcl) { public static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl, int minVersion, int maxVersion, int mask, boolean boostEcl) {
if (segs == null || ecl == null) Objects.requireNonNull(segs);
throw new NullPointerException(); Objects.requireNonNull(ecl);
if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7) if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7)
throw new IllegalArgumentException("Invalid value"); throw new IllegalArgumentException("Invalid value");
@ -201,12 +202,10 @@ public final class QrCode {
*/ */
public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int mask) { public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int mask) {
// Check arguments // Check arguments
if (ecl == null) Objects.requireNonNull(ecl);
throw new NullPointerException();
if (ver < 1 || ver > 40 || mask < -1 || mask > 7) if (ver < 1 || ver > 40 || mask < -1 || mask > 7)
throw new IllegalArgumentException("Value out of range"); throw new IllegalArgumentException("Value out of range");
if (dataCodewords == null) Objects.requireNonNull(dataCodewords);
throw new NullPointerException();
// Initialize fields // Initialize fields
version = ver; version = ver;
@ -234,8 +233,7 @@ public final class QrCode {
*/ */
public QrCode(QrCode qr, int mask) { public QrCode(QrCode qr, int mask) {
// Check arguments // Check arguments
if (qr == null) Objects.requireNonNull(qr);
throw new NullPointerException();
if (mask < -1 || mask > 7) if (mask < -1 || mask > 7)
throw new IllegalArgumentException("Mask value out of range"); throw new IllegalArgumentException("Mask value out of range");
@ -497,8 +495,7 @@ public final class QrCode {
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
// data area of this QR Code symbol. Function modules need to be marked off before this is called. // data area of this QR Code symbol. Function modules need to be marked off before this is called.
private void drawCodewords(byte[] data) { private void drawCodewords(byte[] data) {
if (data == null) Objects.requireNonNull(data);
throw new NullPointerException();
if (data.length != getNumRawDataModules(version) / 8) if (data.length != getNumRawDataModules(version) / 8)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -832,8 +829,7 @@ public final class QrCode {
* @throws NullPointerException if the data is {@code null} * @throws NullPointerException if the data is {@code null}
*/ */
public byte[] getRemainder(byte[] data) { public byte[] getRemainder(byte[] data) {
if (data == null) Objects.requireNonNull(data);
throw new NullPointerException();
// Compute the remainder by performing polynomial division // Compute the remainder by performing polynomial division
byte[] result = new byte[coefficients.length]; byte[] result = new byte[coefficients.length];

@ -28,6 +28,7 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -50,8 +51,7 @@ public final class QrSegment {
* @throws NullPointerException if the array is {@code null} * @throws NullPointerException if the array is {@code null}
*/ */
public static QrSegment makeBytes(byte[] data) { public static QrSegment makeBytes(byte[] data) {
if (data == null) Objects.requireNonNull(data);
throw new NullPointerException();
return new QrSegment(Mode.BYTE, data.length, data, data.length * 8); return new QrSegment(Mode.BYTE, data.length, data, data.length * 8);
} }
@ -64,8 +64,7 @@ public final class QrSegment {
* @throws IllegalArgumentException if the string contains non-digit characters * @throws IllegalArgumentException if the string contains non-digit characters
*/ */
public static QrSegment makeNumeric(String digits) { public static QrSegment makeNumeric(String digits) {
if (digits == null) Objects.requireNonNull(digits);
throw new NullPointerException();
if (!NUMERIC_REGEX.matcher(digits).matches()) if (!NUMERIC_REGEX.matcher(digits).matches())
throw new IllegalArgumentException("String contains non-numeric characters"); throw new IllegalArgumentException("String contains non-numeric characters");
@ -89,8 +88,7 @@ public final class QrSegment {
* @throws IllegalArgumentException if the string contains non-encodable characters * @throws IllegalArgumentException if the string contains non-encodable characters
*/ */
public static QrSegment makeAlphanumeric(String text) { public static QrSegment makeAlphanumeric(String text) {
if (text == null) Objects.requireNonNull(text);
throw new NullPointerException();
if (!ALPHANUMERIC_REGEX.matcher(text).matches()) if (!ALPHANUMERIC_REGEX.matcher(text).matches())
throw new IllegalArgumentException("String contains unencodable characters in alphanumeric mode"); throw new IllegalArgumentException("String contains unencodable characters in alphanumeric mode");
@ -115,8 +113,7 @@ public final class QrSegment {
* @throws NullPointerException if the text is {@code null} * @throws NullPointerException if the text is {@code null}
*/ */
public static List<QrSegment> makeSegments(String text) { public static List<QrSegment> makeSegments(String text) {
if (text == null) Objects.requireNonNull(text);
throw new NullPointerException();
// Select the most efficient segment encoding automatically // Select the most efficient segment encoding automatically
List<QrSegment> result = new ArrayList<>(); List<QrSegment> result = new ArrayList<>();
@ -160,8 +157,8 @@ public final class QrSegment {
* @throws IllegalArgumentException if the character count or bit length are negative or invalid * @throws IllegalArgumentException if the character count or bit length are negative or invalid
*/ */
public QrSegment(Mode md, int numCh, byte[] b, int bitLen) { public QrSegment(Mode md, int numCh, byte[] b, int bitLen) {
if (md == null || b == null) Objects.requireNonNull(md);
throw new NullPointerException(); Objects.requireNonNull(b);
if (numCh < 0 || bitLen < 0 || bitLen > b.length * 8L) if (numCh < 0 || bitLen < 0 || bitLen > b.length * 8L)
throw new IllegalArgumentException("Invalid value"); throw new IllegalArgumentException("Invalid value");
mode = md; mode = md;
@ -188,15 +185,13 @@ public final class QrSegment {
// Package-private helper function. // Package-private helper function.
static int getTotalBits(List<QrSegment> segs, int version) { static int getTotalBits(List<QrSegment> segs, int version) {
if (segs == null) Objects.requireNonNull(segs);
throw new NullPointerException();
if (version < 1 || version > 40) if (version < 1 || version > 40)
throw new IllegalArgumentException("Version number out of range"); throw new IllegalArgumentException("Version number out of range");
int result = 0; int result = 0;
for (QrSegment seg : segs) { for (QrSegment seg : segs) {
if (seg == null) Objects.requireNonNull(seg);
throw new NullPointerException();
int ccbits = seg.mode.numCharCountBits(version); int ccbits = seg.mode.numCharCountBits(version);
// Fail if segment length value doesn't fit in the length field's bit-width // Fail if segment length value doesn't fit in the length field's bit-width
if (seg.numChars >= (1 << ccbits)) if (seg.numChars >= (1 << ccbits))

@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.Objects;
public final class QrSegmentAdvanced { public final class QrSegmentAdvanced {
@ -57,8 +58,8 @@ public final class QrSegmentAdvanced {
*/ */
public static List<QrSegment> makeSegmentsOptimally(String text, QrCode.Ecc ecl, int minVersion, int maxVersion) { public static List<QrSegment> makeSegmentsOptimally(String text, QrCode.Ecc ecl, int minVersion, int maxVersion) {
// Check arguments // Check arguments
if (text == null || ecl == null) Objects.requireNonNull(text);
throw new NullPointerException(); Objects.requireNonNull(ecl);
if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40)) if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40))
throw new IllegalArgumentException("Invalid value"); throw new IllegalArgumentException("Invalid value");
@ -238,8 +239,7 @@ public final class QrSegmentAdvanced {
* @see #isEncodableAsKanji(String) * @see #isEncodableAsKanji(String)
*/ */
public static QrSegment makeKanjiSegment(String text) { public static QrSegment makeKanjiSegment(String text) {
if (text == null) Objects.requireNonNull(text);
throw new NullPointerException();
BitBuffer bb = new BitBuffer(); BitBuffer bb = new BitBuffer();
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
int val = UNICODE_TO_QR_KANJI[text.charAt(i)]; int val = UNICODE_TO_QR_KANJI[text.charAt(i)];
@ -262,8 +262,7 @@ public final class QrSegmentAdvanced {
* @see #makeKanjiSegment(String) * @see #makeKanjiSegment(String)
*/ */
public static boolean isEncodableAsKanji(String text) { public static boolean isEncodableAsKanji(String text) {
if (text == null) Objects.requireNonNull(text);
throw new NullPointerException();
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
if (UNICODE_TO_QR_KANJI[text.charAt(i)] == -1) if (UNICODE_TO_QR_KANJI[text.charAt(i)] == -1)
return false; return false;

Loading…
Cancel
Save