1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
13 from lucene import PythonSet, PythonIterator, JavaError
16 class JavaSet(PythonSet):
18 This class implements java.util.Set around a Python set instance it wraps.
21 def __init__(self, _set):
22 super(JavaSet, self).__init__()
25 def __contains__(self, obj):
26 return obj in self._set
32 return iter(self._set)
35 if obj not in self._set:
40 def addAll(self, collection):
42 self._set.update(collection)
43 return len(self._set) > size
48 def contains(self, obj):
49 return obj in self._set
51 def containsAll(self, collection):
52 for obj in collection:
53 if obj not in self._set:
57 def equals(self, collection):
58 if type(self) is type(collection):
59 return self._set == collection._set
63 return len(self._set) == 0
66 class _iterator(PythonIterator):
68 super(_iterator, _self).__init__()
69 _self._iterator = iter(self._set)
71 if hasattr(_self, '_next'):
74 _self._next = _self._iterator.next()
79 if hasattr(_self, '_next'):
83 next = _self._iterator.next()
87 def remove(self, obj):
94 def removeAll(self, collection):
96 for obj in collection:
104 def retainAll(self, collection):
106 for obj in list(self._set):
108 self._set.remove(obj)
113 return len(self._set)
116 return list(self._set)