/** * @param rgbaCssValue the rgbaCssValue to set * @since 1.0.0 * @author WFF */ public void setRgbaCssValue(final RgbaCssValue rgbaCssValue) { if (rgbaCssValue == null) { throw new NullValueException("rgbaCssValue can not be null"); } if (this.rgbaCssValue != null) { if (rgbaCssValue.isAlreadyInUse() && this.rgbaCssValue.getStateChangeInformer() != rgbaCssValue.getStateChangeInformer()) { try { final RgbaCssValue rgbaCssValueClone = CloneUtil.deepClone(rgbaCssValue); this.rgbaCssValue.setAlreadyInUse(false); this.rgbaCssValue = rgbaCssValueClone; if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning( "cloned rgbaCssValue " + rgbaCssValue + "(hashcode: " + rgbaCssValue.hashCode() + ") as it is already used by another object"); } } catch (final CloneNotSupportedException e) { throw new InvalidValueException(e.toString()); } } else { this.rgbaCssValue.setAlreadyInUse(false); this.rgbaCssValue = rgbaCssValue; } } else { if (rgbaCssValue.isAlreadyInUse()) { try { this.rgbaCssValue = CloneUtil.deepClone(rgbaCssValue); if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning( "cloned rgbaCssValue " + rgbaCssValue + "(hashcode: " + rgbaCssValue.hashCode() + ") as it is already used by another object"); } } catch (final CloneNotSupportedException e) { throw new InvalidValueException(e.toString()); } } else { this.rgbaCssValue = rgbaCssValue; } } cssValue = this.rgbaCssValue.getValue(); this.rgbaCssValue.setStateChangeInformer(this); this.rgbaCssValue.setAlreadyInUse(true); makeRgbCssValueNull(); makeHslCssValueNull(); makeHslaCssValueNull(); }
/** * @param cssValue * @return true if the given {@code cssValue} is valid. * @since 1.0.0 * @author WFF */ public static boolean isValid(final String cssValue) { if (cssValue == null) { return false; } final String trimmedCssValue = cssValue.trim(); if (trimmedCssValue.isEmpty()) { return false; } final String trimmedCssValueLowerCase = TagStringUtil.toLowerCase(trimmedCssValue); if (INITIAL.equals(trimmedCssValueLowerCase) || INHERIT.equals(trimmedCssValueLowerCase) || TRANSPARENT.equals(trimmedCssValueLowerCase)) { return true; } if (CssColorName.isValid(trimmedCssValue)) { return true; } if (RgbCssValue.isValid(trimmedCssValue)) { return true; } if (RgbaCssValue.isValid(trimmedCssValue)) { return true; } if (HslCssValue.isValid(trimmedCssValue)) { return true; } if (HslaCssValue.isValid(trimmedCssValue)) { return true; } try { if (!trimmedCssValue.startsWith("#")) { return false; } final long value = Long.parseLong(cssValue.replaceFirst("[#]", ""), 16); return !(value > CommonConstants.FFFFFF_HEX_VALUE || value < 0); } catch (final NumberFormatException ex) { } return false; }
private void makeRgbaCssValueNull() { if (rgbaCssValue != null) { rgbaCssValue.setAlreadyInUse(false); rgbaCssValue = null; } }
/** * @param cssValue the value should be a color/color code/rgb, for example <code>#0000ff</code>. * {@code null} is considered as an invalid value and it will throw {@code * NullValueException}.And an empty string is also considered as an invalid value and it will * throw {@code InvalidValueException}. * @since 1.0.0 * @author WFF */ @Override public BorderTopColor setCssValue(final String cssValue) { final String previousCssValue = this.cssValue; try { if (cssValue == null) { throw new NullValueException( "null is an invalid value. The value should be any color for example #0000ff, rgb(15, 25, 155) Or, initial/inherit/transparent."); } else if (!isValid(cssValue)) { throw new InvalidValueException( cssValue + " is an invalid value. The value should be any color for example #0000ff, rgb(15, 25, 155) Or, initial/inherit/transparent."); } else { this.cssValue = cssValue.trim(); if (RgbCssValue.isValid(cssValue)) { if (rgbCssValue == null) { rgbCssValue = new RgbCssValue(cssValue); } else { rgbCssValue.setRgb(this.cssValue); } rgbCssValue.setStateChangeInformer(this); } else { makeRgbCssValueNull(); } if (RgbaCssValue.isValid(cssValue)) { if (rgbaCssValue == null) { rgbaCssValue = new RgbaCssValue(cssValue); } else { rgbaCssValue.setRgba(this.cssValue); } rgbaCssValue.setStateChangeInformer(this); } else { makeRgbaCssValueNull(); } if (HslCssValue.isValid(cssValue)) { if (hslCssValue == null) { hslCssValue = new HslCssValue(cssValue); } else { hslCssValue.setHsl(this.cssValue); } hslCssValue.setStateChangeInformer(this); } else { makeHslCssValueNull(); } if (HslaCssValue.isValid(cssValue)) { if (hslaCssValue == null) { hslaCssValue = new HslaCssValue(cssValue); } else { hslaCssValue.setHsla(this.cssValue); } hslaCssValue.setStateChangeInformer(this); } else { makeHslaCssValueNull(); } if (getStateChangeInformer() != null) { getStateChangeInformer().stateChanged(this); } } } catch (final InvalidValueException e) { this.cssValue = previousCssValue; throw e; } return this; }