Refactor checking of write format in 1D writers

This commit is contained in:
Sean Owen 2019-05-31 10:53:24 -05:00
parent 2d77cec7c8
commit 723b65fe3d
9 changed files with 42 additions and 104 deletions

View file

@ -17,11 +17,9 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
/**
* This class renders CodaBar as {@code boolean[]}.
@ -36,16 +34,8 @@ public final class CodaBarWriter extends OneDimensionalCodeWriter {
private static final char DEFAULT_GUARD = START_END_CHARS[0];
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (format != BarcodeFormat.CODABAR) {
throw new IllegalArgumentException("Can only encode CODABAR, but got " + format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.CODABAR);
}
@Override

View file

@ -17,13 +17,11 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Collections;
/**
* This object renders a CODE128 code as a {@link BitMatrix}.
@ -61,15 +59,8 @@ public final class Code128Writer extends OneDimensionalCodeWriter {
}
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (format != BarcodeFormat.CODE_128) {
throw new IllegalArgumentException("Can only encode CODE_128, but got " + format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.CODE_128);
}
@Override

View file

@ -17,11 +17,10 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
/**
* This object renders a CODE39 code as a {@link BitMatrix}.
@ -31,15 +30,8 @@ import java.util.Map;
public final class Code39Writer extends OneDimensionalCodeWriter {
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (format != BarcodeFormat.CODE_39) {
throw new IllegalArgumentException("Can only encode CODE_39, but got " + format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.CODE_39);
}
@Override

View file

@ -16,26 +16,18 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
/**
* This object renders a CODE93 code as a BitMatrix
*/
public class Code93Writer extends OneDimensionalCodeWriter {
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (format != BarcodeFormat.CODE_93) {
throw new IllegalArgumentException("Can only encode CODE_93, but got " + format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.CODE_93);
}
/**

View file

@ -17,12 +17,11 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
/**
* This object renders an EAN13 code as a {@link BitMatrix}.
@ -38,16 +37,8 @@ public final class EAN13Writer extends UPCEANWriter {
3; // end guard
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (format != BarcodeFormat.EAN_13) {
throw new IllegalArgumentException("Can only encode EAN_13, but got " + format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.EAN_13);
}
@Override

View file

@ -17,12 +17,11 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
/**
* This object renders an EAN8 code as a {@link BitMatrix}.
@ -38,17 +37,8 @@ public final class EAN8Writer extends UPCEANWriter {
3; // end guard
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (format != BarcodeFormat.EAN_8) {
throw new IllegalArgumentException("Can only encode EAN_8, but got "
+ format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.EAN_8);
}
/**

View file

@ -17,11 +17,10 @@
package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
/**
* This object renders a ITF code as a {@link BitMatrix}.
@ -52,16 +51,8 @@ public final class ITFWriter extends OneDimensionalCodeWriter {
};
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (format != BarcodeFormat.ITF) {
throw new IllegalArgumentException("Can only encode ITF, but got " + format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.ITF);
}
@Override

View file

@ -22,6 +22,7 @@ import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Pattern;
@ -60,6 +61,11 @@ public abstract class OneDimensionalCodeWriter implements Writer {
throw new IllegalArgumentException("Negative size is not allowed. Input: "
+ width + 'x' + height);
}
Collection<BarcodeFormat> supportedFormats = getSupportedWriteFormats();
if (supportedFormats != null && !supportedFormats.contains(format)) {
throw new IllegalArgumentException("Can only encode " + supportedFormats +
", but got " + format);
}
int sidesMargin = getDefaultMargin();
if (hints != null && hints.containsKey(EncodeHintType.MARGIN)) {
@ -70,6 +76,10 @@ public abstract class OneDimensionalCodeWriter implements Writer {
return renderResult(code, width, height, sidesMargin);
}
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return null;
}
/**
* @return a byte array of horizontal pixels (0 = white, 1 = black)
*/

View file

@ -16,12 +16,11 @@
package com.google.zxing.oned;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
/**
@ -36,16 +35,8 @@ public final class UPCEWriter extends UPCEANWriter {
6; // end guard
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType, ?> hints) throws WriterException {
if (format != BarcodeFormat.UPC_E) {
throw new IllegalArgumentException("Can only encode UPC_E, but got " + format);
}
return super.encode(contents, format, width, height, hints);
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
return Collections.singleton(BarcodeFormat.UPC_E);
}
@Override