package org.torch.server.cache; import java.util.Locale; import java.util.concurrent.TimeUnit; import javax.annotation.concurrent.ThreadSafe; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; @ThreadSafe public final class Caches { // TODO: configurable size, time private Caches() {} /** * java.lang.Object -> java.lang.Object#toString * */ private final static LoadingCache<Object, String> objectStrings = Caffeine.newBuilder().expireAfterWrite(9, TimeUnit.HOURS).maximumSize(512).build(Object::toString); /** * java.lang.String -> java.lang.String (in lower case) */ private static final Cache<String, String> lowerCases = Caffeine.newBuilder().expireAfterWrite(9, TimeUnit.HOURS).maximumSize(512).build(); /** * Converts all of the characters in this {@code String} to lower * case using the rules of the default locale. This is equivalent to calling * {@code toLowerCase(Locale.getDefault())}. * <p> * <b>Note:</b> This method is locale sensitive, and may produce unexpected * results if used for strings that are intended to be interpreted locale * independently. * Examples are programming language identifiers, protocol keys, and HTML * tags. * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the * LATIN SMALL LETTER DOTLESS I character. * To obtain correct results for locale insensitive strings, use * {@code toLowerCase(Locale.ROOT)}. * <p> * @return the {@code String}, converted to lowercase. * @see java.lang.String#toLowerCase() */ public static String toLowerCase(String src) { return lowerCases.get(src, String::toLowerCase); } /** * Converts all of the characters in this {@code String} to lower * case using the rules of the given {@code Locale}. Case mapping is based * on the Unicode Standard version specified by the {@link java.lang.Character Character} * class. Since case mappings are not always 1:1 char mappings, the resulting * {@code String} may be a different length than the original {@code String}. * <p> * Examples of lowercase mappings are in the following table: * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description"> * <tr> * <th>Language Code of Locale</th> * <th>Upper Case</th> * <th>Lower Case</th> * <th>Description</th> * </tr> * <tr> * <td>tr (Turkish)</td> * <td>\u0130</td> * <td>\u0069</td> * <td>capital letter I with dot above -> small letter i</td> * </tr> * <tr> * <td>tr (Turkish)</td> * <td>\u0049</td> * <td>\u0131</td> * <td>capital letter I -> small letter dotless i </td> * </tr> * <tr> * <td>(all)</td> * <td>French Fries</td> * <td>french fries</td> * <td>lowercased all chars in String</td> * </tr> * <tr> * <td>(all)</td> * <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi"> * <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil"> * <img src="doc-files/capsigma.gif" alt="capsigma"></td> * <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi"> * <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon"> * <img src="doc-files/sigma1.gif" alt="sigma"></td> * <td>lowercased all chars in String</td> * </tr> * </table> * * @param locale use the case transformation rules for this locale * @return the {@code String}, converted to lowercase. * @see java.lang.String#toLowerCase() * @see java.lang.String#toUpperCase() * @see java.lang.String#toUpperCase(Locale) */ public static String toLowerCase(String src, Locale locale) { String lower = lowerCases.getIfPresent(src); if (lower == null) { lower = src.toLowerCase(locale); lowerCases.put(src, lower); } return lower; } /** * Returns a string representation of the object. In general, the * {@code toString} method returns a string that * "textually represents" this object. The result should * be a concise but informative representation that is easy for a * person to read. * It is recommended that all subclasses override this method. * <p> * The {@code toString} method for class {@code Object} * returns a string consisting of the name of the class of which the * object is an instance, the at-sign character `{@code @}', and * the unsigned hexadecimal representation of the hash code of the * object. In other words, this method returns a string equal to the * value of: * <blockquote> * <pre> * getClass().getName() + '@' + Integer.toHexString(hashCode()) * </pre></blockquote> * * @return a string representation of the object. */ public static String objectString(Object obj) { return objectStrings.get(obj); } }