EntityBeanMap

 package org.jboss.seam.contexts;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Swizzles entities held in the conversation context at
 * the end of each request.
 * 
 * @see PassivatedEntity
 * 
 * @author Gavin King
 *
 */
class EntityBeanMap extends AbstractEntityBeanCollection
{
   private static final long serialVersionUID = -2884601453783925804L;
   
   private Map map; //所有保存的对象,激活的对象
   private Map<Object, PassivatedEntity> passivatedEntityMap;//对象key保存,被钝化的对象
   
   public EntityBeanMap(Map instance)
   {
      this.map = instance;
   }
   
   @Override
   protected Iterable<PassivatedEntity> getPassivatedEntities() 
   {
      return passivatedEntityMap.values();
   }
   
   @Override
   protected Object getEntityCollection()
   {
      return map;
   }
   
   @Override
   protected void clearPassivatedEntities()
   {
      passivatedEntityMap = null;
   }

   @Override
   protected boolean isPassivatedEntitiesInitialized()
   {
      return passivatedEntityMap!=null;
   }

   @Override
   protected void activateAll()
   {
      for ( Map.Entry<Object, PassivatedEntity> me: passivatedEntityMap.entrySet() )
      {
         map.put( me.getKey(), me.getValue().toEntityReference(true) );
      }
      clearPassivatedEntities(); //对象激活了 set钝化对象为null
   }
   
   @Override
   protected void passivateAll()
   {
      HashMap<Object, PassivatedEntity> newPassivatedMap = 
          new HashMap<Object, PassivatedEntity>(map.size());
      boolean found = false;
      for (Map.Entry me: (Set<Map.Entry>) map.entrySet()) {
         Object value = me.getValue();
         if (value!=null) {
            PassivatedEntity passivatedEntity = PassivatedEntity.passivateEntity(value);
            if (passivatedEntity!=null) {
               if (!found) {
                   map = new HashMap(map);
                   found=true;
               }

               //this would be dangerous, except that we 
               //are doing it to a copy of the original 
               //list:
               map.remove(me.getKey()); //没钝化一个则删除激活MAP一个
               newPassivatedMap.put(me.getKey(), passivatedEntity);
            }
         }
      }
      if (found) {
          passivatedEntityMap = newPassivatedMap;
      }
   }
   
}


你可能感兴趣的:(java,jboss,seam)