Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2361)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/identity/UniqueIdentificationGeneratorFactory.java

Issue 12313075: [sync] Upstream the Android ProfileSyncService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/identity/UniqueIdentificationGeneratorFactory.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/identity/UniqueIdentificationGeneratorFactory.java b/chrome/android/java/src/org/chromium/chrome/browser/identity/UniqueIdentificationGeneratorFactory.java
new file mode 100644
index 0000000000000000000000000000000000000000..e6cc4c03c09bf25a9c741f3df28c5a8d5b1bdfe9
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/identity/UniqueIdentificationGeneratorFactory.java
@@ -0,0 +1,69 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.identity;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Factory for setting and retrieving instances of {@link UniqueIdentificationGenerator}s.
+ * <p/>
+ * A generator must always be set for a generator type before it is asked for. A generator type
+ * is any string you want to use for your generator. It is typically defined as a public static
+ * field in the generator itself.
+ */
+public final class UniqueIdentificationGeneratorFactory {
+ private static final Object LOCK = new Object();
+ private static final Map<String, UniqueIdentificationGenerator> GENERATOR_MAP =
+ new HashMap<String, UniqueIdentificationGenerator>();
+
+ private UniqueIdentificationGeneratorFactory() {
+ }
+
+ /**
+ * Returns a UniqueIdentificationGenerator if it exists, else throws IllegalArgumentException.
+ *
+ * @param generatorType the generator type you want
+ * @return a unique ID generator
+ */
+ public static UniqueIdentificationGenerator getInstance(String generatorType) {
+ synchronized (LOCK) {
+ if (!GENERATOR_MAP.containsKey(generatorType)) {
+ throw new IllegalArgumentException("Unknown generator type " + generatorType);
+ }
+ return GENERATOR_MAP.get(generatorType);
+ }
+ }
+
+ /**
+ * During startup of the application, and before any calls to
+ * {@link #getInstance(String)} you must add all supported generators
+ * to this factory.
+ *
+ * @param generatorType the type of generator this is. Must be a unique string.
+ * @param gen the generator.
+ * @param force if set to true, will override any existing generator for this type. Else
+ * discards calls where a generator exists.
+ */
+ @VisibleForTesting
+ public static void registerGenerator(String generatorType, UniqueIdentificationGenerator gen,
+ boolean force) {
+ synchronized (LOCK) {
+ if (GENERATOR_MAP.containsKey(generatorType) && !force) {
+ return;
+ }
+ GENERATOR_MAP.put(generatorType, gen);
+ }
+ }
+
+ @VisibleForTesting
+ public static void clearGeneratorMapForTest() {
+ synchronized (LOCK) {
+ GENERATOR_MAP.clear();
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698