Custom Types

Enumerations allow to specify a range of valid values to select a property value from.

Custom Type

The custom type is required to be serializable.

Here is an example of a custom type:

public final class CredentialsProperty implements Serializable
{
  private static final long serialVersionUID = 1L;

  private final String userId;

  private final String password;

  private CredentialsProperty(final Builder builder)
  {
    this.userId = builder.userId;
    this.password = builder.password;
  }

  private static final class Builder
  {
    private String userId;

    private String password;

    Builder withUserId(final String userId)
    {
      this.userId = userId;
      return this;
    }

    Builder withPassword(final String password)
    {
      this.password = password;
      return this;
    }

    CredentialsProperty build()
    {
      return new CredentialsProperty(this);
    }
  }

  public static CredentialsProperty fromString(final String string)
    throws IllegalArgumentException
  {
    Arguments.checkNotBlank("string", string);

    final CredentialsProperty.Builder builder =
        new CredentialsProperty.Builder();

    final int splitIndex = string.indexOf(':');
    if (splitIndex == -1 || splitIndex == string.length() - 1)
    {
      builder.withUserId(string);
    }
    else
    {
      final String userId = string.substring(0, splitIndex);
      final String password = string.substring(splitIndex + 1);
      builder.withUserId(userId);
      builder.withPassword(password);
    }

    final CredentialsProperty property = builder.build();
    return property;
  }

  public String getUserId()
  {
    return userId;
  }

  public String getPassword()
  {
    return password;
  }

  @Override
  public int hashCode()
  {
    return userId.hashCode();
  }

  @Override
  public boolean equals(final Object object)
  {
    if (this == object)
    {
      return true;
    }
    else if (object == null || getClass() != object.getClass())
    {
      return false;
    }

    final CredentialsProperty other = (CredentialsProperty) object;

    return (userId.equals(other.userId) && ObjectUtils.equals(password,
        other.password));
  }

  @Override
  public String toString()
  {
    final StringBuilder buffer = new StringBuilder();

    buffer.append(userId);

    if (StringUtils.isNotBlank(password))
    {
      buffer.append(':').append(password);
    }

    return buffer.toString();
  }
}

The static fromString and the toString are required and have to create an instance of the custom type from and to its string representation. Please also provide a valid hashCode and equals (smartics-test-utils may be of help in testing the implementation).

Using Custom Types

Using custom types is straight forward.

    CredentialsProperty credentials();

Custom types can be used in lists, too.

@PropertyElementType(CredentialsProperty.class)
List<CredentialsProperty> credentialList();

Please refer to smartics-properties-doc for further examples.