001 package com.hammurapi.reasoning.impl;
002
003 import java.lang.reflect.Array;
004 import java.util.Collection;
005 import java.util.Iterator;
006 import java.util.concurrent.locks.Lock;
007 import java.util.concurrent.locks.ReadWriteLock;
008 import java.util.concurrent.locks.ReentrantReadWriteLock;
009
010 import com.hammurapi.reasoning.Handle;
011 import com.hammurapi.reasoning.ReasoningException;
012
013 public class HandleCollection<E> implements Collection<E>, ReadWriteLock {
014
015 protected Collection<Handle<E>> handleCollection;
016 protected KnowledgeBase<E> knowledgeBase;
017
018
019 public HandleCollection(KnowledgeBase<E> kb, Collection<Handle<E>> handleCollection) {
020 this.knowledgeBase = kb;
021 this.handleCollection = handleCollection;
022 }
023
024
025 public boolean add(E e) {
026 try {
027 return handleCollection.add(knowledgeBase.put(e));
028 } catch (ReasoningException ex) {
029 throw new RuntimeReasoningException(ex);
030 }
031 }
032
033
034 public boolean addAll(Collection<? extends E> c) {
035 boolean ret = false;
036 for (E e: c) {
037 if (add(e)) {
038 ret = true;
039 }
040 }
041 return ret;
042 }
043
044
045 public void clear() {
046 handleCollection.clear();
047 }
048
049
050 public boolean contains(Object o) {
051 Handle<E> handle = knowledgeBase.getHandle(o);
052 return handle==null ? false : handleCollection.contains(handle);
053 }
054
055
056 public boolean containsAll(Collection<?> c) {
057 for (Object o: c) {
058 if (!contains(o)) {
059 return false;
060 }
061 }
062 return true;
063 }
064
065
066 // TODO - implement equals and hash code correctly
067 // public boolean equals(Object o) {
068 // return handleCollection.equals(o);
069 // }
070 //
071 //
072 // public int hashCode() {
073 // return handleCollection.hashCode();
074 // }
075
076
077 public boolean isEmpty() {
078 return handleCollection.isEmpty();
079 }
080
081
082 public Iterator<E> iterator() {
083 return new Iterator<E>() {
084 Iterator<Handle<E>> master = handleCollection.iterator();
085
086 @Override
087 public boolean hasNext() {
088 return master.hasNext();
089 }
090
091 @SuppressWarnings("unchecked")
092 @Override
093 public E next() {
094 Handle hNext = master.next();
095 return ((HandleImpl<E>) hNext).get();
096 }
097
098 @Override
099 public void remove() {
100 master.remove();
101 }
102
103 };
104 }
105
106
107 public boolean remove(Object o) {
108 Handle<E> handle = knowledgeBase.getHandle(o);
109 return handle==null ? false : handleCollection.remove(handle);
110 }
111
112
113 public boolean removeAll(Collection<?> c) {
114 boolean ret = false;
115 for (Object e: c) {
116 if (remove(e)) {
117 ret = true;
118 }
119 }
120 return ret;
121 }
122
123
124 public boolean retainAll(Collection<?> c) {
125 boolean ret = true;
126 Iterator<E> it = iterator();
127 while (it.hasNext()) {
128 E next = it.next();
129 if (!c.contains(next)) {
130 ret = true;
131 it.remove();
132 }
133 }
134 return ret;
135 }
136
137
138 public int size() {
139 return handleCollection.size();
140 }
141
142 public Object[] toArray() {
143 Object[] ret = new Object[size()];
144 Iterator<E> it = iterator();
145 for (int i=0; it.hasNext(); ++i) {
146 ret[i] = it.next();
147 }
148 return ret;
149 }
150
151
152 @SuppressWarnings("unchecked")
153 public <T> T[] toArray(T[] a) {
154 T[] ret = a.length>=size() ? a : (T[]) Array.newInstance(a.getClass().getComponentType(), size());
155 Iterator<E> it = iterator();
156 for (int i=0; it.hasNext(); ++i) {
157 ret[i] = (T) it.next();
158 }
159 return ret;
160 }
161
162 private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
163
164 @Override
165 public Lock readLock() {
166 return rwLock.readLock();
167 }
168
169
170 @Override
171 public Lock writeLock() {
172 return rwLock.writeLock();
173 }
174
175
176 }