Java Code Examples for java.lang.ref.SoftReference#get()
The following examples show how to use
java.lang.ref.SoftReference#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnsafeSerializer.java From joyrpc with Apache License 2.0 | 7 votes |
public static UnsafeSerializer create(Class<?> cl) { synchronized (_serializerMap) { SoftReference<UnsafeSerializer> baseRef = _serializerMap.get(cl); UnsafeSerializer base = baseRef != null ? baseRef.get() : null; if (base == null) { if (cl.isAnnotationPresent(HessianUnshared.class)) { base = new UnsafeUnsharedSerializer(cl); } else { base = new UnsafeSerializer(cl); } baseRef = new SoftReference<UnsafeSerializer>(base); _serializerMap.put(cl, baseRef); } return base; } }
Example 2
Source File: JavacMessages.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
public List<ResourceBundle> getBundles(Locale locale) { if (locale == currentLocale && currentBundles != null) return currentBundles; SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale); List<ResourceBundle> bundleList = bundles == null ? null : bundles.get(); if (bundleList == null) { bundleList = List.nil(); for (String bundleName : bundleNames) { try { ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale); bundleList = bundleList.prepend(rb); } catch (MissingResourceException e) { throw new InternalError("Cannot find javac resource bundle for locale " + locale); } } bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList)); } return bundleList; }
Example 3
Source File: ImageCache.java From phoebus with Eclipse Public License 1.0 | 6 votes |
/** Cache an image * * @param key Key for the image * @param provider Will be called to create image if it's not in the cache * @return Image or <code>null</code> */ public static Image cache(final String key, final Supplier<Image> provider) { // Atomically clean expired entry for the key SoftReference<Image> ref = cache.computeIfPresent(key, (k, r) -> { return (r.get() == null) ? null : r; }); // Have existing image? Image img = ref == null ? null : ref.get(); if (img != null) return img; // Add new image // Not atomic; small chance of multiple threads // concurrently adding an image for the same key. // Pity, but map is concurrent, i.e. no crash, // and better than risking blocking/deadlocks. img = provider.get(); if (img != null) cache.put(key, new SoftReference<>(img)); return img; }
Example 4
Source File: CacheMap.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
private void cache(K key) { Iterator<SoftReference<K>> it = cache.iterator(); while (it.hasNext()) { SoftReference<K> sref = it.next(); K key1 = sref.get(); if (key1 == null) it.remove(); else if (key.equals(key1)) { // Move this element to the head of the LRU list it.remove(); cache.add(0, sref); return; } } int size = cache.size(); if (size == nSoftReferences) { if (size == 0) return; // degenerate case, equivalent to WeakHashMap it.remove(); } cache.add(0, new SoftReference<K>(key)); }
Example 5
Source File: PropertyMap.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Check prototype history for an existing property map with specified prototype. * * @param proto New prototype object. * * @return Existing {@link PropertyMap} or {@code null} if not found. */ private PropertyMap checkProtoHistory(final ScriptObject proto) { final PropertyMap cachedMap; if (protoHistory != null) { final SoftReference<PropertyMap> weakMap = protoHistory.get(proto); cachedMap = (weakMap != null ? weakMap.get() : null); } else { cachedMap = null; } if (Context.DEBUG && cachedMap != null) { protoHistoryHit.increment(); } return cachedMap; }
Example 6
Source File: SpCache.java From AndroidBase with Apache License 2.0 | 5 votes |
private Object get(String key, Object defaultVal) { SoftReference reference = mCache.get(key); Object val = null; if (null == reference || null == reference.get()) { val = readDisk(key, defaultVal); mCache.put(key, new SoftReference<Object>(val)); } val = mCache.get(key).get(); return val; }
Example 7
Source File: ContextSerializerFactory.java From joyrpc with Apache License 2.0 | 5 votes |
public static ContextSerializerFactory create(ClassLoader loader) { synchronized (_contextRefMap) { SoftReference<ContextSerializerFactory> factoryRef = _contextRefMap.get(loader); ContextSerializerFactory factory = null; if (factoryRef != null) { factory = factoryRef.get(); } if (factory == null) { ContextSerializerFactory parent = null; if (loader != null) { parent = create(loader.getParent()); } factory = new ContextSerializerFactory(parent, loader); factoryRef = new SoftReference<ContextSerializerFactory>(factory); _contextRefMap.put(loader, factoryRef); } return factory; } }
Example 8
Source File: MethodTypeForm.java From Bytecoder with Apache License 2.0 | 5 votes |
public synchronized LambdaForm setCachedLambdaForm(int which, LambdaForm form) { // Simulate a CAS, to avoid racy duplication of results. SoftReference<LambdaForm> entry = lambdaForms[which]; if (entry != null) { LambdaForm prev = entry.get(); if (prev != null) { return prev; } } lambdaForms[which] = new SoftReference<>(form); return form; }
Example 9
Source File: TagLibParseSupport.java From netbeans with Apache License 2.0 | 5 votes |
/** Returns a cached parse information about the page. * @param successfulOnly if true, and the page has been parsed successfully in the past, returns * the result of this successful parse. Otherwise returns null. * If set to false, never returns null. * @param needCurrent if true, attempts to return the result corresponding to the page exactly at this moment<br> * If both parameters are true, and the page is currently successfully parsable, then returns this result, If it is * unparsable, returns null. * @return the result of parsing this page */ public JspParserAPI.ParseResult getCachedParseResult(boolean successfulOnly, boolean preferCurrent, boolean forceParse) { boolean needToParse = forceParse; if (preferCurrent && isDocumentDirty()) { // need to get an up to date copy needToParse = true; } if (parseResultRef == null) { // no information available needToParse = true; } JspParserAPI.ParseResult ret = null; SoftReference myRef = successfulOnly ? parseResultSuccessfulRef : parseResultRef; if (myRef != null) { ret = (JspParserAPI.ParseResult)myRef.get(); } if ((ret == null) && (!successfulOnly)) { // to comply with the Javadoc regarding not returning null needToParse = true; } if (needToParse) { RequestProcessor.Task t = prepare(); // having the reference is important // so the SoftReference does not get garbage collected t.waitFinished(); myRef = successfulOnly ? parseResultSuccessfulRef : parseResultRef; if (myRef != null) { ret = (JspParserAPI.ParseResult)myRef.get(); } } return ret; }
Example 10
Source File: MethodTypeForm.java From hottub with GNU General Public License v2.0 | 5 votes |
synchronized public MethodHandle setCachedMethodHandle(int which, MethodHandle mh) { // Simulate a CAS, to avoid racy duplication of results. SoftReference<MethodHandle> entry = methodHandles[which]; if (entry != null) { MethodHandle prev = entry.get(); if (prev != null) { return prev; } } methodHandles[which] = new SoftReference<>(mh); return mh; }
Example 11
Source File: ZoneInfoFile.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static byte[] getZoneInfoOldMappings() { byte[] data; SoftReference<byte[]> cache = zoneInfoMappings; if (cache != null) { data = cache.get(); if (data != null) { return data; } } data = readZoneInfoFile(JAVAZM_FILE_NAME); if (data == null) { throw new RuntimeException("ZoneInfoOldMapping " + JAVAZM_FILE_NAME + " either doesn't exist or doesn't have data"); } int index; for (index = 0; index < JAVAZM_LABEL.length; index++) { if (data[index] != JAVAZM_LABEL[index]) { System.err.println("ZoneInfoOld: wrong magic number: " + JAVAZM_FILE_NAME); return null; } } if (data[index++] > JAVAZM_VERSION) { System.err.println("ZoneInfoOld: incompatible version (" + data[index - 1] + "): " + JAVAZM_FILE_NAME); return null; } zoneInfoMappings = new SoftReference<>(data); return data; }
Example 12
Source File: JLaTeXMathCache.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Paint a cached formula * @param o an Object to identify the image in the cache * @param g the graphics where to paint the image * @return the key in the map */ public static Object paintCachedTeXFormula(Object o, Graphics2D g) throws ParseException { if (o == null || !(o instanceof CachedTeXFormula)) { return null; } CachedTeXFormula cached = (CachedTeXFormula) o; SoftReference<CachedImage> img = cache.get(cached); if (img == null || img.get() == null) { img = makeImage(cached); } g.drawImage(img.get().image, identity, null); return cached; }
Example 13
Source File: DateTimeFormatterBuilder.java From Java8CN with Apache License 2.0 | 5 votes |
private String getDisplayName(String id, int type, Locale locale) { if (textStyle == TextStyle.NARROW) { return null; } String[] names; SoftReference<Map<Locale, String[]>> ref = cache.get(id); Map<Locale, String[]> perLocale = null; if (ref == null || (perLocale = ref.get()) == null || (names = perLocale.get(locale)) == null) { names = TimeZoneNameUtility.retrieveDisplayNames(id, locale); if (names == null) { return null; } names = Arrays.copyOfRange(names, 0, 7); names[5] = TimeZoneNameUtility.retrieveGenericDisplayName(id, TimeZone.LONG, locale); if (names[5] == null) { names[5] = names[0]; // use the id } names[6] = TimeZoneNameUtility.retrieveGenericDisplayName(id, TimeZone.SHORT, locale); if (names[6] == null) { names[6] = names[0]; } if (perLocale == null) { perLocale = new ConcurrentHashMap<>(); } perLocale.put(locale, names); cache.put(id, new SoftReference<>(perLocale)); } switch (type) { case STD: return names[textStyle.zoneNameStyleIndex() + 1]; case DST: return names[textStyle.zoneNameStyleIndex() + 3]; } return names[textStyle.zoneNameStyleIndex() + 5]; }
Example 14
Source File: SystemFlavorMap.java From jdk1.8-source-analysis with Apache License 2.0 | 5 votes |
public LinkedHashSet<V> check(K key) { if (cache == null) return null; SoftReference<LinkedHashSet<V>> ref = cache.get(key); if (ref != null) { return ref.get(); } return null; }
Example 15
Source File: TimeZoneNameUtility.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static String[] retrieveDisplayNamesImpl(String id, Locale locale) { LocaleServiceProviderPool pool = LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class); String[] names; Map<Locale, String[]> perLocale = null; SoftReference<Map<Locale, String[]>> ref = cachedDisplayNames.get(id); if (Objects.nonNull(ref)) { perLocale = ref.get(); if (Objects.nonNull(perLocale)) { names = perLocale.get(locale); if (Objects.nonNull(names)) { return names; } } } // build names array names = new String[7]; names[0] = id; for (int i = 1; i <= 6; i ++) { names[i] = pool.getLocalizedObject(TimeZoneNameGetter.INSTANCE, locale, i<5 ? (i<3 ? "std" : "dst") : "generic", i%2, id); } if (Objects.isNull(perLocale)) { perLocale = new ConcurrentHashMap<>(); } perLocale.put(locale, names); ref = new SoftReference<>(perLocale); cachedDisplayNames.put(id, ref); return names; }
Example 16
Source File: ZoneInfoFile.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
static int[] getRawOffsets() { int[] offsets = null; SoftReference<int[]> cache = rawOffsets; if (cache != null) { offsets = cache.get(); if (offsets != null) { return offsets; } } byte[] buf = getZoneInfoOldMappings(); int index = JAVAZM_LABEL_LENGTH + 1; int filesize = buf.length; try { loop: while (index < filesize) { byte tag = buf[index++]; int len = ((buf[index++] & 0xFF) << 8) + (buf[index++] & 0xFF); switch (tag) { case TAG_RawOffsets: { int n = len/4; offsets = new int[n]; for (int i = 0; i < n; i++) { int val = buf[index++] & 0xff; val = (val << 8) + (buf[index++] & 0xff); val = (val << 8) + (buf[index++] & 0xff); val = (val << 8) + (buf[index++] & 0xff); offsets[i] = val; } } break loop; default: index += len; break; } } } catch (ArrayIndexOutOfBoundsException e) { System.err.println("ZoneInfoOld: corrupted " + JAVAZM_FILE_NAME); } rawOffsets = new SoftReference<>(offsets); return offsets; }
Example 17
Source File: MethodTypeForm.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
public LambdaForm cachedLambdaForm(int which) { assert(assertIsBasicType()); SoftReference<LambdaForm> entry = lambdaForms[which]; return (entry != null) ? entry.get() : null; }
Example 18
Source File: MethodTypeForm.java From hottub with GNU General Public License v2.0 | 4 votes |
public MethodHandle cachedMethodHandle(int which) { assert(assertIsBasicType()); SoftReference<MethodHandle> entry = methodHandles[which]; return (entry != null) ? entry.get() : null; }
Example 19
Source File: DateFormatSymbols.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
/** * Initializes this DateFormatSymbols with the locale data. This method uses * a cached DateFormatSymbols instance for the given locale if available. If * there's no cached one, this method creates an uninitialized instance and * populates its fields from the resource bundle for the locale, and caches * the instance. Note: zoneStrings isn't initialized in this method. */ private void initializeData(Locale locale) { SoftReference<DateFormatSymbols> ref = cachedInstances.get(locale); DateFormatSymbols dfs; if (ref == null || (dfs = ref.get()) == null) { if (ref != null) { // Remove the empty SoftReference cachedInstances.remove(locale, ref); } dfs = new DateFormatSymbols(false); // Initialize the fields from the ResourceBundle for locale. LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale); // Avoid any potential recursions if (!(adapter instanceof ResourceBundleBasedAdapter)) { adapter = LocaleProviderAdapter.getResourceBundleBased(); } ResourceBundle resource = ((ResourceBundleBasedAdapter)adapter).getLocaleData().getDateFormatData(locale); dfs.locale = locale; // JRE and CLDR use different keys // JRE: Eras, short.Eras and narrow.Eras // CLDR: long.Eras, Eras and narrow.Eras if (resource.containsKey("Eras")) { dfs.eras = resource.getStringArray("Eras"); } else if (resource.containsKey("long.Eras")) { dfs.eras = resource.getStringArray("long.Eras"); } else if (resource.containsKey("short.Eras")) { dfs.eras = resource.getStringArray("short.Eras"); } dfs.months = resource.getStringArray("MonthNames"); dfs.shortMonths = resource.getStringArray("MonthAbbreviations"); dfs.ampms = resource.getStringArray("AmPmMarkers"); dfs.localPatternChars = resource.getString("DateTimePatternChars"); // Day of week names are stored in a 1-based array. dfs.weekdays = toOneBasedArray(resource.getStringArray("DayNames")); dfs.shortWeekdays = toOneBasedArray(resource.getStringArray("DayAbbreviations")); // Put dfs in the cache ref = new SoftReference<>(dfs); SoftReference<DateFormatSymbols> x = cachedInstances.putIfAbsent(locale, ref); if (x != null) { DateFormatSymbols y = x.get(); if (y == null) { // Replace the empty SoftReference with ref. cachedInstances.replace(locale, x, ref); } else { ref = x; dfs = y; } } // If the bundle's locale isn't the target locale, put another cache // entry for the bundle's locale. Locale bundleLocale = resource.getLocale(); if (!bundleLocale.equals(locale)) { SoftReference<DateFormatSymbols> z = cachedInstances.putIfAbsent(bundleLocale, ref); if (z != null && z.get() == null) { cachedInstances.replace(bundleLocale, z, ref); } } } // Copy the field values from dfs to this instance. copyMembers(dfs, this); }
Example 20
Source File: JLaTeXMathCache.java From FlexibleRichTextView with Apache License 2.0 | 3 votes |
/** * Get a cached formula * * @param f * a formula * @param style * a style like TeXConstants.STYLE_DISPLAY * @param size * the size of font * @param inset * the inset to add on the top, bottom, left and right * @return the key in the map */ public static Object getCachedTeXFormula(String f, int style, int type, int size, int inset, Integer fgcolor) throws ParseException { CachedTeXFormula cached = new CachedTeXFormula(f, style, type, size, inset, fgcolor); SoftReference<CachedImage> img = cache.get(cached); if (img == null || img.get() == null) { img = makeImage(cached); } return cached; }