Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Add number format expression (#15424)
Browse files Browse the repository at this point in the history
* [android] add number format expression
  • Loading branch information
tobrun authored Sep 2, 2019
1 parent 3c23a29 commit 0fa5944
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 13 deletions.
1 change: 1 addition & 0 deletions platform/android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Mapbox welcomes participation and contributions from everyone. If you'd like to
### Features
- Introduce `clusterProperties` option for aggregated cluster properties. [#15425](https://github.com/mapbox/mapbox-gl-native/pull/15425)
- Expose the `CameraPosition#padding` field and associated utility camera position builders. This gives a choice to set a persisting map padding immediately during a transition instead of setting it lazily `MapboxMap#setPadding`, which required scheduling additional transition to be applied. This also deprecates `MapboxMap#setPadding` as there should be no need for a lazy padding setter. [#15444](https://github.com/mapbox/mapbox-gl-native/pull/15444)
- Add number-format expression that allows to format a number to a string, with configurations as minimal/maximal fraction and locale/currency. [#15424](https://github.com/mapbox/mapbox-gl-native/pull/15424)

### Performance improvements
- Mark used offline region resources in batches. [#15521](https://github.com/mapbox/mapbox-gl-native/pull/15521)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Size;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -3144,6 +3143,40 @@ public static Expression number(@NonNull Expression... input) {
return new Expression("number", input);
}

/**
* Converts the input number into a string representation using the providing formatting rules.
* If set, the locale argument specifies the locale to use, as a BCP 47 language tag.
* If set, the currency argument specifies an ISO 4217 code to use for currency-style formatting.
* If set, the min-fraction-digits and max-fraction-digits arguments specify the minimum and maximum number
* of fractional digits to include.
*
* @param number number expression
* @param options number formatting options
* @return expression
*/
public static Expression numberFormat(@NonNull Expression number, @NonNull NumberFormatOption... options) {
final Map<String, Expression> map = new HashMap<>();
for (NumberFormatOption option : options) {
map.put(option.type, option.value);
}
return new Expression("number-format", number, new ExpressionMap(map));
}

/**
* Converts the input number into a string representation using the providing formatting rules.
* If set, the locale argument specifies the locale to use, as a BCP 47 language tag.
* If set, the currency argument specifies an ISO 4217 code to use for currency-style formatting.
* If set, the min-fraction-digits and max-fraction-digits arguments specify the minimum and maximum number
* of fractional digits to include.
*
* @param number number expression
* @param options number formatting options
* @return expression
*/
public static Expression numberFormat(@NonNull Number number, @NonNull NumberFormatOption... options) {
return numberFormat(literal(number), options);
}

/**
* Asserts that the input value is a boolean.
* If multiple values are provided, each one is evaluated in order until a boolean value is obtained.
Expand Down Expand Up @@ -4385,21 +4418,138 @@ public static class FormatEntry {
}
}

/**
* Base class for an option entry that is encapsulated as a json object member for an expression.
*/
private static class Option {
@NonNull
String type;
@NonNull
Expression value;

/**
* Create an option option entry that is encapsulated as a json object member for an expression.
*
* @param type json object member name
* @param value json object member value
*/
Option(@NonNull String type, @NonNull Expression value) {
this.type = type;
this.value = value;
}
}

/**
* Holds format options used in a {@link #numberFormat(Number, NumberFormatOption...)} expression.
*/
public static class NumberFormatOption extends Option {

/**
* {@inheritDoc}
*/
NumberFormatOption(@NonNull String type, @NonNull Expression value) {
super(type, value);
}

/**
* Number formatting option for specifying the locale to use, as a BCP 47 language tag.
*
* @param string the locale to use while performing number formatting
* @return number format option
*/
@NonNull
public static NumberFormatOption locale(@NonNull Expression string) {
return new NumberFormatOption("locale", string);
}

/**
* Number formatting option for specifying the locale to use, as a BCP 47 language tag.
*
* @param string the locale to use while performing number formatting
* @return number format option
*/
@NonNull
public static NumberFormatOption locale(@NonNull String string) {
return new NumberFormatOption("locale", literal(string));
}

/**
* Number formatting option for specifying the currency to use, an ISO 4217 code.
*
* @param string the currency to use while performing number formatting
* @return number format option
*/
@NonNull
public static NumberFormatOption currency(@NonNull Expression string) {
return new NumberFormatOption("currency", string);
}

/**
* Number formatting options for specifying the currency to use, an ISO 4217 code.
*
* @param string the currency to use while performing number formatting
* @return number format option
*/
@NonNull
public static NumberFormatOption currency(@NonNull String string) {
return new NumberFormatOption("currency", literal(string));
}

/**
* Number formatting options for specifying the minimum fraction digits to include.
*
* @param number the amount of minimum fraction digits to include
* @return number format option
*/
@NonNull
public static NumberFormatOption minFractionDigits(@NonNull Expression number) {
return new NumberFormatOption("min-fraction-digits", number);
}

/**
* Number formatting options for specifying the minimum fraction digits to include.
*
* @param number the amount of minimum fraction digits to include
* @return number format option
*/
@NonNull
public static NumberFormatOption minFractionDigits(int number) {
return new NumberFormatOption("min-fraction-digits", literal(number));
}

/**
* Number formatting options for specifying the maximum fraction digits to include.
*
* @param number the amount of minimum fraction digits to include
* @return number format option
*/
@NonNull
public static NumberFormatOption maxFractionDigits(@NonNull Expression number) {
return new NumberFormatOption("max-fraction-digits", number);
}

/**
* Number formatting options for specifying the maximum fraction digits to include.
*
* @param number the amount of minimum fraction digits to include
* @return number format option
*/
@NonNull
public static NumberFormatOption maxFractionDigits(@NonNull int number) {
return new NumberFormatOption("max-fraction-digits", literal(number));
}
}

/**
* Holds format options used in a {@link #formatEntry(Expression, FormatOption...)} that builds
* a {@link #format(FormatEntry...)} expression.
* <p>
* If an option is not set, it defaults to the base value defined for the symbol.
*/
public static class FormatOption {
@NonNull
private String type;
@NonNull
private Expression value;
public static class FormatOption extends Option {

FormatOption(@NonNull String type, @NonNull Expression value) {
this.type = type;
this.value = value;
super(type, value);
}

/**
Expand Down
Loading

0 comments on commit 0fa5944

Please sign in to comment.