View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: Resolver.java,v 1.3 2005/08/26 18:09:35 mat007 Exp $
29   */
30  
31  package jtge.engine.data;
32  
33  import java.util.Hashtable;
34  
35  /***
36   * Implements a data identifier resolver.
37   * <p>
38   * This class registers data in order to retrieve them later based on their identifier.
39   *
40   * @author Mathieu Champlon
41   * @version $Revision: 1.3 $ $Date: 2005/08/26 18:09:35 $
42   */
43  public class Resolver implements IResolver
44  {
45      private final Hashtable objects;
46  
47      /***
48       * Create a resolver.
49       */
50      public Resolver()
51      {
52          objects = new Hashtable();
53      }
54  
55      /***
56       * Register a new data.
57       * <p>
58       * The data cannot be registered if another data with the same identifier already exists.
59       *
60       * @param data the data to register
61       * @return Whether the data has been registered or not
62       */
63      public final boolean register( final IData data )
64      {
65          if( data == null )
66              return false;
67          if( this.objects.containsKey( data.getIdentifier() ) )
68              return false;
69          this.objects.put( data.getIdentifier(), data );
70          return true;
71      }
72  
73      /***
74       * Unregister a data.
75       * <p>
76       * The data cannot be unregistered if it has not been previously registered.
77       *
78       * @param data the data to unregister
79       * @return Whether the data has been unregistered or not
80       */
81      public final boolean unregister( final IData data )
82      {
83          if( data == null )
84              return false;
85          return this.objects.remove( data.getIdentifier() ) != null;
86      }
87  
88      /***
89       * {@inheritDoc}
90       */
91      public final IData resolve( final String identifier )
92      {
93          return (IData)objects.get( identifier );
94      }
95  }