Package org.fjank.jcache.collection

Source Code of org.fjank.jcache.collection.MapAdapter

/*   Open Source Java Caching Service
*    Copyright (C) 2002 Frank Karlstr�m
*    This library is free software; you can redistribute it and/or
*    modify it under the terms of the GNU Lesser General Public
*    License as published by the Free Software Foundation; either
*    version 2.1 of the License, or (at your option) any later version.
*
*    This library is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*    Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public
*    License along with this library; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*    The author can be contacted by email: fjankk@users.sourceforge.net
*/
package org.fjank.jcache.collection;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.util.jcache.Attributes;
import javax.util.jcache.CacheException;
import javax.util.jcache.CacheFullException;
import javax.util.jcache.CacheMap;
import javax.util.jcache.NotARetrievableObjectException;
import org.fjank.jcache.CacheAccessImpl2;
import org.fjank.jcache.CacheGroup;
import org.fjank.jcache.CacheObject;
import org.fjank.jcache.CacheRegion;

/**A Map based implementation of CacheAccess.
* @author Frank Karlstr�m
*
*/
public class MapAdapter implements CacheMap {
  private final CacheAccessImpl2 acc;
  public MapAdapter(CacheAccessImpl2 originalAccess) {
    this.acc=originalAccess;
  }
    /**Returns the current number of objects (Raw objects and groups)
     * in this region. this method is not recursive across the groups,
     * so any objects under the groups ini the region root is not
     * accounted for.
     *
     * @see java.util.Map#size()
     */
    public int size() {
      return acc.getRegion().getObjectCount();
    }

    public boolean isEmpty() {
        return acc.getRegion().getObjectCount()==0;
    }

    /**Returns true if this map contains a mapping for the specified key.
     * More formally, returns true if and
   * only if this map contains at a mapping for a key k such that
   * (key==null ? k==null : key.equals(k)).
   * (There can be at most one such mapping.)
  * @param key key whose presence in this map is to be tested.
  * @return <code>true</code> if this map contains a mapping for the specified key.
  * @throws NullPointerException if the key is null.
     * @see java.util.Map#containsKey(java.lang.Object)
     */
    public boolean containsKey(Object key) {
        if(key==null) throw new NullPointerException("This Map does not permit null keys.");
        return acc.isPresent(key);
    }

    public boolean containsValue(Object value) {
       CacheRegion region = acc.getRegion();
       return region.containsValue(value);
    }

    /**
     * Returns the value to which this map maps the specified key.  Returns
     * <tt>null</tt> if the map contains no mapping for this key. 
     *
     * <p>More formally, if this map contains a mapping from a key
     * <tt>k</tt> to a value <tt>v</tt> such that <tt>(key.equals(k))</tt>,
     * then this method returns <tt>v</tt>; otherwise
     * it returns <tt>null</tt>.  (There can be at most one such mapping.)
     *
     * @param key key whose associated value is to be returned.
     * @return the value to which this map maps the specified key, or
     *         <tt>null</tt> if the map contains no mapping for this key.
     *
     * @throws ClassCastException if an attempt is mad to retrieve a group.
     * @throws NullPointerException key is <tt>null</tt>.
     * @see java.util.Map#get(java.lang.Object)
     */
    public Object get(Object key) {
      if(key==null) throw new NullPointerException("This Map does not permit null keys.");
        Object object = acc.get(key);
        if(object==null) {
            CacheException ex = acc.getException(true);
            if(ex instanceof NotARetrievableObjectException) {
                throw new ClassCastException(ex.getMessage());
            }
            if(ex!=null) {
                throw new IllegalStateException(ex.getMessage());
            }
            //not an error. the object did not exist.
            return null;
        }
        return object;
    }

    /**
     * @see javax.util.jcache.CacheAccess#put(java.lang.Object, java.lang.Object)
     * @see java.util.Map#put(java.lang.Object, java.lang.Object)
     * @throws IllegalArgumentException if some aspect of this key or value prevents it from being stored in this map.
     */
    public Object put(Object key, Object value) {
        Object prev=null;
      if(acc.isPresent(key)) {
      prev = acc.get(key);
      }
      boolean success = acc.put(key, value);
      if(!success) {
          CacheException ex = acc.getException(true);
          if(ex instanceof CacheFullException) {
              throw new IllegalArgumentException(ex.getMessage());
          }
          if(ex!=null) {
              throw new IllegalStateException(ex.getMessage());
          }
          throw new IllegalStateException("Unknown error.");
      }
        return prev;
    }
    /**
     * @see javax.util.jcache.CacheAccess#put(Object, String, Object)
     * @see CacheMap#put(Object, String, Object)
     * @throws IllegalArgumentException if some aspect of this key, group or value prevents it from being stored in this map.
     */
    public Object put(Object key, String group, Object value) {
       Object prev = null;
      
           if(!acc.isPresent("group")) {
               acc.defineGroup(group);
           }
           if(acc.isPresent(key)) {
               prev=acc.get(key, group, null);
           }
           acc.put(key, group, value);
   
           return prev;
    }
    /**Removes an object from this map. The removed object is returned.
     * if the object does not exist in the cache, null is returned.
     *
     * @param key the object to remove.
     * @return the removed object, or <tt>null</tt> if the object does not exist in FKache.
     * @see java.util.Map#remove(java.lang.Object)
     */
    public Object remove(Object key) {
      if (key==null) {
      throw new NullPointerException("This Map does not permit null keys.");
      }
        CacheObject obj = (CacheObject) acc.getRegion().get(key);
        if(obj==null) {
            return null;
        }
        obj.invalidate();
        return obj.get();
    }
    public Object remove(Object key, String group) {
        if (key==null || group==null) {
      throw new NullPointerException("This Map does not permit null keys.");
      }
        CacheRegion region = acc.getRegion();
        CacheGroup group2 = region.getGroup(group);
        if (group2 == null) {
            return null;
        }
        CacheObject object = (CacheObject) group2.get(key);
        if(object==null) {
            return null;
        }
        object.invalidate();
        return object.get();
    }
    /**Adds all elements in th map into FKache.
     * @param t the map which contains the elements to add.
     * @throws IllegalArgumentException if some aspect of the map prevents this operation to complete.
     * @throws NullPointerException if the map is <tt>null</tt>, a key is <tt>null</tt>, or any value is <tt>null</tt>.
     * @see java.util.Map#putAll(java.util.Map)
     */
    public void putAll(Map t) {
        //naive implementation, but it works. If someone complains, I'll change it.
        for (Iterator iter = t.keySet().iterator(); iter.hasNext();) {
            Object key = iter.next();
            acc.put(key, t.get(key));
        }
    }

    public void clear() {
        acc.invalidate();
    }

    public Set keySet() {
        return acc.getRegion().keySet();
    }

    public Collection values() {
    return acc.getRegion().values();
    }

    public Set entrySet() {
        return acc.getRegion().entrySet();
    }
    /* (non-Javadoc)
     * @see javax.util.jcache.CacheMap#getAttributes()
     */
    public Attributes getAttributes() {
        return acc.getAttributes();
    }
    /* (non-Javadoc)
     * @see javax.util.jcache.CacheMap#getAttributes(java.lang.Object)
     */
    public Attributes getAttributes(Object name) throws CacheException {
        return acc.getAttributes(name);
    }
    /* (non-Javadoc)
     * @see javax.util.jcache.CacheMap#get(java.lang.Object, java.lang.Object)
     */
    public Object get(Object name, Object arguments) {
        return acc.get(name, arguments);
    }
    /* (non-Javadoc)
     * @see javax.util.jcache.CacheMap#defineObject(java.lang.Object, javax.util.jcache.Attributes)
     */
    public void defineObject(Object name, Attributes attributes) {
        acc.defineObject(name, attributes);
    }
}
TOP

Related Classes of org.fjank.jcache.collection.MapAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.