Clover coverage report - DNA Site - 1.1
Coverage timestamp: Sun May 2 2004 15:33:21 BST
file stats: LOC: 158   Methods: 7
NCLOC: 78   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DefaultResourceLocator.java 100% 100% 100% 100%
coverage
 1   
 /*
 2   
  * Copyright (C) The DNA Group. All rights reserved.
 3   
  *
 4   
  * This software is published under the terms of the DNA
 5   
  * Software License version 1.1, a copy of which has been included
 6   
  * with this distribution in the LICENSE.txt file.
 7   
  */
 8   
 package org.codehaus.dna.impl;
 9   
 
 10   
 import java.util.HashMap;
 11   
 import java.util.Map;
 12   
 
 13   
 import org.codehaus.dna.MissingResourceException;
 14   
 import org.codehaus.dna.ResourceLocator;
 15   
 
 16   
 /**
 17   
  * ResourceLocator implementation backed by a Map and
 18   
  * optionally delegating to parent ResourceLocators.
 19   
  * The developer should create the DefaultResourceLocator,
 20   
  * associate resources with locator and then invoke
 21   
  * {@link #makeReadOnly()} before passing the Locator to
 22   
  * the client component.
 23   
  *
 24   
  * <p>The implementation will first check for resources
 25   
  * associated with itself and if unable to locate resource
 26   
  * locally it will delegate to parent ResourceLocator.</p>
 27   
  *
 28   
  * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
 29   
  */
 30   
 public class DefaultResourceLocator
 31   
     extends AbstractFreezable
 32   
     implements ResourceLocator
 33   
 {
 34   
     /**
 35   
      * parent locator to look into if unable to
 36   
      * find resource in current locator.
 37   
      */
 38   
     private final ResourceLocator m_parent;
 39   
 
 40   
     /**
 41   
      * Resources registered with locator.
 42   
      */
 43   
     private final Map m_resources = new HashMap();
 44   
 
 45   
     /**
 46   
      * Create a ResourceLocator with no parent.
 47   
      */
 48  28
     public DefaultResourceLocator()
 49   
     {
 50  28
         this( null );
 51   
     }
 52   
 
 53   
     /**
 54   
      * Create a ResourceLocator with specified parent.
 55   
      *
 56   
      * @param parent the parent ResourceLocator
 57   
      */
 58  36
     public DefaultResourceLocator( final ResourceLocator parent )
 59   
     {
 60  36
         m_parent = parent;
 61   
     }
 62   
 
 63   
     /**
 64   
      * Return resource registered with specified key.
 65   
      *
 66   
      * @param key the key
 67   
      * @return the resource
 68   
      * @throws MissingResourceException if unable to locate
 69   
      *         resource with specified key
 70   
      */
 71  24
     public Object lookup( final String key )
 72   
         throws MissingResourceException
 73   
     {
 74  24
         final Object resource = getResourceMap().get( key );
 75  24
         if( null != resource )
 76   
         {
 77  8
             return resource;
 78   
         }
 79   
 
 80  16
         final ResourceLocator parent = getParent();
 81  16
         if( null != parent )
 82   
         {
 83  8
             return parent.lookup( key );
 84   
         }
 85   
         else
 86   
         {
 87  8
             final String message = "Unable to locate resource " + key + ".";
 88  8
             throw new MissingResourceException( message, key );
 89   
         }
 90   
     }
 91   
 
 92   
     /**
 93   
      * Return true if a resource exists with specified key.
 94   
      *
 95   
      * @param key the key
 96   
      * @return true if a resource exists with specified key.
 97   
      */
 98  24
     public boolean contains( final String key )
 99   
     {
 100  24
         final Object resource = getResourceMap().get( key );
 101  24
         if( null != resource )
 102   
         {
 103  8
             return true;
 104   
         }
 105   
 
 106  16
         final ResourceLocator parent = getParent();
 107  16
         if( null != parent )
 108   
         {
 109  8
             return parent.contains( key );
 110   
         }
 111   
         else
 112   
         {
 113  8
             return false;
 114   
         }
 115   
     }
 116   
 
 117   
     /**
 118   
      * Add a resource to resource locator.
 119   
      *
 120   
      * @param key the key used to store resource (Must not be null).
 121   
      * @param resource the resource (Must not be null).
 122   
      */
 123  16
     public void put( final String key,
 124   
                      final Object resource )
 125   
     {
 126  16
         if( null == key )
 127   
         {
 128  4
             throw new NullPointerException( "key" );
 129   
         }
 130  12
         if( null == resource )
 131   
         {
 132  4
             throw new NullPointerException( "resource" );
 133   
         }
 134  8
         checkWriteable();
 135  8
         getResourceMap().put( key, resource );
 136   
     }
 137   
 
 138   
     /**
 139   
      * Return the parent ResourceLocator if any.
 140   
      *
 141   
      * @return the parent ResourceLocator if any.
 142   
      */
 143  32
     protected final ResourceLocator getParent()
 144   
     {
 145  32
         return m_parent;
 146   
     }
 147   
 
 148   
     /**
 149   
      * Return the map used to store resources.
 150   
      *
 151   
      * @return the map used to store resources.
 152   
      */
 153  56
     protected final Map getResourceMap()
 154   
     {
 155  56
         return m_resources;
 156   
     }
 157   
 }
 158