summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Roozbeh Pournader <roozbeh@google.com> 2015-08-19 22:38:24 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2015-08-19 22:38:24 +0000
commit5afe8f2089a0c3be0e7e54ca609324edac6a1223 (patch)
tree1fdbe2172bc66e05a344f263acd211f8211387e8
parente5e6f4837b27eefa542e65cdb89c796f28e11ec5 (diff)
parent0ba0d6bb188cf8cd279b0684b70c72323bf1fea2 (diff)
Merge "Add first implementation of LocaleList."
-rw-r--r--api/current.txt9
-rw-r--r--api/system-current.txt9
-rw-r--r--core/java/android/util/LocaleList.java82
3 files changed, 100 insertions, 0 deletions
diff --git a/api/current.txt b/api/current.txt
index 3e1f5a521db6..acc9c4b11024 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -34141,6 +34141,15 @@ package android.util {
field public static final int RTL = 1; // 0x1
}
+ public final class LocaleList {
+ ctor public LocaleList();
+ ctor public LocaleList(java.util.Locale[]);
+ method public java.util.Locale get(int);
+ method public java.util.Locale getPrimary();
+ method public boolean isEmpty();
+ method public int size();
+ }
+
public final class Log {
method public static int d(java.lang.String, java.lang.String);
method public static int d(java.lang.String, java.lang.String, java.lang.Throwable);
diff --git a/api/system-current.txt b/api/system-current.txt
index 984d3953ef86..955fb07e20a7 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -36474,6 +36474,15 @@ package android.util {
field public static final int RTL = 1; // 0x1
}
+ public final class LocaleList {
+ ctor public LocaleList();
+ ctor public LocaleList(java.util.Locale[]);
+ method public java.util.Locale get(int);
+ method public java.util.Locale getPrimary();
+ method public boolean isEmpty();
+ method public int size();
+ }
+
public final class Log {
method public static int d(java.lang.String, java.lang.String);
method public static int d(java.lang.String, java.lang.String, java.lang.Throwable);
diff --git a/core/java/android/util/LocaleList.java b/core/java/android/util/LocaleList.java
new file mode 100644
index 000000000000..017735ac03df
--- /dev/null
+++ b/core/java/android/util/LocaleList.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.util;
+
+import android.annotation.Nullable;
+
+import java.util.HashSet;
+import java.util.Locale;
+
+// TODO: We don't except too many LocaleLists to exist at the same time, and
+// we need access to the data at native level, so we should pass the data
+// down to the native level, create a mapt of every list seen there, take a
+// pointer back, and just keep that pointed in the Java-level object, so
+// things could be copied very quickly.
+
+/**
+ * LocaleList is an immutable list of Locales, typically used to keep an
+ * ordered user preferences for locales.
+ */
+public final class LocaleList {
+ private final Locale[] mList;
+ private static final Locale[] sEmptyList = new Locale[0];
+
+ public Locale get(int location) {
+ return location < mList.length ? mList[location] : null;
+ }
+
+ public Locale getPrimary() {
+ return mList.length == 0 ? null : get(0);
+ }
+
+ public boolean isEmpty() {
+ return mList.length == 0;
+ }
+
+ public int size() {
+ return mList.length;
+ }
+
+ public LocaleList() {
+ mList = sEmptyList;
+ }
+
+ /**
+ * @throws NullPointerException if any of the input locales is <code>null</code>.
+ * @throws IllegalArgumentException if any of the input locales repeat.
+ */
+ public LocaleList(@Nullable Locale[] list) {
+ if (list == null || list.length == 0) {
+ mList = sEmptyList;
+ } else {
+ final Locale[] localeList = new Locale[list.length];
+ final HashSet<Locale> seenLocales = new HashSet<Locale>();
+ for (int i = 0; i < list.length; ++i) {
+ final Locale l = list[i];
+ if (l == null) {
+ throw new NullPointerException();
+ } else if (seenLocales.contains(l)) {
+ throw new IllegalArgumentException();
+ } else {
+ seenLocales.add(l);
+ localeList[i] = (Locale) l.clone();
+ }
+ }
+ mList = localeList;
+ }
+ }
+}