All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squarespace.cldr.StringMessageArg Maven / Gradle / Ivy

package com.squarespace.cldr;

import java.math.BigDecimal;


/**
 * Default class for generic String arguments. This may or may not be numbers,
 * so we attempt to force-cast to BigDecimal when needed. Returns a null
 * currency by default.
 */
public class StringMessageArg implements MessageArg {

  private final String value;
  private BigDecimal decimalValue;
  private String currency;

  public StringMessageArg(String value) {
    this.value = value;
  }

  @Override
  public void reset() {
    this.decimalValue = null;
    this.currency = null;
  }
  
  @Override
  public boolean resolve() {
    return true;
  }

  @Override
  public String asString() {
    return value;
  }
  
  @Override
  public long asLong() {
    return value == null ? 0 : MessageFormat.toLong(value, 0, value.length());
  }

  @Override
  public BigDecimal asBigDecimal() {
    try {
      if (decimalValue == null) {
        decimalValue = new BigDecimal(value);
      }
    } catch (NumberFormatException e) {
      decimalValue = BigDecimal.ZERO;
    }
    return decimalValue;
  }

  @Override
  public String currency() {
    return currency;
  }

  public void setCurrency(String currency) {
    this.currency = currency;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy