KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > query > result > WrapperSet


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.query.result;
25
26 import org.objectweb.jalisto.se.impl.LogicalOid;
27 import org.objectweb.jalisto.se.api.Session;
28
29 import java.util.*;
30
31 public class WrapperSet {
32
33     public WrapperSet() {
34         this.readedWrappers = new HashMap();
35         this.noneReadedWrappers = new HashMap();
36     }
37
38     public int size() {
39         return readedWrappers.size() + noneReadedWrappers.size();
40     }
41
42     public boolean isEmpty() {
43         return readedWrappers.isEmpty() && noneReadedWrappers.isEmpty();
44     }
45
46     public boolean contains(QueryResultWrapper w) {
47         return readedWrappers.containsKey(w.getLogicalOid()) || noneReadedWrappers.containsKey(w.getLogicalOid());
48     }
49
50     // return -1 si dans readed
51
// return 1 si dans non readed
52
// return 0 sinon
53
public int containsIn(QueryResultWrapper w) {
54         if (readedWrappers.containsKey(w.getLogicalOid()))
55             return -1;
56         if (noneReadedWrappers.containsKey(w.getLogicalOid()))
57             return 1;
58         return 0;
59     }
60
61     public Iterator iterator() {
62         ArrayList list = new ArrayList();
63         list.addAll(readedWrappers.values());
64         list.addAll(noneReadedWrappers.values());
65         return list.iterator();
66     }
67
68     public void add(QueryResultWrapper w) {
69         if (w.getValue() == null) {
70             noneReadedWrappers.put(w.getLogicalOid(), w);
71         } else {
72             readedWrappers.put(w.getLogicalOid(), w);
73         }
74     }
75
76     public void addToUnread(QueryResultWrapper w) {
77         noneReadedWrappers.put(w.getLogicalOid(), w);
78     }
79
80     public void addToRead(QueryResultWrapper w) {
81         readedWrappers.put(w.getLogicalOid(), w);
82     }
83
84     public void addAll(Collection collection) {
85         Iterator iterator = collection.iterator();
86         while (iterator.hasNext()) {
87             add((QueryResultWrapper) iterator.next());
88         }
89     }
90
91     public void addAll(WrapperSet wSet) {
92         readedWrappers.putAll(wSet.readedWrappers);
93         noneReadedWrappers.putAll(wSet.noneReadedWrappers);
94     }
95
96     public QueryResultWrapper get(Object JavaDoc key) {
97         if (readedWrappers.containsKey(key)) {
98             return (QueryResultWrapper) readedWrappers.get(key);
99         } else {
100             return (QueryResultWrapper) noneReadedWrappers.get(key);
101         }
102     }
103
104     public Set getCopyInSet() {
105         HashSet result = new HashSet();
106         result.addAll(readedWrappers.values());
107         result.addAll(noneReadedWrappers.values());
108         return result;
109     }
110
111     public void remove(QueryResultWrapper w) {
112         Object JavaDoc loid = w.getLogicalOid();
113         if (readedWrappers.remove(loid) == null) {
114             noneReadedWrappers.remove(loid);
115         }
116     }
117
118     public void clear() {
119         readedWrappers.clear();
120         noneReadedWrappers.clear();
121     }
122
123     public void readAll(Session session, Map valueMap) {
124         Iterator none = noneReadedWrappers.values().iterator();
125         while (none.hasNext()) {
126             QueryResultWrapper wrapper = (QueryResultWrapper) none.next();
127             LogicalOid loid = wrapper.getLogicalOid();
128             if (valueMap.containsKey(loid)) {
129                 wrapper.setValue((Object JavaDoc[]) valueMap.get(loid));
130             } else {
131                 Object JavaDoc[] value = session.readObjectByOid(loid);
132                 wrapper.setValue(value);
133                 valueMap.put(loid, value);
134             }
135             readedWrappers.put(loid, wrapper);
136         }
137         noneReadedWrappers.clear();
138     }
139
140     private Map readedWrappers;
141     private Map noneReadedWrappers;
142 }
143
Popular Tags