com.teamscale.commons.utils.StringPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons Show documentation
Show all versions of teamscale-commons Show documentation
Provides common DTOs for Teamscale
package com.teamscale.commons.utils;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Custom string pool which performs consistently faster when interning a lot of
* strings.
*
* @link http://java-performance.info/string-intern-in-java-6-7-8/
*/
public class StringPool {
private static final Map> STRING_POOL = Collections
.synchronizedMap(new WeakHashMap<>(100000));
/**
* Checks whether we already have a string in the StringPool that it equal to
* the given String. If such a String already exists it returns this String
* otherwise
*/
public static String intern(String str) {
WeakReference cached = STRING_POOL.get(str);
if (cached != null) {
String value = cached.get();
if (value != null) {
return value;
}
}
STRING_POOL.put(str, new WeakReference<>(str));
return str;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy