/** * Create a new instance of the size value that only contains a type. This only works for types that allow to be * used without value. * * @param type the type * @throws java.lang.IllegalArgumentException in case the type requires a value * @see SizeValueType#getValueRequirement() */ public SizeValue(@Nonnull final SizeValueType type) { if (type.getValueRequirement() == SizeValueType.ValueRequirement.Required) { throw new IllegalArgumentException("Size value type " + type.name() + " requires a value!"); } this.type = type; value = 0.0f; hasValue = false; hasCalculatedValue = false; }
@Override public int hashCode() { int result = type.hashCode(); result = 31 * result + (value != +0.0f ? Float.floatToIntBits(value) : 0); result = 31 * result + (hasCalculatedValue ? 1 : 0); result = 31 * result + (hasValue ? 1 : 0); return result; } }
/** * Do we need to know the size of the parent element to calculate this value? * * @return {@code true} if the size of this value can be calculated without knowing about the parent. */ public boolean isIndependentFromParent() { return type.isIndependent(); }
for (@Nonnull final SizeValueType currentType : SizeValueType.values()) { if (valueParam.endsWith(currentType.getExtension())) { selectedType = currentType; break; final int extensionLength = type.getExtension().length(); switch (type.getValueRequirement()) { case CalculatedOnly: throw new IllegalArgumentException("Setting the value by string is not allowed for types that only allow " + "a calculated value."); case Forbidden: throw new IllegalArgumentException("The size type " + type.name() + " does not allow any values."); if (type.getValueRequirement() == SizeValueType.ValueRequirement.Required) { throw new IllegalArgumentException("Size value type " + type.name() + " requires a value!");
/** * Get a string representation of this SizeValue that complies with the formatting * standards for {@link #SizeValue(String)} constructor. * @return a well-formed string representation of this SizeValue */ public String getValueAsString() { final StringBuilder builder = new StringBuilder(); if (hasValue) { builder.append(value); } builder.append(type.getExtension()); return builder.toString(); }
/** * Get a generic string representation of this size value. * <p /> * The output of this function is most likely <b>not</b> valid to be parsed by the {@link #SizeValue(String)} * constructor as it contains the current calculated value and this constructor does not support this kind of value. * To get a String value parseable by the {@link #SizeValue(String)} constructor, use {@link #getValueAsString()}. * * @return the string representation of this instance */ @Nonnull @Override public String toString() { StringBuilder builder = new StringBuilder(); if (hasValue) { builder.append(value); } builder.append(type.getExtension()); if (hasCalculatedValue) { builder.append('[').append(value).append("px]"); } return builder.toString(); }
/** * Create a new instance of the size value that contains a value and a type. This only works for types that allow a * fixed set value. * * @param value the value * @param type the type * @throws java.lang.IllegalArgumentException in case the type forbids the usage of a value or only accepts a * calculated value * @see SizeValueType#getValueRequirement() */ public SizeValue(final int value, @Nonnull final SizeValueType type) { switch (type.getValueRequirement()) { case Forbidden: throw new IllegalArgumentException("Size value type " + type.name() + " does not allow a value."); case CalculatedOnly: throw new IllegalArgumentException("Size value type " + type.name() + " does only allow calculated values"); } this.type = type; this.value = value; hasValue = true; hasCalculatedValue = false; }
/** * Create a new instance of a size value that contains a computed value and a type. This only works for values that * do not require a fixed value and allow a computed value. * * @param type the type * @param calculatedValue the computed value * @throws java.lang.IllegalArgumentException in case the type forbids the usage of values or requires a fixed set * value * * @see SizeValueType#getValueRequirement() */ public SizeValue(@Nonnull final SizeValueType type, final int calculatedValue) { switch (type.getValueRequirement()) { case Forbidden: throw new IllegalArgumentException("Size value type " + type.name() + " does not allow a value."); case Required: throw new IllegalArgumentException("Size value type " + type.name() + " requires a value!"); } this.type = type; this.value = calculatedValue; hasValue = false; hasCalculatedValue = true; }