Coverage Report - org.melati.poem.util.CachedIndexFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
CachedIndexFactory
95%
21/22
100%
10/10
3
 
 1  
 /*
 2  
  * $Source$
 3  
  * $Revision$
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     William Chesters <williamc At paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 
 46  
 package org.melati.poem.util;
 47  
 
 48  
 import java.util.Vector;
 49  
 
 50  
 /**
 51  
  * Supply and cache objects identified by a numeric index.
 52  
  * <p>
 53  
  * Subtypes define how the object for a given index is obtained,
 54  
  * and hence the mapping of indexes to objects used in the
 55  
  * caller.
 56  
  * <p>
 57  
  * The name is a touch misleading - the objects returned are
 58  
  * not (necessarily) indexes.
 59  
  * <p>
 60  
  * <code>null</code> object references can be cached and returned.
 61  
  * <p>
 62  
  * Individual elements can be removed from the cache or all elements 
 63  
  * may be removed.
 64  
  */
 65  
 
 66  1263
 public abstract class CachedIndexFactory implements IndexFactory {
 67  
   
 68  1263
   private Vector<Object> cache = new Vector<Object>();
 69  1
   private static final Object nullFromFactory = new Object();
 70  
 
 71  
   /**
 72  
    * @param index of the item, possibly a troid
 73  
    * @return the cached Object
 74  
    */
 75  
   protected abstract Object reallyGet(int index);
 76  
 
 77  
   /**
 78  
    * Get either from cache or, failing that, really get it.
 79  
    * {@inheritDoc}
 80  
    * @see org.melati.poem.util.IndexFactory#get(int)
 81  
    */
 82  
   public Object get(int index) {
 83  11662
     synchronized (cache) {
 84  11662
       if (cache.size() <= index) {
 85  686
         cache.setSize(index + 1);
 86  686
         Object it = reallyGet(index);
 87  686
         cache.setElementAt(it == null ? nullFromFactory : it, index);
 88  686
         return it;
 89  
       }
 90  
       else {
 91  10976
         Object it = cache.elementAt(index);
 92  10976
         if (it == null) {
 93  2
           it = reallyGet(index);
 94  2
           cache.setElementAt(it == null ? nullFromFactory : it, index);
 95  2
           return it;
 96  
         }
 97  10974
         else if (it == nullFromFactory)
 98  1
           return null;
 99  
         else
 100  10973
           return it;
 101  
       }
 102  0
     }
 103  
   }
 104  
 
 105  
   /**
 106  
    * Invalidate an entry in the cache.
 107  
    * @param index the entry's index to invalidate
 108  
    */
 109  
   public void invalidate(int index) {
 110  1
     cache.setElementAt(null, index);
 111  1
   }
 112  
 
 113  
   /**
 114  
    * Invalidate whole cache.
 115  
    */
 116  
   public void invalidate() {
 117  103
     cache.removeAllElements();
 118  103
   }
 119  
 }
 120  
 
 121  
 
 122  
 
 123