New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Numbers#indicator(integer) #406

Closed
jepsar opened this Issue Sep 28, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@jepsar
Copy link

jepsar commented Sep 28, 2017

This will prevent the need to check for empty or 0 in the user interface to hide brackets or parentheses.

I'm using the following method(s) in one of my projects:

  /**
   * Returns {@code null} if integer is {@code null} or {@code 0}, else the integer formatted using
   * {@link Numbers#formatThousands(java.lang.Number) }, formatted using the default pattern {@code "(%s)"}.
   * For example {@code indicator(1004)} will return {@code (1k)}.
   *
   * @param integer The integer to create an indicator for.
   *
   * @return Indicator string for the provided integer.
   */
  public String indicator(final Integer integer)
  {
    return indicatorPattern(integer, "(%s)");
  }


  /**
   * Returns {@code null} if integer is {@code null} or {@code 0}, else the integer formatted using
   * {@link Numbers#formatThousands(java.lang.Number) }, formatted using the provided pattern.
   * For example {@code indicatorPattern(1004, "(%s)")} will return {@code (1k)}.
   *
   * @param integer The integer to create an indicator for.
   * @param pattern String format pattern to use, for example {@code "(%s)"}.
   *                The integer will be passed as the only argument after it has been
   *                {@link Numbers#formatThousands(java.lang.Number) formatted}.
   *                See {@link Formatter}.
   *
   * @return Indicator string for the provided integer and pattern.
   */
  public String indicatorPattern(final Integer integer, final String pattern)
  {
    if (integer == null || integer == 0) {
      return null;
    }
    return String.format(pattern, Numbers.formatThousands(integer));
  }

@BalusC

This comment has been minimized.

Copy link
Member

BalusC commented Dec 3, 2017

Makes sense, but I'm not sure about the name "indicator". It wouldn't occur to me to use a function with such name for such purpose. Where exactly does that word come from in this context?

@jepsar

This comment has been minimized.

Copy link

jepsar commented Dec 4, 2017

It might indicate the number of (unread) mails / events etc. Maybe amountIndicator is better?

BalusC added a commit that referenced this issue Dec 10, 2017

@BalusC

This comment has been minimized.

Copy link
Member

BalusC commented Dec 10, 2017

of:parenthesize() has been added.

@BalusC BalusC closed this Dec 10, 2017

@jepsar

This comment has been minimized.

Copy link

jepsar commented Dec 22, 2017

I get why you opted for passing an object. However, if you would use formatting (to get strings like "1k"), you could pass a string "0" as a result of formatting. Maybe add a check for instance of string and equals "0"?

BalusC added a commit that referenced this issue Dec 26, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment