1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.identity;
7 import org.chromium.base.VisibleForTesting;
9 import java.util.HashMap;
13 * Factory for setting and retrieving instances of {@link UniqueIdentificationGenerator}s.
15 * A generator must always be set for a generator type before it is asked for. A generator type
16 * is any string you want to use for your generator. It is typically defined as a public static
17 * field in the generator itself.
19 public final class UniqueIdentificationGeneratorFactory {
20 private static final Object LOCK = new Object();
21 private static final Map<String, UniqueIdentificationGenerator> GENERATOR_MAP =
22 new HashMap<String, UniqueIdentificationGenerator>();
24 private UniqueIdentificationGeneratorFactory() {
28 * Returns a UniqueIdentificationGenerator if it exists, else throws IllegalArgumentException.
30 * @param generatorType the generator type you want
31 * @return a unique ID generator
33 public static UniqueIdentificationGenerator getInstance(String generatorType) {
35 if (!GENERATOR_MAP.containsKey(generatorType)) {
36 throw new IllegalArgumentException("Unknown generator type " + generatorType);
38 return GENERATOR_MAP.get(generatorType);
43 * During startup of the application, and before any calls to
44 * {@link #getInstance(String)} you must add all supported generators
47 * @param generatorType the type of generator this is. Must be a unique string.
48 * @param gen the generator.
49 * @param force if set to true, will override any existing generator for this type. Else
50 * discards calls where a generator exists.
53 public static void registerGenerator(String generatorType, UniqueIdentificationGenerator gen,
56 if (GENERATOR_MAP.containsKey(generatorType) && !force) {
59 GENERATOR_MAP.put(generatorType, gen);
64 public static void clearGeneratorMapForTest() {
66 GENERATOR_MAP.clear();