web/lib/arch/osx/lucene/collections.py
changeset 29 cc9b7e14412b
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
       
     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
       
     4 #
       
     5 #       http://www.apache.org/licenses/LICENSE-2.0
       
     6 #
       
     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.
       
    12 
       
    13 from lucene import PythonSet, PythonIterator, JavaError
       
    14 
       
    15 
       
    16 class JavaSet(PythonSet):
       
    17     """
       
    18     This class implements java.util.Set around a Python set instance it wraps.
       
    19     """
       
    20 
       
    21     def __init__(self, _set):
       
    22         super(JavaSet, self).__init__()
       
    23         self._set = _set
       
    24 
       
    25     def __contains__(self, obj):
       
    26         return obj in self._set
       
    27 
       
    28     def __len__(self):
       
    29         return len(self._set)
       
    30 
       
    31     def __iter__(self):
       
    32         return iter(self._set)
       
    33 
       
    34     def add(self, obj):
       
    35         if obj not in self._set:
       
    36             self._set.add(obj)
       
    37             return True
       
    38         return False
       
    39 
       
    40     def addAll(self, collection):
       
    41         size = len(self._set)
       
    42         self._set.update(collection)
       
    43         return len(self._set) > size
       
    44 
       
    45     def clear(self):
       
    46         self._set.clear()
       
    47 
       
    48     def contains(self, obj):
       
    49         return obj in self._set
       
    50 
       
    51     def containsAll(self, collection):
       
    52         for obj in collection:
       
    53             if obj not in self._set:
       
    54                 return False
       
    55         return True
       
    56 
       
    57     def equals(self, collection):
       
    58         if type(self) is type(collection):
       
    59             return self._set == collection._set
       
    60         return False
       
    61 
       
    62     def isEmpty(self):
       
    63         return len(self._set) == 0
       
    64 
       
    65     def iterator(self):
       
    66         class _iterator(PythonIterator):
       
    67             def __init__(_self):
       
    68                 super(_iterator, _self).__init__()
       
    69                 _self._iterator = iter(self._set)
       
    70             def hasNext(_self):
       
    71                 if hasattr(_self, '_next'):
       
    72                     return True
       
    73                 try:
       
    74                     _self._next = _self._iterator.next()
       
    75                     return True
       
    76                 except StopIteration:
       
    77                     return False
       
    78             def next(_self):
       
    79                 if hasattr(_self, '_next'):
       
    80                     next = _self._next
       
    81                     del _self._next
       
    82                 else:
       
    83                     next = _self._iterator.next()
       
    84                 return next
       
    85         return _iterator()
       
    86             
       
    87     def remove(self, obj):
       
    88         try:
       
    89             self._set.remove(obj)
       
    90             return True
       
    91         except KeyError:
       
    92             return False
       
    93 
       
    94     def removeAll(self, collection):
       
    95         result = False
       
    96         for obj in collection:
       
    97             try:
       
    98                 self._set.remove(obj)
       
    99                 result = True
       
   100             except KeyError:
       
   101                 pass
       
   102         return result
       
   103 
       
   104     def retainAll(self, collection):
       
   105         result = False
       
   106         for obj in list(self._set):
       
   107             if obj not in c:
       
   108                 self._set.remove(obj)
       
   109                 result = True
       
   110         return result
       
   111 
       
   112     def size(self):
       
   113         return len(self._set)
       
   114 
       
   115     def toArray(self):
       
   116         return list(self._set)
       
   117