1 /*
2 * $Source$
3 * $Revision$
4 *
5 * Copyright (C) 2007 Tim Pizey
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 * Tim Pizey <timp At paneris.org>
42 * http://paneris.org/~timp
43 */
44 package org.melati.poem;
45
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.Map;
49 import java.util.Set;
50
51 /**
52 * @author timp
53 * @since 8 Jun 2007
54 *
55 */
56 public class TableMap<P extends Persistent> implements Map<Integer, P> {
57
58 protected Table<P> table;
59 /**
60 * Constructor.
61 */
62 public TableMap() {
63 }
64
65 /**
66 * Constructor given a Table.
67 */
68 public TableMap(Table<P> t) {
69 this.table = t;
70 }
71
72 /**
73 * @return the table
74 */
75 public Table<P> getTable() {
76 return table;
77 }
78
79 /**
80 * @param table the table to set
81 */
82 public void setTable(Table<P> table) {
83 this.table = table;
84 }
85
86
87
88 /**
89 * @see java.util.Map#clear()
90 */
91 @Override
92 public void clear() {
93 throw new UnsupportedOperationException();
94 }
95
96 /**
97 * @see java.util.Map#containsKey(java.lang.Object)
98 */
99 @Override
100 public boolean containsKey(Object key) {
101 if (key == null)
102 return false;
103 else {
104 try {
105 table.getObject((Integer)key);
106 return true;
107 } catch (NoSuchRowPoemException e) {
108 return false;
109 }
110 }
111 }
112
113 /**
114 * @see java.util.Map#containsValue(java.lang.Object)
115 */
116 @Override
117 public boolean containsValue(Object value) {
118 Integer troid = ((Persistent)value).troid();
119 return containsKey(troid);
120 }
121
122 /**
123 * @see java.util.Map#entrySet()
124 */
125 @Override
126 public Set<Map.Entry<Integer, P>> entrySet() {
127 throw new UnsupportedOperationException();
128 }
129
130 /**
131 * @see java.util.Map#get(java.lang.Object)
132 */
133 @Override
134 public P get(Object key) {
135 return table.getObject((Integer)key);
136 }
137
138 /**
139 * @see java.util.Map#isEmpty()
140 */
141 @Override
142 public boolean isEmpty() {
143 return table.cachedCount((String)null).count() == 0;
144 }
145
146 /**
147 * @see java.util.Map#keySet()
148 */
149 @Override
150 public Set<Integer> keySet() {
151 throw new UnsupportedOperationException();
152 }
153
154 /**
155 * @see java.util.Map#put
156 */
157 @Override
158 public P put(Integer arg0, Persistent arg1) {
159 throw new UnsupportedOperationException();
160 }
161
162
163 /**
164 * @see java.util.Map#remove(java.lang.Object)
165 */
166 @Override
167 public P remove(Object key) {
168 P p = table.getObject((Integer)key);
169 if (p != null)
170 p.delete();
171 return p;
172 }
173
174 /**
175 * @see java.util.Map#size()
176 */
177 @Override
178 public int size() {
179 return table.cachedCount((String)null).count();
180 }
181
182 /**
183 * @see java.util.Map#values()
184 */
185 @Override
186 public Collection<P> values() {
187 return Collections.list(table.selection());
188 }
189
190
191 @Override
192 public void putAll(Map<? extends Integer, ? extends P> arg0) {
193 throw new UnsupportedOperationException();
194 }
195
196
197 }