@Override public String randomValueAsString(Field<TYPE> field) { TYPE randomValue = randomValue(field); String stringValue = randomValue.toString(); if (field.getPadding() != null) { long maxLength = field.getMaxLength() != 0 ? field.getMaxLength() : field.getFixedLength(); stringValue = paddedRandomValue(field.getPadding(), randomValue, maxLength); } return stringValue; }
/** * This method is used to apply padding to a random value to make it the required length * * @param padding the value to be padded * @param value the generated random value * @param maxLength the maximum length of the random value * @return the padded random value as String */ private String paddedRandomValue(String padding, TYPE value, long maxLength) { long paddingLength = maxLength - value.toString().length(); String formattedValue = value.toString(); if (paddingLength <= 0) { return formattedValue; } StringBuilder valueWithPadding = new StringBuilder(padding); while (valueWithPadding.length() < paddingLength) { valueWithPadding.append(padding); } valueWithPadding.append(value.toString()); if (valueWithPadding.length() > maxLength) { formattedValue = valueWithPadding.substring(0, (int) maxLength); } else { formattedValue = valueWithPadding.toString(); } return formattedValue; }
/** * Constructs a new {@code EnumParam}, initialized to the default value. Limit the set of options * with the supplied {@code EnumSet}. * * @param name of the {@code Param} (max 72 chars) * @param info {@code String} for use in tooltips; a {@code null} value will be set to an empty * {@code String} (max 256 chars) * @param defaultValue of the {@code Param} * @param options a subset of the option type * @throws IllegalArgumentException if {@code name} is an empty string, {@code options} is empty, * or the {@code defaultValue} is not included in {@code options} */ DefaultEnumParam(String name, String info, E defaultValue, Set<E> options) { super(name, info, defaultValue); checkNotNull(options); checkArgument(!options.isEmpty(), "Options is empty"); checkArgument(options.contains(defaultValue), "Default missing from options"); this.options = Sets.immutableEnumSet(options); // init state state.addProperty(TYPE.toString(), ENUM.toString()); JsonArray stateOptions = new JsonArray(); for (E option : options) { stateOptions.add(new JsonPrimitive(option.name())); } state.add(OPTIONS.toString(), stateOptions); }