Skip to content

Commit

Permalink
Cleanup of the Builder Methods loading Defaults for invalid inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
yaforster committed Dec 7, 2023
1 parent fcb7521 commit 11b1582
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CaptchaRenderer extends AbstractCaptchaRenderer {
/**
* Defines the maximum angle that can be used to rotate a single character in the captcha
*/
private final Double maxrotateAngle;
private final Double maximumLetterRotationAngle;
/**
* String name of the font to be used when writing characters to the image
*/
Expand All @@ -55,16 +55,14 @@ public class CaptchaRenderer extends AbstractCaptchaRenderer {

@Builder
public CaptchaRenderer(int pictureHeight, int pictureWidth, List<Color> availableTextColors, String imgFileFormat
, Double maxrotateAngle, String fontName, List<AbstractBufferedImageOp> imageOperationsList,
, Double maximumLetterRotationAngle, String fontName, List<AbstractBufferedImageOp> imageOperationsList,
AbstractCaptchaImageBackground imageBackground, NoiseSettings noiseSettings) {
super(getPictureHeightOrDefault(pictureHeight), getPictureWidthOrDefault(pictureWidth),
(availableTextColors == null || availableTextColors.isEmpty()) ?
Collections.singletonList(Color.BLACK) : availableTextColors,
StringUtils.isBlank(imgFileFormat) ? DEFAULT_IMAGE_FORMAT : imgFileFormat);
this.maxrotateAngle = Optional.ofNullable(maxrotateAngle).orElse(DEFAULT_MAX_LETTER_ROTATION_ANGLE);
this.fontName = StringUtils.isBlank(fontName) ? DEFAULT_FONT : fontName;
this.imageOperationsList = Optional.ofNullable(imageOperationsList).orElse(Collections.emptyList());
this.imageBackground = Optional.ofNullable(imageBackground).orElse(DEFAULT_BACKGROUND);
getAvailableTextColorsOrDefault(availableTextColors), getImageFileFormatOrDefault(imgFileFormat));
this.maximumLetterRotationAngle = getMaximumLetterRotationAngleOrDefault(maximumLetterRotationAngle);
this.fontName = getFontNameOrDefault(fontName);
this.imageOperationsList = getImageOperationsListOrDefault(imageOperationsList);
this.imageBackground = getImageBackgroundOrDefault(imageBackground);
this.noiseSettings = getNoiseSettingsOrDefault(noiseSettings);
}

Expand All @@ -82,6 +80,31 @@ private static int getPictureWidthOrDefault(int pictureWidth) {
return pictureWidth != 0 ? pictureWidth : DEFAULT_PICTURE_WIDTH;
}

private static List<Color> getAvailableTextColorsOrDefault(List<Color> availableTextColors) {
return (availableTextColors == null || availableTextColors.isEmpty()) ?
Collections.singletonList(Color.BLACK) : availableTextColors;
}

private static String getImageFileFormatOrDefault(String imgFileFormat) {
return StringUtils.isBlank(imgFileFormat) ? DEFAULT_IMAGE_FORMAT : imgFileFormat;
}

private static Double getMaximumLetterRotationAngleOrDefault(Double maxrotateAngle) {
return Optional.ofNullable(maxrotateAngle).orElse(DEFAULT_MAX_LETTER_ROTATION_ANGLE);
}

private static String getFontNameOrDefault(String fontName) {
return StringUtils.isBlank(fontName) ? DEFAULT_FONT : fontName;
}

private static List<AbstractBufferedImageOp> getImageOperationsListOrDefault(List<AbstractBufferedImageOp> imageOperationsList) {
return Optional.ofNullable(imageOperationsList).orElse(Collections.emptyList());
}

private static AbstractCaptchaImageBackground getImageBackgroundOrDefault(AbstractCaptchaImageBackground imageBackground) {
return Optional.ofNullable(imageBackground).orElse(DEFAULT_BACKGROUND);
}

/**
* Sets a clean NoiseSetting that will not produce any noise when rendered if the given noiseSetting is null or
* contains no color.
Expand Down Expand Up @@ -254,7 +277,7 @@ private static int getHorizontalPlacementOfCharacter(TextRenderingData tRD, Inte
}

private double getRandomAngleWithinMaximumBounds() {
return (new SecureRandom().nextDouble() - 0.5) * maxrotateAngle;
return (new SecureRandom().nextDouble() - 0.5) * maximumLetterRotationAngle;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ public class CaptchaCipher extends AbstractCaptchaCipher {
@Builder
private CaptchaCipher(CipherSettings cipherSettings, String encryptionPassword,
ExpirationTimeSettings expirationTimeSettings) {
super(Optional.ofNullable(cipherSettings)
.orElse(CipherSettings.getDefaultCipherSettings()), Optional.ofNullable(encryptionPassword)
.orElse(StringUtils.EMPTY), expirationTimeSettings);
super(getCipherSettingsOrDefault(cipherSettings), getEncryptionPasswordOrDefault(encryptionPassword),
expirationTimeSettings);
}

private static CipherSettings getCipherSettingsOrDefault(CipherSettings cipherSettings) {
return Optional.ofNullable(cipherSettings).orElse(CipherSettings.getDefaultCipherSettings());
}

private static String getEncryptionPasswordOrDefault(String encryptionPassword) {
return Optional.ofNullable(encryptionPassword).orElse(StringUtils.EMPTY);
}

@Override
Expand Down

This file was deleted.

0 comments on commit 11b1582

Please sign in to comment.