KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > eziba > sql > ConnectionMap


1 /*
2  * Copyright (C) 2001 eZiba.com, Inc.
3  * All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution. Neither the name of eZiba.com nor the
15  * names of its contributors may be used to endorse or promote
16  * products derived from this software without specific prior
17  * written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * eZiba.com OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.mockobjects.eziba.sql;
34 import java.sql.SQLException JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.HashMap JavaDoc;class ConnectionMap
38 {
39
40     ConnectionMap(String JavaDoc p_type)
41     {
42         m_type = p_type;
43     }
44
45     public String JavaDoc toString()
46     {
47         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
48         sb.append(m_type);
49         sb.append(NEWLINE);
50         for (Iterator JavaDoc i = m_map.keySet().iterator();
51                  i.hasNext();
52              )
53         {
54             Object JavaDoc sql = i.next();
55             Map JavaDoc map = (Map JavaDoc) m_map.get(sql);
56             sb.append(sql);
57             sb.append(NEWLINE);
58             for (Iterator JavaDoc j = map.keySet().iterator();
59                  j.hasNext();
60                  )
61             {
62                 Object JavaDoc args = j.next();
63                 sb.append(args);
64                 sb.append(NEWLINE);
65             }
66             sb.append(NEWLINE);
67         }
68         return sb.toString();
69     }
70
71     public int size()
72     {
73         int result = 0;
74         for (Iterator JavaDoc i = m_map.values().iterator();
75              i.hasNext();
76              )
77         {
78             Map JavaDoc m = (Map JavaDoc) i.next();
79             result += m.size();
80         }
81         return result;
82     }
83
84     public void put(String JavaDoc p_sql, Object JavaDoc [] p_args, Object JavaDoc p_obj)
85     {
86         Map JavaDoc map = (Map JavaDoc) m_map.get(p_sql);
87         if (map == null)
88         {
89             map = new HashMap JavaDoc();
90             m_map.put(p_sql,map);
91         }
92         map.put(new ArgumentArray(p_args),p_obj);
93     }
94
95     public Object JavaDoc get(String JavaDoc p_sql, Object JavaDoc [] p_args)
96         throws SQLException JavaDoc
97     {
98         Map JavaDoc map = (Map JavaDoc) m_map.get(p_sql);
99         if (map == null)
100         {
101             throw new SQLException JavaDoc("No " + m_type
102                                    + " registered for " + p_sql);
103         }
104         else
105         {
106             ArgumentArray args = new ArgumentArray(p_args);
107             Object JavaDoc result = map.get(args);
108             if (result == null)
109             {
110                 throw new SQLException JavaDoc("No " + m_type + " registered for "
111                                        + p_sql + " with args "+ args
112                                        + ". Possible argument matches are "
113                                        + get(p_sql));
114             }
115             map.remove(args);
116             if (map.size() == 0)
117             {
118                 m_map.remove(p_sql);
119             }
120             return result;
121         }
122     }
123
124     /**
125      *
126      * @return array of arrays, really
127      */

128     private ArgumentArray get(String JavaDoc p_sql)
129     {
130         Map JavaDoc map = (Map JavaDoc) m_map.get(p_sql);
131         if (map == null)
132         {
133             return new ArgumentArray(new Object JavaDoc [0]);
134         }
135         return new ArgumentArray(map.keySet().toArray());
136     }
137
138     private final Map JavaDoc m_map = new HashMap JavaDoc();
139
140     private final String JavaDoc m_type;
141
142     private static final String JavaDoc NEWLINE =
143         System.getProperty("line.separator");
144 }
Popular Tags