All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ctc.wstx.util.InternCache Maven / Gradle / Ivy

Go to download

Woodstox is a high-performance XML processor that implements Stax (JSR-173) and SAX2 APIs

There is a newer version: 4.4.1
Show newest version
package com.ctc.wstx.util;

import java.util.HashMap;

/**
 * Singleton class that implements "fast intern" functionality, essentially
 * adding a layer that caches Strings that have been previously intern()ed,
 * but that probably shouldn't be added to symbol tables.
 * This is usually used by improving intern()ing of things like namespace
 * URIs.
 *

* Note: that this class extends {@link HashMap} is an implementation * detail -- no code should ever directly call Map methods. */ public final class InternCache extends HashMap { /** * Let's create cache big enough to usually have enough space for * all entries... (assuming NS URIs only) */ private final static int DEFAULT_SIZE = 64; private final static InternCache sInstance = new InternCache(); private InternCache() { /* Let's also try to seriously minimize collisions... since * collisions are likely to be more costly here, with longer * Strings; so let's use 2/3 ratio (67%) instead of default * (75%) */ super(DEFAULT_SIZE, 0.6666f); } public static InternCache getInstance() { return sInstance; } public String intern(String input) { String result; /* Let's split sync block to help in edge cases like * [WSTX-220] */ synchronized (this) { result = (String) get(input); } if (result == null) { result = input.intern(); synchronized (this) { put(result, result); } } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy