From f8c7c8800be70f1fa6dd1e2efa0e40875d7e73fa Mon Sep 17 00:00:00 2001 From: gerzees Date: Wed, 3 Jun 2020 09:39:43 +0900 Subject: [PATCH] 1. Extract method 2. handleConstructorMasking in QrCode 3. To improve readability --- .../main/java/io/nayuki/qrcodegen/QrCode.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java index 162aaf0..f619deb 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java @@ -626,24 +626,32 @@ public final class QrCode { // A messy helper function for the constructor. This QR Code must be in an unmasked state when this // method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed. // This method applies and returns the actual mask chosen, from 0 to 7. - private int handleConstructorMasking(int msk) { - if (msk == -1) { // Automatically choose best mask - int minPenalty = Integer.MAX_VALUE; - for (int i = 0; i < 8; i++) { - applyMask(i); - drawFormatBits(i); - int penalty = getPenaltyScore(); - if (penalty < minPenalty) { - msk = i; - minPenalty = penalty; - } - applyMask(i); // Undoes the mask due to XOR + private int handleConstructorMasking(int mask) { + if (mask == -1) { + mask = findBestMask(); + } + assert 0 <= mask && mask <= 7; + applyMask(mask); // Apply the final choice of mask + drawFormatBits(mask); // Overwrite old format bits + return mask; // The caller shall assign this value to the final-declared field + } + + + // Automatically choose best mask + private int findBestMask() { + int mask = -1; + int minPenalty = Integer.MAX_VALUE; + for (int i = 0; i < 8; i++) { + applyMask(i); + drawFormatBits(i); + int penalty = getPenaltyScore(); + if (penalty < minPenalty) { + mask = i; + minPenalty = penalty; } + applyMask(i); // Undoes the mask due to XOR } - assert 0 <= msk && msk <= 7; - applyMask(msk); // Apply the final choice of mask - drawFormatBits(msk); // Overwrite old format bits - return msk; // The caller shall assign this value to the final-declared field + return mask; }