List of usage examples for org.apache.commons.collections.map AbstractReferenceMap WEAK
int WEAK
To view the source code for org.apache.commons.collections.map AbstractReferenceMap WEAK.
Click Source Link
From source file:com.google.gwt.dev.shell.designtime.WrappersCache.java
/** * Weakly caches a given JSO by unique id. A cached JSO can be looked up by unique id until it is garbage * collected.//from w w w . j av a 2s .c om * * Instantiations: changed to long. * * @param uniqueId * a unique id associated with the JSO * @param jso * the value to cache */ @SuppressWarnings("unchecked") public static void putCachedJso(ClassLoader cl, long uniqueId, Object jso) { Map<Long, Object> cache = m_jsoCache.get(cl); if (cache == null) { cache = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK); m_jsoCache.put(cl, cache); } cache.put(uniqueId, jso); }
From source file:com.google.gwt.dev.shell.designtime.WrappersCache.java
/** * Weakly caches a wrapper for a given Java Object. * // w w w .j ava 2 s .c om * @param javaObject * the Object being wrapped * @param wrapper * the mapped wrapper */ @SuppressWarnings("unchecked") public static void putWrapperForObject(ClassLoader cl, Object javaObject, Object wrapper) { Map<Object, Object> cache = m_javaWrapperCache.get(cl); if (cache == null) { cache = new ReferenceIdentityMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK); m_javaWrapperCache.put(cl, cache); } cache.put(javaObject, wrapper); }
From source file:com.google.gwt.dev.javac.TypeOracleMediatorTestBase.java
/** * Tests which variant of AbstractRefrenceMap we want to store the map for * parameterizedTypes, arrayTypes, and wildCardTypes in TypeOracle. Note: this * test is manual because gc can be unreliable. *//*from w w w .j a v a 2 s . co m*/ @SuppressWarnings("unchecked") public void manualTestAbstractRefrenceMap() { /* * with a HARD -> WEAK map, verify that the entry remains if there is no * reference to key, but is deleted when the reference to value is gone */ Map<Integer, Integer> simpleMap = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK, true); Integer bar = new Integer(42); simpleMap.put(new Integer(32), bar); Runtime.getRuntime().gc(); assertEquals(1, simpleMap.size()); bar = null; Runtime.getRuntime().gc(); assertEquals(0, simpleMap.size()); /* * with a WEAK -> WEAK map, verify that the entry is gone if there are no * references to either the key or the value. */ simpleMap = new ReferenceMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK, true); Map<Integer, Integer> reverseMap = new ReferenceMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK, true); Integer foo = new Integer(32); bar = new Integer(42); simpleMap.put(foo, bar); reverseMap.put(bar, foo); Runtime.getRuntime().gc(); assertEquals(1, simpleMap.size()); assertEquals(1, reverseMap.size()); bar = null; Runtime.getRuntime().gc(); assertEquals(0, simpleMap.size()); assertEquals(0, reverseMap.size()); }
From source file:org.apache.cayenne.access.DefaultObjectMapRetainStrategy.java
@SuppressWarnings("unchecked") public Map<Object, Persistent> createObjectMap() { String strategy = runtimeProperties.get(Constants.SERVER_OBJECT_RETAIN_STRATEGY_PROPERTY); if (strategy == null || WEAK_RETAIN_STRATEGY.equals(strategy)) { return new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK); } else if (SOFT_RETAIN_STRATEGY.equals(strategy)) { return new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT); } else if (HARD_RETAIN_STRATEGY.equals(strategy)) { return new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.HARD); } else {/*from w w w . ja va2 s . co m*/ throw new CayenneRuntimeException("Unsupported retain strategy %s", strategy); } }
From source file:org.apache.cayenne.access.ObjectStore.java
/** * Factory method to create default Map for storing registered objects. * // w ww. j a v a 2s .c om * @since 3.0 * @return a map with hard referenced keys and weak referenced values. */ static Map<Object, Persistent> createObjectMap() { return new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK); }
From source file:org.apache.myfaces.ext202patch.application.viewstate.SerializedViewCollection.java
/** * @return old serialized views map//from ww w. j a v a 2 s. c om */ @SuppressWarnings("unchecked") protected Map<Object, Object> getOldSerializedViewsMap() { FacesContext context = FacesContext.getCurrentInstance(); if (_oldSerializedViews == null && context != null) { String cacheMode = getCacheOldViewsInSessionMode(context); if (ServerSideStateCacheImpl.CACHE_OLD_VIEWS_IN_SESSION_MODE_WEAK.equals(cacheMode)) { _oldSerializedViews = new ReferenceMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK, true); } else if (ServerSideStateCacheImpl.CACHE_OLD_VIEWS_IN_SESSION_MODE_SOFT_WEAK.equals(cacheMode)) { _oldSerializedViews = new ReferenceMap(AbstractReferenceMap.SOFT, AbstractReferenceMap.WEAK, true); } else if (ServerSideStateCacheImpl.CACHE_OLD_VIEWS_IN_SESSION_MODE_SOFT.equals(cacheMode)) { _oldSerializedViews = new ReferenceMap(AbstractReferenceMap.SOFT, AbstractReferenceMap.SOFT, true); } else if (ServerSideStateCacheImpl.CACHE_OLD_VIEWS_IN_SESSION_MODE_HARD_SOFT.equals(cacheMode)) { _oldSerializedViews = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT); } } return _oldSerializedViews; }
From source file:org.gradle.api.internal.AbstractClassGenerator.java
private <T> Class<? extends T> generateUnderLock(Class<T> type) { Map<Class<?>, Class<?>> cache = GENERATED_CLASSES.get(getClass()); if (cache == null) { // WeakHashMap won't work here. It keeps a strong reference to the mapping value, which is the generated class in this case // However, the generated class has a strong reference to the source class (by extending it), so the keys will always be // strongly reachable while this Class is strongly reachable. Use weak references for both key and value of the mapping instead. cache = new ReferenceMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK); GENERATED_CLASSES.put(getClass(), cache); }//from w w w . java 2s .c o m Class<?> generatedClass = cache.get(type); if (generatedClass != null) { return generatedClass.asSubclass(type); } if (Modifier.isPrivate(type.getModifiers())) { throw new GradleException( String.format("Cannot create a proxy class for private class '%s'.", type.getSimpleName())); } if (Modifier.isAbstract(type.getModifiers())) { throw new GradleException( String.format("Cannot create a proxy class for abstract class '%s'.", type.getSimpleName())); } Class<? extends T> subclass; try { ClassMetaData classMetaData = inspectType(type); ClassBuilder<T> builder = start(type, classMetaData); builder.startClass(); if (!DynamicObjectAware.class.isAssignableFrom(type)) { if (ExtensionAware.class.isAssignableFrom(type)) { throw new UnsupportedOperationException( "A type that implements ExtensionAware must currently also implement DynamicObjectAware."); } builder.mixInDynamicAware(); } if (!GroovyObject.class.isAssignableFrom(type)) { builder.mixInGroovyObject(); } builder.addDynamicMethods(); if (classMetaData.conventionAware && !IConventionAware.class.isAssignableFrom(type)) { builder.mixInConventionAware(); } Class noMappingClass = Object.class; for (Class<?> c = type; c != null && noMappingClass == Object.class; c = c.getSuperclass()) { if (c.getAnnotation(NoConventionMapping.class) != null) { noMappingClass = c; } } Set<PropertyMetaData> conventionProperties = new HashSet<PropertyMetaData>(); for (PropertyMetaData property : classMetaData.properties.values()) { if (SKIP_PROPERTIES.contains(property.name)) { continue; } if (property.injector) { builder.addInjectorProperty(property); for (Method getter : property.getters) { builder.applyServiceInjectionToGetter(property, getter); } for (Method setter : property.setters) { builder.applyServiceInjectionToSetter(property, setter); } continue; } boolean needsConventionMapping = false; if (classMetaData.isExtensible()) { for (Method getter : property.getters) { if (!Modifier.isFinal(getter.getModifiers()) && !getter.getDeclaringClass().isAssignableFrom(noMappingClass)) { needsConventionMapping = true; break; } } } if (needsConventionMapping) { conventionProperties.add(property); builder.addConventionProperty(property); for (Method getter : property.getters) { builder.applyConventionMappingToGetter(property, getter); } } if (needsConventionMapping) { for (Method setter : property.setters) { if (!Modifier.isFinal(setter.getModifiers())) { builder.applyConventionMappingToSetter(property, setter); } } } } Set<Method> actionMethods = classMetaData.missingOverloads; for (Method method : actionMethods) { builder.addActionMethod(method); } // Adds a set method for each mutable property for (PropertyMetaData property : classMetaData.properties.values()) { if (property.setters.isEmpty()) { continue; } if (Iterable.class.isAssignableFrom(property.getType())) { // Currently not supported continue; } if (property.setMethods.isEmpty()) { for (Method setter : property.setters) { builder.addSetMethod(property, setter); } } else if (conventionProperties.contains(property)) { for (Method setMethod : property.setMethods) { builder.applyConventionMappingToSetMethod(property, setMethod); } } } for (Constructor<?> constructor : type.getConstructors()) { if (Modifier.isPublic(constructor.getModifiers())) { builder.addConstructor(constructor); } } subclass = builder.generate(); } catch (Throwable e) { throw new GradleException( String.format("Could not generate a proxy class for class %s.", type.getName()), e); } cache.put(type, subclass); cache.put(subclass, subclass); return subclass; }
From source file:org.hibernate.engine.internal.StatefulPersistenceContext.java
/** * Constructs a PersistentContext, bound to the given session. * * @param session The session "owning" this context. *///from w w w. jav a 2s. c o m public StatefulPersistenceContext(SessionImplementor session) { this.session = session; entitiesByKey = new HashMap(INIT_COLL_SIZE); entitiesByUniqueKey = new HashMap(INIT_COLL_SIZE); proxiesByKey = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK); entitySnapshotsByKey = new HashMap(INIT_COLL_SIZE); entityEntries = IdentityMap.instantiateSequenced(INIT_COLL_SIZE); collectionEntries = IdentityMap.instantiateSequenced(INIT_COLL_SIZE); collectionsByKey = new HashMap(INIT_COLL_SIZE); arrayHolders = IdentityMap.instantiate(INIT_COLL_SIZE); parentsByChild = IdentityMap.instantiateSequenced(INIT_COLL_SIZE); nullifiableEntityKeys = new HashSet(); initTransientState(); }
From source file:org.hibernate.engine.internal.StatefulPersistenceContext.java
public static StatefulPersistenceContext deserialize(ObjectInputStream ois, SessionImplementor session) throws IOException, ClassNotFoundException { LOG.trace("Serializing persistent-context"); StatefulPersistenceContext rtn = new StatefulPersistenceContext(session); // during deserialization, we need to reconnect all proxies and // collections to this session, as well as the EntityEntry and // CollectionEntry instances; these associations are transient // because serialization is used for different things. try {/*from ww w . ja v a2 s . c om*/ rtn.defaultReadOnly = ois.readBoolean(); // todo : we can actually just determine this from the incoming EntityEntry-s rtn.hasNonReadOnlyEntities = ois.readBoolean(); int count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] entitiesByKey entries"); rtn.entitiesByKey = new HashMap(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count); for (int i = 0; i < count; i++) { rtn.entitiesByKey.put(EntityKey.deserialize(ois, session), ois.readObject()); } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] entitiesByUniqueKey entries"); rtn.entitiesByUniqueKey = new HashMap(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count); for (int i = 0; i < count; i++) { rtn.entitiesByUniqueKey.put(EntityUniqueKey.deserialize(ois, session), ois.readObject()); } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] proxiesByKey entries"); rtn.proxiesByKey = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK, count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count, .75f); for (int i = 0; i < count; i++) { EntityKey ek = EntityKey.deserialize(ois, session); Object proxy = ois.readObject(); if (proxy instanceof HibernateProxy) { ((HibernateProxy) proxy).getHibernateLazyInitializer().setSession(session); rtn.proxiesByKey.put(ek, proxy); } else LOG.trace("Encountered prunded proxy"); // otherwise, the proxy was pruned during the serialization process } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] entitySnapshotsByKey entries"); rtn.entitySnapshotsByKey = new HashMap(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count); for (int i = 0; i < count; i++) { rtn.entitySnapshotsByKey.put(EntityKey.deserialize(ois, session), ois.readObject()); } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] entityEntries entries"); rtn.entityEntries = IdentityMap.instantiateSequenced(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count); for (int i = 0; i < count; i++) { Object entity = ois.readObject(); EntityEntry entry = EntityEntry.deserialize(ois, session); rtn.entityEntries.put(entity, entry); } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] collectionsByKey entries"); rtn.collectionsByKey = new HashMap(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count); for (int i = 0; i < count; i++) { rtn.collectionsByKey.put(CollectionKey.deserialize(ois, session), ois.readObject()); } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] collectionEntries entries"); rtn.collectionEntries = IdentityMap .instantiateSequenced(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count); for (int i = 0; i < count; i++) { final PersistentCollection pc = (PersistentCollection) ois.readObject(); final CollectionEntry ce = CollectionEntry.deserialize(ois, session); pc.setCurrentSession(session); rtn.collectionEntries.put(pc, ce); } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] arrayHolders entries"); rtn.arrayHolders = IdentityMap.instantiate(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count); for (int i = 0; i < count; i++) { rtn.arrayHolders.put(ois.readObject(), ois.readObject()); } count = ois.readInt(); LOG.trace("Starting deserialization of [" + count + "] nullifiableEntityKey entries"); rtn.nullifiableEntityKeys = new HashSet(); for (int i = 0; i < count; i++) { rtn.nullifiableEntityKeys.add(EntityKey.deserialize(ois, session)); } } catch (HibernateException he) { throw new InvalidObjectException(he.getMessage()); } return rtn; }
From source file:org.nuxeo.ecm.core.storage.sql.PersistenceContext.java
@SuppressWarnings("unchecked") public PersistenceContext(Model model, RowMapper mapper, SessionImpl session) throws StorageException { this.model = model; this.mapper = mapper; this.session = session; hierComplex = new SelectionContext(SelectionType.CHILDREN, Boolean.TRUE, mapper, this); hierNonComplex = new SelectionContext(SelectionType.CHILDREN, Boolean.FALSE, mapper, this); seriesVersions = new SelectionContext(SelectionType.SERIES_VERSIONS, null, mapper, this); selections = new ArrayList<SelectionContext>(Arrays.asList(hierComplex, hierNonComplex, seriesVersions)); if (model.proxiesEnabled) { seriesProxies = new SelectionContext(SelectionType.SERIES_PROXIES, null, mapper, this); targetProxies = new SelectionContext(SelectionType.TARGET_PROXIES, null, mapper, this); selections.add(seriesProxies);/*from w w w . j ava 2 s .c om*/ selections.add(targetProxies); } else { seriesProxies = null; targetProxies = null; } // use a weak reference for the values, we don't hold them longer than // they need to be referenced, as the underlying mapper also has its own // cache pristine = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK); modified = new HashMap<RowId, Fragment>(); // this has to be linked to keep creation order, as foreign keys // are used and need this createdIds = new LinkedHashSet<Serializable>(); cacheCount = registry.counter( MetricRegistry.name("nuxeo", "repositories", session.getRepositoryName(), "caches", "count")); cacheHitCount = registry.counter( MetricRegistry.name("nuxeo", "repositories", session.getRepositoryName(), "caches", "hit")); }