@Test public void uniquePerThread() throws Exception { final AtomicInteger threadIndex = new AtomicInteger(); final Collator[] array = new Collator[NTHREADS]; Thread[] threads = new Thread[NTHREADS]; for (int i = 0; i < NTHREADS; i++) { threads[i] = new Thread(new Runnable() { public void run() { int index = threadIndex.getAndIncrement(); AkCollatorICU icu = (AkCollatorICU) (AkCollatorFactory.getAkCollator("sv_se_ci")); array[index] = icu.collator.get(); } }); } for (int i = 0; i < NTHREADS; i++) { threads[i].start(); } for (int i = 0; i < NTHREADS; i++) { threads[i].join(); } for (int i = 0; i < NTHREADS; i++) { assertNotNull("Null", array[i]); for (int j = 0; j < i; j++) { assertTrue("Not unique", array[i] != array[j]); } } }
public static AkCollator getAkCollator(final String scheme) { if (mode == Mode.DISABLED || scheme == null) { return UCS_BINARY_COLLATOR; } if (scheme.equalsIgnoreCase(UCS_BINARY)) { return mapToBinary(scheme); } SoftReference<AkCollator> ref = collatorMap.get(scheme); if (ref != null) { AkCollator akCollator = ref.get(); if (akCollator != null) { cacheHits++; return akCollator; } } synchronized (collatorMap) { Integer collationId = schemeToIdMap.get(scheme); if (collationId == null) { collationId = collationIdGenerator.incrementAndGet(); } final AkCollator akCollator; try { akCollator = new AkCollatorICU(scheme, collationId); } catch (InvalidCollationSchemeException | UnsupportedCollationException e) { if (mode == Mode.LOOSE) { return mapToBinary(scheme); } throw e; } ref = new SoftReference<>(akCollator); collatorMap.put(scheme, ref); collationIdMap.put(collationId, ref); schemeToIdMap.put(scheme, collationId); return akCollator; } }
AkCollatorFactory { public static AkCollator getAkCollator(final String scheme) { if (mode == Mode.DISABLED || scheme == null) { return UCS_BINARY_COLLATOR; } if (scheme.equalsIgnoreCase(UCS_BINARY)) { return mapToBinary(scheme); } SoftReference<AkCollator> ref = collatorMap.get(scheme); if (ref != null) { AkCollator akCollator = ref.get(); if (akCollator != null) { cacheHits++; return akCollator; } } synchronized (collatorMap) { Integer collationId = schemeToIdMap.get(scheme); if (collationId == null) { collationId = collationIdGenerator.incrementAndGet(); } final AkCollator akCollator; try { akCollator = new AkCollatorICU(scheme, collationId); } catch (InvalidCollationSchemeException | UnsupportedCollationException e) { if (mode == Mode.LOOSE) { return mapToBinary(scheme); } throw e; } ref = new SoftReference<>(akCollator); collatorMap.put(scheme, ref); collationIdMap.put(collationId, ref); schemeToIdMap.put(scheme, collationId); return akCollator; } } }
AkCollatorFactory { public static AkCollator getAkCollator(final String scheme) { if (mode == Mode.DISABLED || scheme == null) { return UCS_BINARY_COLLATOR; } if (scheme.equalsIgnoreCase(UCS_BINARY)) { return mapToBinary(scheme); } SoftReference<AkCollator> ref = collatorMap.get(scheme); if (ref != null) { AkCollator akCollator = ref.get(); if (akCollator != null) { cacheHits++; return akCollator; } } synchronized (collatorMap) { Integer collationId = schemeToIdMap.get(scheme); if (collationId == null) { collationId = collationIdGenerator.incrementAndGet(); } final AkCollator akCollator; try { akCollator = new AkCollatorICU(scheme, collationId); } catch (InvalidCollationSchemeException | UnsupportedCollationException e) { if (mode == Mode.LOOSE) { return mapToBinary(scheme); } throw e; } ref = new SoftReference<>(akCollator); collatorMap.put(scheme, ref); collationIdMap.put(collationId, ref); schemeToIdMap.put(scheme, collationId); return akCollator; } }  }
AkCollatorFactory { public static AkCollator getAkCollator(final String scheme) { if (mode == Mode.DISABLED || scheme == null) { return UCS_BINARY_COLLATOR; } if (scheme.equalsIgnoreCase(UCS_BINARY)) { return mapToBinary(scheme); } SoftReference<AkCollator> ref = collatorMap.get(scheme); if (ref != null) { AkCollator akCollator = ref.get(); if (akCollator != null) { cacheHits++; return akCollator; } } synchronized (collatorMap) { Integer collationId = schemeToIdMap.get(scheme); if (collationId == null) { collationId = collationIdGenerator.incrementAndGet(); } final AkCollator akCollator; try { akCollator = new AkCollatorICU(scheme, collationId); } catch (InvalidCollationSchemeException | UnsupportedCollationException e) { if (mode == Mode.LOOSE) { return mapToBinary(scheme); } throw e; } ref = new SoftReference<>(akCollator); collatorMap.put(scheme, ref); collationIdMap.put(collationId, ref); schemeToIdMap.put(scheme, collationId); return akCollator; } }  static void setCollationMode(String modeString); static void setCollationMode(Mode m); static Mode getCollationMode(); static AkCollator getAkCollator(final String scheme); static AkCollator getAkCollator(final int collatorId); static T getKeyByValue(Map<T, E> map, E value); }
AkCollatorFactory { public static AkCollator getAkCollator(final String scheme) { if (mode == Mode.DISABLED || scheme == null) { return UCS_BINARY_COLLATOR; } if (scheme.equalsIgnoreCase(UCS_BINARY)) { return mapToBinary(scheme); } SoftReference<AkCollator> ref = collatorMap.get(scheme); if (ref != null) { AkCollator akCollator = ref.get(); if (akCollator != null) { cacheHits++; return akCollator; } } synchronized (collatorMap) { Integer collationId = schemeToIdMap.get(scheme); if (collationId == null) { collationId = collationIdGenerator.incrementAndGet(); } final AkCollator akCollator; try { akCollator = new AkCollatorICU(scheme, collationId); } catch (InvalidCollationSchemeException | UnsupportedCollationException e) { if (mode == Mode.LOOSE) { return mapToBinary(scheme); } throw e; } ref = new SoftReference<>(akCollator); collatorMap.put(scheme, ref); collationIdMap.put(collationId, ref); schemeToIdMap.put(scheme, collationId); return akCollator; } }  static void setCollationMode(String modeString); static void setCollationMode(Mode m); static Mode getCollationMode(); static AkCollator getAkCollator(final String scheme); static AkCollator getAkCollator(final int collatorId); static T getKeyByValue(Map<T, E> map, E value); final static int UCS_BINARY_ID; final static String UCS_BINARY; final static AkCollator UCS_BINARY_COLLATOR; }