Coverage Report - org.apache.commons.cache.GroupMapImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupMapImpl
0%
0/75
0%
0/22
2.5
 
 1  
 /*
 2  
  * Copyright 2001-2004 The Apache Software Foundation
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.cache;
 17  
 
 18  
 import java.io.Serializable;
 19  
 import java.util.HashMap;
 20  
 import java.util.HashSet;
 21  
 
 22  
 /**
 23  
  * tk.
 24  
  * @version $Id: GroupMapImpl.java 155435 2005-02-26 13:17:27Z dirkv $
 25  
  * @author Rodney Waldhoff
 26  
  */
 27  0
 public class GroupMapImpl extends BaseStorageListener implements GroupMap, StorageListener {
 28  
   /** The {@link Cache} I am associated with. */
 29  0
   protected Cache _cache = null;
 30  
   /** A map from groups to a set of associated keys. */
 31  0
   protected HashMap _groupToKeys = new HashMap();
 32  
   /** A map from keys to their groups. */
 33  0
   protected HashMap _keyToGroup = new HashMap();
 34  
 
 35  
   /** Returns an array of key values associated with the given group. */
 36  
   public synchronized Serializable[] getKeysForGroup(Serializable group) {
 37  
     try {
 38  0
       HashSet set = (HashSet)(_groupToKeys.get(group));
 39  0
       return (Serializable[])(set.toArray(new Serializable[0]));
 40  0
     } catch(Exception e) {
 41  0
       return new Serializable[0];
 42  
     }
 43  
   }
 44  
 
 45  
   /** Sets my cache. */
 46  
   public void setCache(Cache c) {
 47  0
     if(null != _cache) {
 48  0
       Object mutex = _cache;
 49  0
       synchronized(mutex) {
 50  0
         synchronized(c) {
 51  0
           synchronized(this) {
 52  0
             unsetCache();
 53  0
             _cache = c;
 54  0
             _cache.registerStorageListener(this);
 55  0
           }
 56  0
         }
 57  0
       }
 58  0
     } else {
 59  0
       synchronized(c) {
 60  0
         synchronized(this) {
 61  0
           _cache = c;
 62  0
           _cache.registerStorageListener(this);
 63  0
         }
 64  0
       }
 65  
     }
 66  0
   }
 67  
 
 68  
   /** Unsets my cache. */
 69  
   public void unsetCache() {
 70  0
     if(null != _cache) {
 71  0
       Object mutex = _cache;
 72  0
       synchronized(mutex) {
 73  0
         synchronized(this) {
 74  0
           _cache.unregisterStorageListener(this);
 75  0
           clear();
 76  0
           _cache = null;
 77  0
         }
 78  0
       }
 79  
     }
 80  0
   }
 81  
 
 82  
   public synchronized void stored(Serializable key, Serializable val, Long expiresAt, Long cost, Serializable group) {
 83  0
     if(null == group) {
 84  0
       Object oldgroup = _keyToGroup.get(key);
 85  0
       if(null != oldgroup) {
 86  0
         HashSet oldset = (HashSet)(_groupToKeys.get(oldgroup));
 87  0
         if(null != oldset) {
 88  0
           oldset.remove(key);
 89  
         }
 90  
       }
 91  0
     } else {
 92  0
       HashSet set = (HashSet)(_groupToKeys.get(group));
 93  0
       if(null != set) {
 94  0
         set.add(key);
 95  
       } else {
 96  0
         set = new HashSet();
 97  0
         set.add(key);
 98  0
         _groupToKeys.put(group,set);
 99  
       }
 100  0
       Object oldgroup = _keyToGroup.put(key,group);
 101  0
       if(null != oldgroup && !(oldgroup.equals(group))) {
 102  0
         HashSet oldset = (HashSet)(_groupToKeys.get(oldgroup));
 103  0
         if(null != oldset) {
 104  0
           oldset.remove(key);
 105  
         }
 106  
       }
 107  
     }
 108  0
   }
 109  
 
 110  
   public synchronized void cleared(Serializable key) {
 111  0
     Serializable group = (Serializable)(_keyToGroup.remove(key));
 112  0
     if(null != group) {
 113  0
       HashSet set = (HashSet)(_groupToKeys.get(group));
 114  0
       set.remove(key);
 115  
     }
 116  0
   }
 117  
 
 118  
   public void cleared() {
 119  0
     clear();
 120  0
   }
 121  
 
 122  
   protected synchronized void clear() {
 123  0
     _groupToKeys.clear();
 124  0
     _keyToGroup.clear();
 125  0
   }
 126  
 
 127  
   public static void main(String[] args) {
 128  0
     GroupMapImpl tm = new GroupMapImpl();
 129  0
     tm.stored("key1","val",null,null,"group1");
 130  0
     tm.stored("key2","val",null,null,"group1");
 131  0
     tm.stored("key3","val",null,null,"group1");
 132  0
     tm.stored("key4","val",null,null,"group1");
 133  0
     Serializable[] keys = tm.getKeysForGroup("group1");
 134  0
     for(int i=0;i<keys.length;i++) {
 135  0
       System.out.println(keys[i]);
 136  
     }
 137  0
   }
 138  
 }