Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/miamm/soapmmil/engine/MapToMany.java


1   /* -*- Mode: java; indent-tabs-mode:nil; c-basic-offset: 2 -*-
2    * ex: set sw=2 expandtab:
3    * $Id: MapToMany.java,v 1.1 2003/04/19 08:24:39 kowey Exp $
4    *
5    * This software is released under a BSD-style license.
6    * Please see the LICENSE file in this distribution.
7    */
8   
9   package org.miamm.soapmmil.engine;
10  
11  import java.util.Collection;
12  import java.util.Map;
13  
14  import org.apache.log4j.Logger;
15  
16  /**
17   * An object that maps keys to values.  A MapToMany cannot contain
18   * duplicate keys;  BUT unlike Maps, each key can map to any number of 
19   * values. 
20   *
21   * <p>
22   * FIXME:  if i really gave a damn, i would make this an interface,
23   * and create an abstract class called "MapToCollection" which
24   * implements this interface
25   * </p>
26   *
27   * @version
28   * $Revision: 1.1 $<br>
29   * $Date: 2003/04/19 08:24:39 $<br>
30   * @author Eric Kow (kow at loria point fr)
31   */
32  public abstract class MapToMany {
33  
34    // ---------------------------------------------------------------
35    // public methods
36    // ---------------------------------------------------------------
37    
38    /**
39     * Associates the specified value with the specified key in this map
40     * (optional operation).  If the map previously contained a mapping
41     * for  this key, the old value is replaced by the specified value.
42     * (A map mis said to contain a mapping for a key kif and only  if
43     * m.containsKey(k) would return true .
44     *
45     * @param key key with which the specified value is to be associated. 
46     * @param value value to be associated with the specified key. 
47     *
48     * @throws UnsupportedOperationException if the put operation is
49     *         not supported by this MapToMany. 
50     * @throws ClassCastException if the class of the specified key or
51     *         value prevents it from being stored in this MapToMany. 
52     * @throws IllegalArgumentException if some aspect of this key or
53     *         value prevents it from being stored in this MapToMany.
54     * @throws NullPointerException this MapToMany does not permit null
55     *         keys or values, and the specified key or value is null .
56     */
57    public void put(Object key, Object value) {
58      _logger.debug("Enter: MapToMany put " + key + " to " + value);
59      if (_map.containsKey(key)) {
60        Collection valueCollection = (Collection)(_map.get(key));
61        valueCollection.add(value);
62      } else { // ! _table.containsKey(key)
63        Collection valueCollection = createCollection();
64        valueCollection.add(value);
65        _map.put(key, valueCollection);
66      }
67     _logger.debug("Exit: MapToMany (after add: " + this + ")");
68    }
69  
70  
71    // FIXME: oh the errors this should throw
72    public void remove(Object key, Object value) {
73      _logger.debug("Enter: " + UCLASSNAME + " remove " + value +
74                    " from " + key);
75      // best thing to do is just to discard the collection if it is empty 
76      if (_map.containsKey(key)) {
77        Collection valueCollection = (Collection)(_map.get(key));
78        valueCollection.remove(value);
79      } 
80      _logger.debug("Exit: " + UCLASSNAME + " remove " + value +
81                    " from " + key);
82    }
83  
84    /**
85     * Returns the values to which this MapToMany maps the specified key.
86     *
87     * <b>Important difference</b>
88     * Returns null if and only if the MapToMany contains no mapping for
89     * this key.
90     *
91     * @param key key whose associated value is to be returned. 
92     */
93    public Collection get(Object key) {
94      return (Collection)(_map.get(key));
95    } 
96    
97    /**
98     * Returns <code>true</code> if this map contains a mapping for the
99     * specified  key.  More formally, returns true if and only if this
100    * map contains at a mapping for a key k such that <code>(key==null ?
101    * k==null : key.equals(k))</code>
102    *
103    * @param key key whose presence in this map is to be tested. 
104    * @return <code>true</code> if this map contains a mapping for the
105    *         specified key.  
106    */
107   public boolean containsKey(Object key) {
108     if (_map.containsKey(key)) {
109       Collection valueCollection = (Collection)(_map.get(key));
110       return (! valueCollection.isEmpty());
111     } else return false;
112   }
113 
114   public boolean containsValue(Object key) {
115     _logger.debug("Enter MapToMany containsKey " + key);
116     return _map.containsKey(key) ;
117   }
118 
119 
120   // ---------------------------------------------------------------
121   // protected or package methods
122   // ---------------------------------------------------------------
123 
124   /**
125    * Returns a new empty Collection.  
126    */
127   protected abstract Collection createCollection();
128 
129   // ---------------------------------------------------------------
130   // constructors
131   // --------------------------------------------------------------- 
132   public MapToMany(Map map) {
133     _map = map;
134   }
135   
136   // ---------------------------------------------------------------
137   // class members
138   // ---------------------------------------------------------------
139   static Logger _logger = Logger.getLogger(MapToMany.class);
140   final static String UCLASSNAME = "MapToMany";
141   protected Map _map;
142 } // end MapToMany 
143