Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8301293

java.util.Locale method to stream available Locales

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 21
    • core-libs
    • None
    • minimal
    • The compatibility risk is minimal, as no existing methods are modified.
    • Java API
    • SE

      Summary

      Introduce a new method to java.util.Locale that returns a Stream of Locale(s).

      Problem

      Currently, Locale only offers java.util.Locale.getAvailableLocales() which returns an array of Locales. If a user wants a stream of available Locales, they have to call Stream.of() on the array themselves. In addition, by calling Stream.of(Locale.getAvailableLocales()), they are streaming a clone of the original Locale array.

      Solution

      Introducing Locale.availableLocales() would only require a few additional lines of code, while providing the benefits of Stream filtering and selecting capabilities for the Locale class. It would also allow the user to stream the Locale array itself as opposed to the cloned copy.

      Specification

      @@ -1210,6 +1211,21 @@ public final class Locale implements Cloneable, Serializable {
      
      +    /**
      +     * {@return A stream of installed locales.}
      +     * 
      +     * The returned stream represents the union of locales supported
      +     * by the Java runtime environment and by installed
      +     * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
      +     * implementations. At a minimum, the returned stream must contain a
      +     * {@code Locale} instance equal to {@link Locale#ROOT Locale.ROOT} and
      +     * a {@code Locale} instance equal to {@link Locale#US Locale.US}.
      +     * @implNote Unlike {@code getAvailableLocales()}, this method does
      +     * not create a defensive copy of the Locale array.
      +     * @since 21
      +     */
      +    public static Stream<Locale> availableLocales() {

            jlu Justin Lu
            rriggs Roger Riggs
            Naoto Sato, Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: