You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pylucene-commits@lucene.apache.org by va...@apache.org on 2012/05/03 00:53:17 UTC
svn commit: r1333240 - in /lucene/pylucene/branches/branch_3x: CHANGES
java/org/apache/pylucene/util/PythonList.java
java/org/apache/pylucene/util/PythonListIterator.java
java/org/apache/pylucene/util/PythonSet.java python/collections.py
Author: vajda
Date: Wed May 2 22:53:16 2012
New Revision: 1333240
URL: http://svn.apache.org/viewvc?rev=1333240&view=rev
Log:
- added JavaList to collections.py, a Python java.util.List (Thomas Koch)
Added:
lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonList.java (with props)
lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonListIterator.java (with props)
Modified:
lucene/pylucene/branches/branch_3x/CHANGES
lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonSet.java
lucene/pylucene/branches/branch_3x/python/collections.py
Modified: lucene/pylucene/branches/branch_3x/CHANGES
URL: http://svn.apache.org/viewvc/lucene/pylucene/branches/branch_3x/CHANGES?rev=1333240&r1=1333239&r2=1333240&view=diff
==============================================================================
--- lucene/pylucene/branches/branch_3x/CHANGES (original)
+++ lucene/pylucene/branches/branch_3x/CHANGES Wed May 2 22:53:16 2012
@@ -2,6 +2,7 @@ Version 3.5.0 ->
---------------------
- renamed classes whose python name would not be unique in lucene module
- refreshed Linux build options, added an OpenJDK 7 example
+ - added JavaList to collections.py, a Python java.util.List (Thomas Koch)
-
Version 3.4 -> 3.5.0
Added: lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonList.java
URL: http://svn.apache.org/viewvc/lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonList.java?rev=1333240&view=auto
==============================================================================
--- lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonList.java (added)
+++ lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonList.java Wed May 2 22:53:16 2012
@@ -0,0 +1,115 @@
+/* ====================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.pylucene.util;
+
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Collection;
+import java.util.Iterator;
+import java.lang.reflect.Array;
+
+public class PythonList implements List {
+
+ private long pythonObject;
+
+ public PythonList()
+ {
+ }
+
+ public void pythonExtension(long pythonObject)
+ {
+ this.pythonObject = pythonObject;
+ }
+ public long pythonExtension()
+ {
+ return this.pythonObject;
+ }
+
+ public void finalize()
+ throws Throwable
+ {
+ pythonDecRef();
+ }
+
+ public native void pythonDecRef();
+
+ public native boolean add(Object obj);
+ public native void add(int index, Object obj);
+ public native boolean addAll(Collection c);
+ public native boolean addAll(int index, Collection c);
+ public native void clear();
+ public native boolean contains(Object obj);
+ public native boolean containsAll(Collection c);
+ public native boolean equals(Object obj);
+ public native Object get(int index);
+ // public native int hashCode();
+ public native int indexOf(Object obj);
+ public native boolean isEmpty();
+ public native Iterator iterator();
+ public native int lastIndexOf(Object obj);
+
+ public native ListIterator listIterator(int index);
+ public ListIterator listIterator()
+ {
+ return listIterator(0);
+ }
+
+ private native Object removeAt(int index);
+ public Object remove(int index)
+ throws IndexOutOfBoundsException
+ {
+ if (index < 0 || index >= this.size())
+ throw new IndexOutOfBoundsException();
+
+ return removeAt(index);
+ }
+
+ private native boolean removeObject(Object obj);
+ public boolean remove(Object obj)
+ {
+ return removeObject(obj);
+ }
+
+ public native boolean removeAll(Collection c);
+ public native boolean retainAll(Collection c);
+ public native Object set(int index, Object obj);
+ public native int size();
+
+ private native List subListChecked(int fromIndex, int toIndex);
+ public List subList(int fromIndex, int toIndex)
+ throws IndexOutOfBoundsException, IllegalArgumentException
+ {
+ if (fromIndex < 0 || toIndex >= size() || fromIndex > toIndex)
+ throw new IndexOutOfBoundsException();
+
+ return subListChecked(fromIndex, toIndex);
+ }
+
+ public native Object[] toArray();
+
+ public Object[] toArray(Object[] a)
+ {
+ Object[] array = toArray();
+
+ if (a.length < array.length)
+ a = (Object[]) Array.newInstance(a.getClass().getComponentType(),
+ array.length);
+
+ System.arraycopy(array, 0, a, 0, array.length);
+
+ return a;
+ }
+}
Propchange: lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonList.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonList.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonListIterator.java
URL: http://svn.apache.org/viewvc/lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonListIterator.java?rev=1333240&view=auto
==============================================================================
--- lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonListIterator.java (added)
+++ lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonListIterator.java Wed May 2 22:53:16 2012
@@ -0,0 +1,30 @@
+/* ====================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.pylucene.util;
+
+import java.util.ListIterator;
+
+public class PythonListIterator extends PythonIterator implements ListIterator {
+ public native boolean hasPrevious();
+ public native Object previous();
+
+ public native int nextIndex();
+ public native int previousIndex();
+
+ public native void set(Object obj);
+ public native void add(Object obj);
+ public native void remove();
+}
Propchange: lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonListIterator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonListIterator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonSet.java
URL: http://svn.apache.org/viewvc/lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonSet.java?rev=1333240&r1=1333239&r2=1333240&view=diff
==============================================================================
--- lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonSet.java (original)
+++ lucene/pylucene/branches/branch_3x/java/org/apache/pylucene/util/PythonSet.java Wed May 2 22:53:16 2012
@@ -58,12 +58,13 @@ public class PythonSet implements Set {
public native boolean removeAll(Collection c);
public native boolean retainAll(Collection c);
public native int size();
+
public native Object[] toArray();
public Object[] toArray(Object[] a)
{
Object[] array = toArray();
-
+
if (a.length < array.length)
a = (Object[]) Array.newInstance(a.getClass().getComponentType(),
array.length);
Modified: lucene/pylucene/branches/branch_3x/python/collections.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/branches/branch_3x/python/collections.py?rev=1333240&r1=1333239&r2=1333240&view=diff
==============================================================================
--- lucene/pylucene/branches/branch_3x/python/collections.py (original)
+++ lucene/pylucene/branches/branch_3x/python/collections.py Wed May 2 22:53:16 2012
@@ -10,7 +10,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from lucene import PythonSet, PythonIterator, JavaError
+from lucene import JArray, \
+ PythonSet, PythonList, PythonIterator, PythonListIterator, JavaError, \
+ NoSuchElementException, IllegalStateException, IndexOutOfBoundsException
class JavaSet(PythonSet):
@@ -83,7 +85,7 @@ class JavaSet(PythonSet):
next = _self._iterator.next()
return next
return _iterator()
-
+
def remove(self, obj):
try:
self._set.remove(obj)
@@ -104,7 +106,7 @@ class JavaSet(PythonSet):
def retainAll(self, collection):
result = False
for obj in list(self._set):
- if obj not in c:
+ if obj not in collection:
self._set.remove(obj)
result = True
return result
@@ -112,6 +114,240 @@ class JavaSet(PythonSet):
def size(self):
return len(self._set)
- def toArray(self):
+ def toArray(self): # JavaSet
return list(self._set)
+
+class JavaListIterator(PythonListIterator):
+ """
+ This class implements java.util.ListIterator for a Python list instance it
+ wraps. (simple bidirectional iterator)
+ """
+ def __init__(self, _lst, index=0):
+ super(JavaListIterator, self).__init__()
+ self._lst = _lst
+ self._lastIndex = -1 # keep state for remove/set
+ self.index = index
+
+ def next(self):
+ if self.index >= len(self._lst):
+ raise JavaError, NoSuchElementException(str(self.index))
+ result = self._lst[self.index]
+ self._lastIndex = self.index
+ self.index += 1
+ return result
+
+ def previous(self):
+ if self.index <= 0:
+ raise JavaError, NoSuchElementException(str(self.index - 1))
+ self.index -= 1
+ self._lastIndex = self.index
+ return self._lst[self.index]
+
+ def hasPrevious(self):
+ return self.index > 0
+
+ def hasNext(self):
+ return self.index < len(self._lst)
+
+ def nextIndex(self):
+ return min(self.index, len(self._lst))
+
+ def previousIndex(self):
+ return max(-1, self.index - 1)
+
+ def add(self, element):
+ """
+ Inserts the specified element into the list.
+ The element is inserted immediately before the next element
+ that would be returned by next, if any, and after the next
+ element that would be returned by previous, if any.
+ """
+ if self._lastIndex < 0:
+ raise JavaError, IllegalStateException("add")
+ self._lst.insert(self.index, element)
+ self.index += 1
+ self._lastIndex = -1 # invalidate state
+
+ def remove(self):
+ """
+ Removes from the list the last element that
+ was returned by next or previous.
+ """
+ if self._lastIndex < 0:
+ raise JavaError, IllegalStateException("remove")
+ del self._lst[self._lastIndex]
+ self._lastIndex = -1 # invalidate state
+
+ def set(self, element):
+ """
+ Replaces the last element returned by next or previous
+ with the specified element.
+ """
+ if self._lastIndex < 0:
+ raise JavaError, IllegalStateException("set")
+ self._lst[self._lastIndex] = element
+
+ def __iter__(self):
+ return self
+
+
+class JavaList(PythonList):
+ """
+ This class implements java.util.List around a Python list instance it wraps.
+ """
+
+ def __init__(self, _lst):
+ super(JavaList, self).__init__()
+ self._lst = _lst
+
+ def __contains__(self, obj):
+ return obj in self._lst
+
+ def __len__(self):
+ return len(self._lst)
+
+ def __iter__(self):
+ return iter(self._lst)
+
+ def add(self, index, obj):
+ self._lst.insert(index, obj)
+
+ def addAll(self, collection):
+ size = len(self._lst)
+ self._lst.extend(collection)
+ return len(self._lst) > size
+
+ def addAll(self, index, collection):
+ size = len(self._lst)
+ self._lst[index:index] = collection
+ return len(self._lst) > size
+
+ def clear(self):
+ del self._lst[:]
+
+ def contains(self, obj):
+ return obj in self._lst
+
+ def containsAll(self, collection):
+ for obj in collection:
+ if obj not in self._lst:
+ return False
+ return True
+
+ def equals(self, collection):
+ if type(self) is type(collection):
+ return self._lst == collection._lst
+ return False
+
+ def get(self, index):
+ if index < 0 or index >= self.size():
+ raise JavaError, IndexOutOfBoundsException(str(index))
+ return self._lst[index]
+
+ def indexOf(self, obj):
+ try:
+ return self._lst.index(obj)
+ except ValueError:
+ return -1
+
+ def isEmpty(self):
+ return len(self._lst) == 0
+
+ def iterator(self):
+ class _iterator(PythonIterator):
+ def __init__(_self):
+ super(_iterator, _self).__init__()
+ _self._iterator = iter(self._lst)
+ def hasNext(_self):
+ if hasattr(_self, '_next'):
+ return True
+ try:
+ _self._next = _self._iterator.next()
+ return True
+ except StopIteration:
+ return False
+ def next(_self):
+ if hasattr(_self, '_next'):
+ next = _self._next
+ del _self._next
+ else:
+ next = _self._iterator.next()
+ return next
+ return _iterator()
+
+ def lastIndexOf(self, obj):
+ i = len(self._lst)-1
+ while (i>=0):
+ if obj.equals(self._lst[i]):
+ break
+ i -= 1
+ return i
+
+ def listIterator(self, index=0):
+ return JavaListIterator(self._lst, index)
+
+ def remove(self, obj_or_index):
+ if type(obj_or_index) is type(1):
+ return removeAt(int(obj_or_index))
+ return removeElement(obj_or_index)
+
+ def removeAt(self, pos):
+ """
+ Removes the element at the specified position in this list.
+ Note: private method called from Java via remove(int index)
+ index is already checked (or IndexOutOfBoundsException thrown)
+ """
+ try:
+ el = self._lst[pos]
+ del self._lst[pos]
+ return el
+ except IndexError:
+ # should not happen
+ return None
+
+ def removeObject(self, obj):
+ """
+ Removes the first occurrence of the specified object
+ from this list, if it is present
+ """
+ try:
+ self._lst.remove(obj)
+ return True
+ except ValueError:
+ return False
+
+ def removeAll(self, collection):
+ result = False
+ for obj in collection:
+ if self.removeElement(obj):
+ result = True
+ return result
+
+ def retainAll(self, collection):
+ result = False
+ for obj in self._lst:
+ if obj not in collection and self.removeElement(obj):
+ result = True
+ return result
+
+ def size(self):
+ return len(self._lst)
+
+ def toArray(self):
+ return self._lst
+
+ def subListChecked(self, fromIndex, toIndex):
+ """
+ Note: private method called from Java via subList()
+ from/to index are already checked (or IndexOutOfBoundsException thrown)
+ also IllegalArgumentException is thronw if the endpoint indices
+ are out of order (fromIndex > toIndex)
+ """
+ sublst = self._lst[fromIndex:toIndex]
+ return JavaList(sublst)
+
+ def set(self, index, obj):
+ if index < 0 or index >= self.size():
+ raise JavaError, IndexOutOfBoundsException(str(index))
+ self._lst[index] = obj