Permalink
Browse files

Filled in some generic type-info in PyUnicode and PyList.

  • Loading branch information...
Stewori committed Jan 11, 2017
1 parent 5822a82 commit 707188dfbc029efac40819f6d864cf63bbdd71f3
Showing with 34 additions and 30 deletions.
  1. +19 −15 src/org/python/core/PyList.java
  2. +15 −15 src/org/python/core/PyUnicode.java
@@ -106,7 +106,7 @@ public PyList(Iterator<PyObject> iter) {
// refactor and put in Py presumably;
// presumably we can consume an arbitrary iterable too!
private static void addCollection(List<PyObject> list, Collection<Object> seq) {
Map<Long, PyObject> seen = new HashMap();
Map<Long, PyObject> seen = new HashMap<>();
for (Object item : seq) {
long id = Py.java_obj_id(item);
PyObject seen_obj = seen.get(id);
@@ -118,6 +118,7 @@ private static void addCollection(List<PyObject> list, Collection<Object> seq) {
}
}
@SuppressWarnings("unchecked")
@ExposedNew
@ExposedMethod(doc = BuiltinDocs.list___init___doc)
final void list___init__(PyObject[] args, String[] kwds) {
@@ -141,7 +142,7 @@ final void list___init__(PyObject[] args, String[] kwds) {
list.addAll(((PyTuple) seq).getList());
} else if (seq.getClass().isAssignableFrom(Collection.class)) {
System.err.println("Adding from collection");
addCollection(list, (Collection)seq);
addCollection(list, (Collection<Object>) seq);
} else {
for (PyObject item : seq.asIterable()) {
append(item);
@@ -169,6 +170,7 @@ protected void delRange(int start, int stop) {
remove(start, stop);
}
@SuppressWarnings("unchecked")
@Override
protected void setslice(int start, int stop, int step, PyObject value) {
if (stop < start) {
@@ -182,19 +184,19 @@ protected void setslice(int start, int stop, int step, PyObject value) {
} else if (value instanceof PySequence) {
setsliceIterator(start, stop, step, value.asIterable().iterator());
} else if (value instanceof List) {
setsliceList(start, stop, step, (List)value);
setsliceList(start, stop, step, (List<Object>)value);
} else {
Object valueList = value.__tojava__(List.class);
if (valueList != null && valueList != Py.NoConversion) {
setsliceList(start, stop, step, (List)valueList);
setsliceList(start, stop, step, (List<Object>)valueList);
} else {
value = new PyList(value);
setsliceIterator(start, stop, step, value.asIterable().iterator());
}
}
}
final private void setsliceList(int start, int stop, int step, List value) {
final private void setsliceList(int start, int stop, int step, List<Object> value) {
if (step == 1) {
list.subList(start, stop).clear();
int n = value.size();
@@ -333,7 +335,7 @@ final synchronized PyObject list___imul__(PyObject o) {
int newSize = size * count;
if (list instanceof ArrayList) {
((ArrayList) list).ensureCapacity(newSize);
((ArrayList<PyObject>) list).ensureCapacity(newSize);
}
List<PyObject> oldList = new ArrayList<PyObject>(list);
for (int i = 1; i < count; i++) {
@@ -379,8 +381,8 @@ final synchronized PyObject list___add__(PyObject o) {
PyList sum = null;
if (o instanceof PySequenceList && !(o instanceof PyTuple)) {
if (o instanceof PyList) {
List oList = ((PyList) o).list;
List newList = new ArrayList(list.size() + oList.size());
List<PyObject> oList = ((PyList) o).list;
ArrayList<PyObject> newList = new ArrayList<>(list.size() + oList.size());
newList.addAll(list);
newList.addAll(oList);
sum = fromList(newList);
@@ -389,11 +391,12 @@ final synchronized PyObject list___add__(PyObject o) {
// also support adding java lists (but not PyTuple!)
Object oList = o.__tojava__(List.class);
if (oList != Py.NoConversion && oList != null) {
List otherList = (List) oList;
@SuppressWarnings("unchecked")
List<PyObject> otherList = (List<PyObject>) oList;
sum = new PyList();
sum.list_extend(this);
for (Iterator i = otherList.iterator(); i.hasNext();) {
sum.add(i.next());
for (PyObject ob: otherList) {
sum.add(ob);
}
}
}
@@ -406,6 +409,7 @@ public PyObject __radd__(PyObject o) {
}
//XXX: needs __doc__
@SuppressWarnings("unchecked")
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject list___radd__(PyObject o) {
// Support adding java.util.List, but prevent adding PyTuple.
@@ -417,7 +421,7 @@ final synchronized PyObject list___radd__(PyObject o) {
Object oList = o.__tojava__(List.class);
if (oList != Py.NoConversion && oList != null) {
sum = new PyList();
sum.addAll((List) oList);
sum.addAll((List<PyObject>) oList);
sum.extend(this);
}
return sum;
@@ -958,7 +962,7 @@ private synchronized void sort(PyObject cmp, PyObject key, boolean reverse) {
Collections.reverse(decorated);
}
if (list instanceof ArrayList) {
((ArrayList) list).ensureCapacity(size);
((ArrayList<PyObject>) list).ensureCapacity(size);
}
for (KV kv : decorated) {
list.add(kv.value);
@@ -1063,8 +1067,8 @@ public synchronized boolean isEmpty() {
}
@Override
public Iterator iterator() {
return new Iterator() {
public Iterator<Object> iterator() {
return new Iterator<Object>() {
private final Iterator<PyObject> iter = list.iterator();
@@ -24,7 +24,7 @@
*/
@Untraversable
@ExposedType(name = "unicode", base = PyBaseString.class, doc = BuiltinDocs.unicode_doc)
public class PyUnicode extends PyString implements Iterable {
public class PyUnicode extends PyString implements Iterable<Integer> {
/**
* Nearly every significant method comes in two versions: one applicable when the string
@@ -822,7 +822,7 @@ public void remove() {
}
}
private static class SteppedIterator<T> implements Iterator {
private static class SteppedIterator<T> implements Iterator<T> {
private final Iterator<T> iter;
private final int step;
@@ -885,14 +885,14 @@ public void remove() {
public Iterator<Integer> newSubsequenceIterator(int start, int stop, int step) {
if (isBasicPlane()) {
if (step < 0) {
return new SteppedIterator(step * -1, new ReversedIterator(
return new SteppedIterator<Integer>(step * -1, new ReversedIterator<Integer>(
new SubsequenceIteratorBasic(stop + 1, start + 1, 1)));
} else {
return new SubsequenceIteratorBasic(start, stop, step);
}
} else {
if (step < 0) {
return new SteppedIterator(step * -1, new ReversedIterator(
return new SteppedIterator<Integer>(step * -1, new ReversedIterator<Integer>(
new SubsequenceIteratorImpl(stop + 1, start + 1, 1)));
} else {
return new SubsequenceIteratorImpl(start, stop, step);
@@ -1026,7 +1026,7 @@ final PyObject unicode_swapcase() {
return new PyUnicode(buffer);
}
private static class StripIterator implements Iterator {
private static class StripIterator implements Iterator<Integer> {
private final Iterator<Integer> iter;
private int lookahead = -1;
@@ -1062,7 +1062,7 @@ public boolean hasNext() {
}
@Override
public Object next() {
public Integer next() {
int old = lookahead;
if (iter.hasNext()) {
lookahead = iter.next();
@@ -1121,7 +1121,7 @@ final PyObject unicode_strip(PyObject sepObj) {
}
// Not basic plane: have to do real Unicode
return new PyUnicode(new ReversedIterator(new StripIterator(sep, new ReversedIterator(
return new PyUnicode(new ReversedIterator<Integer>(new StripIterator(sep, new ReversedIterator<>(
new StripIterator(sep, newSubsequenceIterator())))));
}
@@ -1162,8 +1162,8 @@ final PyObject unicode_rstrip(PyObject sepObj) {
}
// Not basic plane: have to do real Unicode
return new PyUnicode(new ReversedIterator(new StripIterator(sep, new ReversedIterator(
newSubsequenceIterator()))));
return new PyUnicode(new ReversedIterator<Integer>(new StripIterator(sep,
new ReversedIterator<>(newSubsequenceIterator()))));
}
@Override
@@ -1176,7 +1176,7 @@ final PyTuple unicode_partition(PyObject sep) {
return unicodePartition(coerceToUnicode(sep));
}
private abstract class SplitIterator implements Iterator {
private abstract class SplitIterator implements Iterator<PyUnicode> {
protected final int maxsplit;
protected final Iterator<Integer> iter = newSubsequenceIterator();
@@ -1254,7 +1254,7 @@ public PyUnicode next() {
}
}
private static class PeekIterator<T> implements Iterator {
private static class PeekIterator<T> implements Iterator<T> {
private T lookahead = null;
private final Iterator<T> iter;
@@ -1286,7 +1286,7 @@ public void remove() {
}
}
private static class ReversedIterator<T> implements Iterator {
private static class ReversedIterator<T> implements Iterator<T> {
private final List<T> reversed = Generic.list();
private final Iterator<T> iter;
@@ -1315,9 +1315,9 @@ public void remove() {
}
}
private class LineSplitIterator implements Iterator {
private class LineSplitIterator implements Iterator<PyObject> {
private final PeekIterator<Integer> iter = new PeekIterator(newSubsequenceIterator());
private final PeekIterator<Integer> iter = new PeekIterator<>(newSubsequenceIterator());
private final boolean keepends;
LineSplitIterator(boolean keepends) {
@@ -1330,7 +1330,7 @@ public boolean hasNext() {
}
@Override
public Object next() {
public PyObject next() {
StringBuilder buffer = new StringBuilder();
while (iter.hasNext()) {
int codepoint = iter.next();

0 comments on commit 707188d

Please sign in to comment.