Bug 1270213 - Add UUIDUtil.isUUID. r=sebastian
I don't actually use isUUID util, but rather the static regex constants. I'm
not sure if I should remove it because:
1) I use it to test the regex constants/patterns are correct
2) It seems like someone might find it useful at some point
MozReview-Commit-ID: S5BbzEVEka
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/util/UUIDUtil.java
@@ -0,0 +1,29 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package org.mozilla.gecko.util;
+
+import android.support.annotation.NonNull;
+
+import java.util.regex.Pattern;
+
+/**
+ * Utilities for UUIDs.
+ */
+public class UUIDUtil {
+ private UUIDUtil() {}
+
+ public static final String UUID_REGEX = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
+ public static final Pattern UUID_PATTERN = Pattern.compile(UUID_REGEX);
+
+ /**
+ * Return if the given string is a UUID. A caller can use `UUID.fromString` and catch the exception
+ * for this same behavior, but exceptions cause a performance hit and shouldn't be used for control flow.
+ */
+ public static boolean isUUID(@NonNull final String UUID) {
+ return UUID_PATTERN.matcher(UUID).matches();
+ }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -120,16 +120,17 @@ gujar.sources += ['java/org/mozilla/geck
'util/NetworkUtils.java',
'util/NonEvictingLruCache.java',
'util/PrefUtils.java',
'util/ProxySelector.java',
'util/RawResource.java',
'util/StringUtils.java',
'util/ThreadUtils.java',
'util/UIAsyncTask.java',
+ 'util/UUIDUtil.java',
'util/WeakReferenceHandler.java',
'util/WebActivityMapper.java',
'util/WindowUtils.java',
]]
gujar.extra_jars = [
CONFIG['ANDROID_SUPPORT_ANNOTATIONS_JAR_LIB'],
CONFIG['ANDROID_SUPPORT_V4_AAR_LIB'],
CONFIG['ANDROID_SUPPORT_V4_AAR_INTERNAL_LIB'],
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestUUIDUtil.java
@@ -0,0 +1,34 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package org.mozilla.gecko.util;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+
+import java.util.UUID;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for uuid utils.
+ */
+@RunWith(TestRunner.class)
+public class TestUUIDUtil {
+ @Test
+ public void testIsUUID() throws Exception {
+ for (int i = 0; i < 3; ++i) {
+ final String uuid = UUID.randomUUID().toString();
+ assertTrue("Generated UUID, " + uuid + ", is a UUID", UUIDUtil.isUUID(uuid));
+ }
+
+ final String uuid = UUID.randomUUID().toString();
+ for (final String str : new String[] { "not-a-uuid", uuid.substring(0, uuid.length() - 1), uuid + "lol" }) {
+ assertFalse("Given UUID, " + str + ", is not a valid UUID", UUIDUtil.isUUID(str));
+ }
+ }
+}