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

com.squarespace.cldrengine.numbers.NumbersImpl Maven / Gradle / Ivy

The newest version!
package com.squarespace.cldrengine.numbers;

import java.util.List;
import java.util.Map;

import com.squarespace.cldrengine.api.Bundle;
import com.squarespace.cldrengine.api.ContextTransformFieldType;
import com.squarespace.cldrengine.api.ContextType;
import com.squarespace.cldrengine.api.CurrencyDisplayNameOptions;
import com.squarespace.cldrengine.api.CurrencyFormatOptions;
import com.squarespace.cldrengine.api.CurrencyFractions;
import com.squarespace.cldrengine.api.CurrencySymbolWidthType;
import com.squarespace.cldrengine.api.CurrencyType;
import com.squarespace.cldrengine.api.Decimal;
import com.squarespace.cldrengine.api.DecimalAdjustOptions;
import com.squarespace.cldrengine.api.DecimalFormatOptions;
import com.squarespace.cldrengine.api.NumberSymbolType;
import com.squarespace.cldrengine.api.Numbers;
import com.squarespace.cldrengine.api.Pair;
import com.squarespace.cldrengine.api.Part;
import com.squarespace.cldrengine.api.PluralType;
import com.squarespace.cldrengine.general.GeneralInternals;
import com.squarespace.cldrengine.internal.Internals;
import com.squarespace.cldrengine.internal.PrivateApi;

public class NumbersImpl implements Numbers {

  public final Bundle bundle;
  public final NumberInternals numbers;
  public final GeneralInternals general;
  public final PrivateApi privateApi;

  public NumbersImpl(Bundle bundle, Internals internals, PrivateApi privateApi) {
    this.bundle = bundle;
    this.numbers = internals.numbers;
    this.general = internals.general;
    this.privateApi = privateApi;
  }

  public Decimal adjustDecimal(Decimal num) {
    return this.numbers.adjustDecimal(num, null);
  }

  public Decimal adjustDecimal(Decimal num, DecimalAdjustOptions options) {
    return this.numbers.adjustDecimal(num, options);
  }

  public String formatDecimal(Decimal n) {
    return formatDecimal(n, null);
  }

  public String formatDecimal(Decimal n, DecimalFormatOptions options) {
    options = (options == null ? DecimalFormatOptions.build() : options);
    NumberParams params = this.privateApi.getNumberParams(options.numberSystem.get(), null);
    NumberRenderer renderer = this.numbers.stringRenderer(params);
    return this.formatDecimalImpl(renderer, params, n, options);
  }

  public List formatDecimalToParts(Decimal n) {
    return formatDecimalToParts(n, null);
  }

  public List formatDecimalToParts(Decimal n, DecimalFormatOptions options) {
    options = (options == null ? DecimalFormatOptions.build() : options);
    NumberParams params = this.privateApi.getNumberParams(options.numberSystem.get(), null);
    NumberRenderer> renderer = this.numbers.partsRenderer(params);
    return this.formatDecimalImpl(renderer, params, n, options);
  }

  public String formatCurrency(Decimal n, CurrencyType code) {
    return formatCurrency(n, code, null);
  }

  public String formatCurrency(Decimal n, CurrencyType code, CurrencyFormatOptions options) {
    options = (options == null ? CurrencyFormatOptions.build() : options);
    NumberParams params = this.privateApi.getNumberParams(options.numberSystem.get(), "finance");
    NumberRenderer renderer = this.numbers.stringRenderer(params);
    return this.formatCurrencyImpl(renderer, params, n, code, options);
  }

  public List formatCurrencyToParts(Decimal n, CurrencyType code) {
    return formatCurrencyToParts(n, code, null);
  }

  public List formatCurrencyToParts(Decimal n, CurrencyType code, CurrencyFormatOptions options) {
    options = (options == null ? CurrencyFormatOptions.build() : options);
    NumberParams params = this.privateApi.getNumberParams(options.numberSystem.get(), "finance");
    NumberRenderer> renderer = this.numbers.partsRenderer(params);
    return this.formatCurrencyImpl(renderer, params, n, code, options);
  }

  public String getCurrencySymbol(CurrencyType code) {
    return getCurrencySymbol(code, null);
  }

  public String getCurrencySymbol(CurrencyType code, CurrencySymbolWidthType width) {
    return this.numbers.getCurrencySymbol(bundle, code, width);
  }

  public String getCurrencyDisplayName(CurrencyType code) {
    return getCurrencyDisplayName(code, null);
  }

  public String getCurrencyDisplayName(CurrencyType code, CurrencyDisplayNameOptions options) {
    options = (options == null ? CurrencyDisplayNameOptions.build() : options);
    ContextType context = options.context.or(ContextType.BEGIN_SENTENCE);
    String name = this.numbers.getCurrencyDisplayName(this.bundle, code);
    Map transform = this.privateApi.getContextTransformInfo();
    return this.general.contextTransform(name, transform, context, ContextTransformFieldType.CURRENCYNAME);
  }

  public String getCurrencyPluralName(Decimal n, CurrencyType code) {
    return getCurrencyPluralName(n, code, null);
  }

  public String getCurrencyPluralName(Decimal n, CurrencyType code, CurrencyDisplayNameOptions options) {
    options = (options == null ? CurrencyDisplayNameOptions.build() : options);
    ContextType context = options.context.or(ContextType.BEGIN_SENTENCE);
    // TODO: support adjustment options here
    PluralType plural = this.getPluralCardinal(n, null);
    String name = this.numbers.getCurrencyPluralName(bundle, code, plural);
    Map transform = this.privateApi.getContextTransformInfo();
    return this.general.contextTransform(name, transform, context, ContextTransformFieldType.CURRENCYNAME);
  }

  public CurrencyFractions getCurrencyFractions(CurrencyType code) {
    return this.numbers.getCurrencyFractions(code);
  }

  public CurrencyType getCurrencyForRegion(String region) {
    return this.numbers.getCurrencyForRegion(region);
  }

  public PluralType getPluralCardinal(Decimal n) {
    return getPluralCardinal(n, null);
  }

  public PluralType getPluralCardinal(Decimal n, DecimalAdjustOptions options) {
    if (options != null) {
      n = this.adjustDecimal(n, options);
    }
    return this.bundle.plurals().cardinal(n);
  }

  public PluralType getPluralOrdinal(Decimal n) {
    return getPluralOrdinal(n, null);
  }

  public PluralType getPluralOrdinal(Decimal n, DecimalAdjustOptions options) {
    if (options != null) {
      n = this.adjustDecimal(n, options);
    }
    return this.bundle.plurals().ordinal(n);
  }

  protected  T formatDecimalImpl(NumberRenderer renderer, NumberParams params,
      Decimal n, DecimalFormatOptions options) {

    // A NaN or Infinity value will just return the locale's representation
    T v = validate(n, options, renderer, params);
    if (v != null) {
      return v;
    }
    Pair result = this.numbers.formatDecimal(bundle, renderer, n, options, params);
    return result._1;
  }

  protected  T formatCurrencyImpl(NumberRenderer renderer, NumberParams params,
      Decimal n, CurrencyType code, CurrencyFormatOptions options) {
    if (n.isNaN()) {
      throw new IllegalArgumentException("Invalid argument: NaN");
    } else if (n.isInfinity()) {
      throw new IllegalArgumentException("Invalid argument: Infinity");
    }
    return this.numbers.formatCurrency(this.bundle, renderer, n, code, options, params);
  }

  protected  T validate(Decimal n, DecimalFormatOptions options, NumberRenderer renderer,
      NumberParams params) {

    String[] errors = null;
    if (options.errors.ok()) {
      errors = options.errors.get().split("[\\s,]+");
    }

    // Check if we have NaN or Infinity
    boolean isnan = n.isNaN();
    boolean isinfinity = n.isInfinity();

    if (errors != null) {
      // Check if we should throw an error on either of these
      if (isnan && arrayContains(errors, "nan")) {
        throw new IllegalArgumentException("Invalid argument: NaN");
      }
      if (isinfinity && arrayContains(errors, "infinity")) {
        throw new IllegalArgumentException("Invalid argument: Infinity");
      }
    }
    if (isnan) {
      return renderer.make("nan", params.symbols.get(NumberSymbolType.NAN));
    } else if (isinfinity) {
      return renderer.make("infinity", params.symbols.get(NumberSymbolType.INFINITY));
    }
    return null;

  }

  protected static  boolean arrayContains(T[] arr, T t) {
    for (T elem : arr) {
      if (elem.equals(t)) {
        return true;
      }
    }
    return false;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy