From: <pj...@us...> - 2009-10-28 23:42:04
|
Revision: 6926 http://jython.svn.sourceforge.net/jython/?rev=6926&view=rev Author: pjenvey Date: 2009-10-28 23:41:45 +0000 (Wed, 28 Oct 2009) Log Message: ----------- coding standards Modified Paths: -------------- trunk/jython/src/org/python/core/PyDictionary.java Modified: trunk/jython/src/org/python/core/PyDictionary.java =================================================================== --- trunk/jython/src/org/python/core/PyDictionary.java 2009-10-28 16:01:21 UTC (rev 6925) +++ trunk/jython/src/org/python/core/PyDictionary.java 2009-10-28 23:41:45 UTC (rev 6926) @@ -1,4 +1,7 @@ -// Copyright (c) Corporation for National Research Initiatives +/* + * Copyright (c) Corporation for National Research Initiatives + * Copyright (c) Jython Developers + */ package org.python.core; import java.util.AbstractSet; @@ -73,8 +76,8 @@ */ public PyDictionary(PyObject elements[]) { this(); - for (int i = 0; i < elements.length; i+=2) { - table.put(elements[i], elements[i+1]); + for (int i = 0; i < elements.length; i += 2) { + table.put(elements[i], elements[i + 1]); } } @@ -101,6 +104,7 @@ return d; } + @Override public int __len__() { return dict___len__(); } @@ -110,6 +114,7 @@ return table.size(); } + @Override public boolean __nonzero__() { return dict___nonzero__(); } @@ -120,10 +125,12 @@ return table.size() != 0; } + @Override public PyObject __finditem__(int index) { throw Py.TypeError("loop over non-sequence"); } + @Override public PyObject __finditem__(PyObject key) { return table.get(key); } @@ -146,8 +153,9 @@ throw Py.KeyError(key); } + @Override public void __setitem__(PyObject key, PyObject value) { - dict___setitem__(key,value); + dict___setitem__(key, value); } @ExposedMethod(doc = BuiltinDocs.dict___setitem___doc) @@ -155,6 +163,7 @@ table.put(key, value); } + @Override public void __delitem__(PyObject key) { dict___delitem__(key); } @@ -162,10 +171,12 @@ @ExposedMethod(doc = BuiltinDocs.dict___delitem___doc) final void dict___delitem__(PyObject key) { Object ret = table.remove(key); - if (ret == null) + if (ret == null) { throw Py.KeyError(key.toString()); + } } + @Override public PyObject __iter__() { return dict___iter__(); } @@ -175,6 +186,7 @@ return iterkeys(); } + @Override public String toString() { return dict_toString(); } @@ -187,14 +199,13 @@ } StringBuilder buf = new StringBuilder("{"); - for (Entry<PyObject, PyObject> entry : table.entrySet()) { buf.append((entry.getKey()).__repr__().toString()); buf.append(": "); buf.append((entry.getValue()).__repr__().toString()); buf.append(", "); } - if(buf.length() > 1){ + if (buf.length() > 1) { buf.delete(buf.length() - 2, buf.length()); } buf.append("}"); @@ -203,101 +214,113 @@ return buf.toString(); } - public PyObject __eq__(PyObject ob_other) { - return dict___eq__(ob_other); + @Override + public PyObject __eq__(PyObject otherObj) { + return dict___eq__(otherObj); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___eq___doc) - final PyObject dict___eq__(PyObject ob_other) { + final PyObject dict___eq__(PyObject otherObj) { PyType thisType = getType(); - PyType otherType = ob_other.getType(); + PyType otherType = otherObj.getType(); if (otherType != thisType && !thisType.isSubType(otherType) && !otherType.isSubType(thisType)) { return null; } - PyDictionary other = (PyDictionary)ob_other; + PyDictionary other = (PyDictionary)otherObj; int an = table.size(); int bn = other.table.size(); - if (an != bn) + if (an != bn) { return Py.False; + } PyList akeys = keys(); - for (int i=0; i<an; i++) { + for (int i = 0; i < an; i++) { PyObject akey = akeys.pyget(i); PyObject bvalue = other.__finditem__(akey); - if (bvalue == null) + if (bvalue == null) { return Py.False; + } PyObject avalue = __finditem__(akey); - if (!avalue._eq(bvalue).__nonzero__()) + if (!avalue._eq(bvalue).__nonzero__()) { return Py.False; + } } return Py.True; } - public PyObject __ne__(PyObject ob_other) { - return dict___ne__(ob_other); + @Override + public PyObject __ne__(PyObject otherObj) { + return dict___ne__(otherObj); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___ne___doc) - final PyObject dict___ne__(PyObject ob_other) { - PyObject eq_result = __eq__(ob_other); - if (eq_result == null) return null; + final PyObject dict___ne__(PyObject otherObj) { + PyObject eq_result = __eq__(otherObj); + if (eq_result == null) { + return null; + } return eq_result.__not__(); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___lt___doc) - final PyObject dict___lt__(PyObject ob_other){ - int result = __cmp__(ob_other); - if(result == -2){ + final PyObject dict___lt__(PyObject otherObj) { + int result = __cmp__(otherObj); + if (result == -2) { return null; } return result < 0 ? Py.True : Py.False; } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___gt___doc) - final PyObject dict___gt__(PyObject ob_other){ - int result = __cmp__(ob_other); - if(result == -2){ + final PyObject dict___gt__(PyObject otherObj) { + int result = __cmp__(otherObj); + if (result == -2) { return null; } return result > 0 ? Py.True : Py.False; } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___le___doc) - final PyObject dict___le__(PyObject ob_other){ - int result = __cmp__(ob_other); - if(result == -2){ + final PyObject dict___le__(PyObject otherObj) { + int result = __cmp__(otherObj); + if (result == -2) { return null; } return result <= 0 ? Py.True : Py.False; } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___ge___doc) - final PyObject dict___ge__(PyObject ob_other){ - int result = __cmp__(ob_other); - if(result == -2){ + final PyObject dict___ge__(PyObject otherObj) { + int result = __cmp__(otherObj); + if (result == -2) { return null; } return result >= 0 ? Py.True : Py.False; } - public int __cmp__(PyObject ob_other) { - return dict___cmp__(ob_other); + @Override + public int __cmp__(PyObject otherObj) { + return dict___cmp__(otherObj); } @ExposedMethod(type = MethodType.CMP, doc = BuiltinDocs.dict___cmp___doc) - final int dict___cmp__(PyObject ob_other) { + final int dict___cmp__(PyObject otherObj) { PyType thisType = getType(); - PyType otherType = ob_other.getType(); + PyType otherType = otherObj.getType(); if (otherType != thisType && !thisType.isSubType(otherType) && !otherType.isSubType(thisType)) { return -2; } - PyDictionary other = (PyDictionary)ob_other; + PyDictionary other = (PyDictionary)otherObj; int an = table.size(); int bn = other.table.size(); - if (an < bn) return -1; - if (an > bn) return 1; + if (an < bn) { + return -1; + } + if (an > bn) { + return 1; + } PyList akeys = keys(); PyList bkeys = other.keys(); @@ -305,26 +328,28 @@ akeys.sort(); bkeys.sort(); - for (int i=0; i<bn; i++) { + for (int i = 0; i < bn; i++) { PyObject akey = akeys.pyget(i); PyObject bkey = bkeys.pyget(i); int c = akey._cmp(bkey); - if (c != 0) + if (c != 0) { return c; + } PyObject avalue = __finditem__(akey); PyObject bvalue = other.__finditem__(bkey); - if(avalue == null){ - if(bvalue == null){ + if (avalue == null) { + if (bvalue == null) { continue; } return -3; - }else if(bvalue == null){ + } else if (bvalue == null) { return -3; } c = avalue._cmp(bvalue); - if (c != 0) + if (c != 0) { return c; + } } return 0; } @@ -341,6 +366,7 @@ return table.containsKey(key); } + @Override public boolean __contains__(PyObject o) { return dict___contains__(o); } @@ -351,24 +377,20 @@ } /** - * Return this[key] if the key exists in the mapping, default_object - * is returned otherwise. + * Return this[key] if the key exists in the mapping, defaultObj is returned + * otherwise. * - * @param key the key to lookup in the dictionary. - * @param default_object the value to return if the key does not - * exists in the mapping. + * @param key the key to lookup in the dictionary. + * @param defaultObj the value to return if the key does not exists in the mapping. */ - public PyObject get(PyObject key, PyObject default_object) { - return dict_get(key,default_object); + public PyObject get(PyObject key, PyObject defaultObj) { + return dict_get(key, defaultObj); } @ExposedMethod(defaults = "Py.None", doc = BuiltinDocs.dict_get_doc) - final PyObject dict_get(PyObject key, PyObject default_object) { + final PyObject dict_get(PyObject key, PyObject defaultObj) { PyObject o = table.get(key); - if (o == null) - return default_object; - else - return o; + return o == null ? defaultObj : o; } /** @@ -441,8 +463,8 @@ } } - private void merge(Map<Object,Object> other) { - for (Entry<Object,Object> entry : other.entrySet()) { + private void merge(Map<Object, Object> other) { + for (Entry<Object, Object> entry : other.entrySet()) { dict___setitem__(Py.java2py(entry.getKey()), Py.java2py(entry.getValue())); } } @@ -524,29 +546,21 @@ * if key does not already exist. */ public PyObject setdefault(PyObject key, PyObject failobj) { - return dict_setdefault(key,failobj); + return dict_setdefault(key, failobj); } //XXX: needs __doc__ but CPython does not define setdefault @ExposedMethod(defaults = "Py.None") final PyObject dict_setdefault(PyObject key, PyObject failobj) { PyObject oldValue = table.putIfAbsent(key, failobj); - if (oldValue == null) { - return failobj; - } else { - return oldValue; - } + return oldValue == null ? failobj : oldValue; } //XXX: needs __doc__ but CPython does not define setifabsent @ExposedMethod(defaults = "Py.None") final PyObject dict_setifabsent(PyObject key, PyObject failobj) { PyObject oldValue = table.putIfAbsent(key, failobj); - if (oldValue == null) { - return Py.None; - } else { - return oldValue; - } + return oldValue == null ? Py.None : oldValue; } @@ -589,8 +603,9 @@ @ExposedMethod(doc = BuiltinDocs.dict_popitem_doc) final PyObject dict_popitem() { Iterator<Entry<PyObject, PyObject>> it = table.entrySet().iterator(); - if (!it.hasNext()) + if (!it.hasNext()) { throw Py.KeyError("popitem(): dictionary is empty"); + } Entry<PyObject, PyObject> entry = it.next(); PyTuple tuple = new PyTuple(entry.getKey(), entry.getValue()); it.remove(); @@ -667,6 +682,7 @@ return new ValuesIter(table.values()); } + @Override public int hashCode() { return dict___hash__(); } @@ -697,6 +713,7 @@ size = values.size(); } + @Override public PyObject __iternext__() { if (table.size() != size) { throw Py.RuntimeError("dictionary changed size during iteration"); @@ -719,6 +736,7 @@ size = items.size(); } + @Override public PyObject __iternext__() { if (table.size() != size) { throw Py.RuntimeError("dictionary changed size during iteration"); @@ -821,7 +839,11 @@ /** Basic implementation of Entry that just holds onto a key and value and returns them. */ class SimpleEntry implements Entry { - public SimpleEntry(Object key, Object value){ + protected Object key; + + protected Object value; + + public SimpleEntry(Object key, Object value) { this.key = key; this.value = value; } @@ -834,8 +856,9 @@ return value; } + @Override public boolean equals(Object o) { - if(!(o instanceof Map.Entry)) { + if (!(o instanceof Map.Entry)) { return false; } Map.Entry e = (Map.Entry)o; @@ -846,10 +869,12 @@ return o1 == null ? o2 == null : o1.equals(o2); } + @Override public int hashCode() { return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); } + @Override public String toString() { return key + "=" + value; } @@ -857,10 +882,6 @@ public Object setValue(Object val) { throw new UnsupportedOperationException("Not supported by this view"); } - - protected Object key; - - protected Object value; } /** @@ -877,18 +898,23 @@ super(entry.getKey(), entry.getValue()); } + @Override public boolean equals(Object o) { - if (o == null || !(o instanceof Entry)) return false; + if (o == null || !(o instanceof Entry)) { + return false; + } Entry me = new JavaToPyMapEntry((Entry)o); return o.equals(me); } // tojava is called in getKey and getValue so the raw key and value can be // used to create a new SimpleEntry in getEntry. + @Override public Object getKey() { return PyDictionary.tojava(key); } + @Override public Object getValue() { return PyDictionary.tojava(value); } @@ -923,10 +949,12 @@ super(coll); } + @Override Object toJava(Object o) { return PyDictionary.tojava(o); } + @Override Object toPython(Object o) { return Py.java2py(o); } @@ -948,9 +976,11 @@ // We know that PyMapEntrySet will only contains entries, so if the object being passed in is // null or not an Entry, then return null which will match nothing for remove and contains // methods. + @Override Object toPython(Object o) { - if(o == null || !(o instanceof Entry)) + if (o == null || !(o instanceof Entry)) { return null; + } if (o instanceof PyToJavaMapEntry) { // Use the original entry from PyDictionary return ((PyToJavaMapEntry)o).getEntry(); @@ -959,6 +989,7 @@ } } + @Override Object toJava(Object o) { return new PyToJavaMapEntry((Entry)o); } @@ -975,6 +1006,8 @@ */ abstract class PyMapSet extends AbstractSet { + private final Collection coll; + PyMapSet(Collection coll) { this.coll = coll; } @@ -983,18 +1016,22 @@ abstract Object toPython(Object obj); + @Override public int size() { return coll.size(); } + @Override public boolean contains(Object o) { return coll.contains(toPython(o)); } + @Override public boolean remove(Object o) { return coll.remove(toPython(o)); } + @Override public void clear() { coll.clear(); } @@ -1022,9 +1059,8 @@ } } + @Override public Iterator iterator() { return new PySetIter(coll.iterator()); } - - private final Collection coll; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |