Revision: 6985
http://jython.svn.sourceforge.net/jython/?rev=6985&view=rev
Author: pjenvey
Date: 2010-03-14 21:39:57 +0000 (Sun, 14 Mar 2010)
Log Message:
-----------
coding standards/cleanup
Modified Paths:
--------------
trunk/jython/src/org/python/core/PyComplex.java
trunk/jython/src/org/python/core/PyFloat.java
trunk/jython/src/org/python/core/PyInteger.java
trunk/jython/src/org/python/core/PyLong.java
Modified: trunk/jython/src/org/python/core/PyComplex.java
===================================================================
--- trunk/jython/src/org/python/core/PyComplex.java 2010-03-13 17:33:14 UTC (rev 6984)
+++ trunk/jython/src/org/python/core/PyComplex.java 2010-03-14 21:39:57 UTC (rev 6985)
@@ -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 org.python.expose.ExposedGet;
@@ -15,14 +18,28 @@
public static final PyType TYPE = PyType.fromClass(PyComplex.class);
+ static PyComplex J = new PyComplex(0, 1.);
+
@ExposedGet(doc = BuiltinDocs.complex_real_doc)
public double real;
@ExposedGet(doc = BuiltinDocs.complex_imag_doc)
public double imag;
- static PyComplex J = new PyComplex(0, 1.);
+ public PyComplex(PyType subtype, double r, double i) {
+ super(subtype);
+ real = r;
+ imag = i;
+ }
+ public PyComplex(double r, double i) {
+ this(TYPE, r, i);
+ }
+
+ public PyComplex(double r) {
+ this(r, 0.0);
+ }
+
@ExposedNew
public static PyObject complex_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
@@ -58,7 +75,7 @@
PyComplex complexImag;
PyFloat toFloat = null;
if (real instanceof PyComplex) {
- complexReal = (PyComplex)real;
+ complexReal = (PyComplex) real;
} else {
try {
toFloat = real.__float__();
@@ -75,7 +92,7 @@
if (imag == null) {
complexImag = new PyComplex(0.0);
} else if (imag instanceof PyComplex) {
- complexImag = (PyComplex)imag;
+ complexImag = (PyComplex) imag;
} else {
toFloat = null;
try {
@@ -98,20 +115,6 @@
return complexReal;
}
- public PyComplex(PyType subtype, double r, double i) {
- super(subtype);
- real = r;
- imag = i;
- }
-
- public PyComplex(double r, double i) {
- this(TYPE, r, i);
- }
-
- public PyComplex(double r) {
- this(r, 0.0);
- }
-
public final PyFloat getReal() {
return Py.newFloat(real);
}
@@ -121,14 +124,14 @@
}
public static String toString(double value) {
- if (value == Math.floor(value) &&
- value <= Long.MAX_VALUE && value >= Long.MIN_VALUE) {
- return Long.toString((long)value);
+ if (value == Math.floor(value) && value <= Long.MAX_VALUE && value >= Long.MIN_VALUE) {
+ return Long.toString((long) value);
} else {
return Double.toString(value);
}
}
+ @Override
public String toString() {
return complex_toString();
}
@@ -136,16 +139,17 @@
@ExposedMethod(names = {"__repr__", "__str__"}, doc = BuiltinDocs.complex___str___doc)
final String complex_toString() {
if (real == 0.) {
- return toString(imag)+"j";
+ return toString(imag) + "j";
} else {
if (imag >= 0) {
- return "("+toString(real)+"+"+toString(imag)+"j)";
+ return String.format("(%s+%sj)", toString(real), toString(imag));
} else {
- return "("+toString(real)+"-"+toString(-imag)+"j)";
+ return String.format("(%s-%sj)", toString(real), toString(-imag));
}
}
}
+ @Override
public int hashCode() {
return complex___hash__();
}
@@ -155,12 +159,12 @@
if (imag == 0) {
return new PyFloat(real).hashCode();
} else {
- long v = Double.doubleToLongBits(real) ^
- Double.doubleToLongBits(imag);
- return (int)v ^ (int)(v >> 32);
+ long v = Double.doubleToLongBits(real) ^ Double.doubleToLongBits(imag);
+ return (int) v ^ (int) (v >> 32);
}
}
+ @Override
public boolean __nonzero__() {
return complex___nonzero__();
}
@@ -170,18 +174,17 @@
return real != 0 || imag != 0;
}
- /*public Object __tojava__(Class c) {
- return super.__tojava__(c);
- }*/
-
+ @Override
public int __cmp__(PyObject other) {
- if (!canCoerce(other))
+ if (!canCoerce(other)) {
return -2;
+ }
PyComplex c = coerce(other);
double oreal = c.real;
double oimag = c.imag;
- if (real == oreal && imag == oimag)
+ if (real == oreal && imag == oimag) {
return 0;
+ }
if (real != oreal) {
return real < oreal ? -1 : 1;
} else {
@@ -189,42 +192,42 @@
}
}
- /*
- * @see org.python.core.PyObject#__eq__(org.python.core.PyObject)
- */
+ @Override
public PyObject __eq__(PyObject other) {
return complex___eq__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___eq___doc)
final PyObject complex___eq__(PyObject other) {
- if (!canCoerce(other))
+ if (!canCoerce(other)) {
return null;
+ }
PyComplex c = coerce(other);
return Py.newBoolean(real == c.real && imag == c.imag);
}
- /*
- * @see org.python.core.PyObject#__ne__(org.python.core.PyObject)
- */
+ @Override
public PyObject __ne__(PyObject other) {
return complex___ne__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___ne___doc)
final PyObject complex___ne__(PyObject other) {
- if (!canCoerce(other))
+ if (!canCoerce(other)) {
return null;
+ }
PyComplex c = coerce(other);
return Py.newBoolean(real != c.real || imag != c.imag);
}
private PyObject unsupported_comparison(PyObject other) {
- if (!canCoerce(other))
+ if (!canCoerce(other)) {
return null;
+ }
throw Py.TypeError("cannot compare complex numbers using <, <=, >, >=");
}
+ @Override
public PyObject __ge__(PyObject other) {
return complex___ge__(other);
}
@@ -234,6 +237,7 @@
return unsupported_comparison(other);
}
+ @Override
public PyObject __gt__(PyObject other) {
return complex___gt__(other);
}
@@ -243,6 +247,7 @@
return unsupported_comparison(other);
}
+ @Override
public PyObject __le__(PyObject other) {
return complex___le__(other);
}
@@ -252,6 +257,7 @@
return unsupported_comparison(other);
}
+ @Override
public PyObject __lt__(PyObject other) {
return complex___lt__(other);
}
@@ -261,62 +267,72 @@
return unsupported_comparison(other);
}
+ @Override
public Object __coerce_ex__(PyObject other) {
return complex___coerce_ex__(other);
}
-
+
@ExposedMethod(doc = BuiltinDocs.complex___coerce___doc)
final PyObject complex___coerce__(PyObject other) {
return adaptToCoerceTuple(complex___coerce_ex__(other));
}
- /**
+ /**
* Coercion logic for complex. Implemented as a final method to avoid
- * invocation of virtual methods from the exposed coerce.
- */
+ * invocation of virtual methods from the exposed coerce.
+ */
final PyObject complex___coerce_ex__(PyObject other) {
- if (other instanceof PyComplex)
+ if (other instanceof PyComplex) {
return other;
- if (other instanceof PyFloat)
- return new PyComplex(((PyFloat)other).getValue(), 0);
- if (other instanceof PyInteger)
- return new PyComplex(((PyInteger)other).getValue(), 0);
- if (other instanceof PyLong)
- return new PyComplex(((PyLong)other).doubleValue(), 0);
+ }
+ if (other instanceof PyFloat) {
+ return new PyComplex(((PyFloat) other).getValue(), 0);
+ }
+ if (other instanceof PyInteger) {
+ return new PyComplex(((PyInteger) other).getValue(), 0);
+ }
+ if (other instanceof PyLong) {
+ return new PyComplex(((PyLong) other).doubleValue(), 0);
+ }
return Py.None;
}
private final boolean canCoerce(PyObject other) {
- return other instanceof PyComplex ||
- other instanceof PyFloat ||
- other instanceof PyInteger ||
- other instanceof PyLong;
+ return other instanceof PyComplex || other instanceof PyFloat || other instanceof PyInteger
+ || other instanceof PyLong;
}
private final PyComplex coerce(PyObject other) {
- if (other instanceof PyComplex)
+ if (other instanceof PyComplex) {
return (PyComplex) other;
- if (other instanceof PyFloat)
- return new PyComplex(((PyFloat)other).getValue(), 0);
- if (other instanceof PyInteger)
- return new PyComplex(((PyInteger)other).getValue(), 0);
- if (other instanceof PyLong)
- return new PyComplex(((PyLong)other).doubleValue(), 0);
+ }
+ if (other instanceof PyFloat) {
+ return new PyComplex(((PyFloat) other).getValue(), 0);
+ }
+ if (other instanceof PyInteger) {
+ return new PyComplex(((PyInteger) other).getValue(), 0);
+ }
+ if (other instanceof PyLong) {
+ return new PyComplex(((PyLong) other).doubleValue(), 0);
+ }
throw Py.TypeError("xxx");
}
+ @Override
public PyObject __add__(PyObject right) {
return complex___add__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___add___doc)
final PyObject complex___add__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
PyComplex c = coerce(right);
- return new PyComplex(real+c.real, imag+c.imag);
+ return new PyComplex(real + c.real, imag + c.imag);
}
+ @Override
public PyObject __radd__(PyObject left) {
return complex___radd__(left);
}
@@ -327,55 +343,63 @@
}
private final static PyObject _sub(PyComplex o1, PyComplex o2) {
- return new PyComplex(o1.real-o2.real, o1.imag-o2.imag);
+ return new PyComplex(o1.real - o2.real, o1.imag - o2.imag);
}
+ @Override
public PyObject __sub__(PyObject right) {
return complex___sub__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___sub___doc)
final PyObject complex___sub__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _sub(this, coerce(right));
}
+ @Override
public PyObject __rsub__(PyObject left) {
return complex___rsub__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rsub___doc)
final PyObject complex___rsub__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _sub(coerce(left), this);
}
private final static PyObject _mul(PyComplex o1, PyComplex o2) {
- return new PyComplex(o1.real*o2.real-o1.imag*o2.imag,
- o1.real*o2.imag+o1.imag*o2.real);
+ return new PyComplex(o1.real * o2.real - o1.imag * o2.imag,
+ o1.real * o2.imag + o1.imag * o2.real);
}
+ @Override
public PyObject __mul__(PyObject right) {
return complex___mul__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___mul___doc)
final PyObject complex___mul__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _mul(this, coerce(right));
}
+ @Override
public PyObject __rmul__(PyObject left) {
return complex___rmul__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rmul___doc)
final PyObject complex___rmul__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _mul(coerce(left), this);
}
@@ -400,95 +424,113 @@
}
}
+ @Override
public PyObject __div__(PyObject right) {
return complex___div__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___div___doc)
final PyObject complex___div__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
- if (Options.divisionWarning >= 2)
+ }
+ if (Options.divisionWarning >= 2) {
Py.warning(Py.DeprecationWarning, "classic complex division");
+ }
return _div(this, coerce(right));
}
+ @Override
public PyObject __rdiv__(PyObject left) {
return complex___rdiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rdiv___doc)
final PyObject complex___rdiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
- if (Options.divisionWarning >= 2)
+ }
+ if (Options.divisionWarning >= 2) {
Py.warning(Py.DeprecationWarning, "classic complex division");
+ }
return _div(coerce(left), this);
}
+ @Override
public PyObject __floordiv__(PyObject right) {
return complex___floordiv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___floordiv___doc)
final PyObject complex___floordiv__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _divmod(this, coerce(right)).__finditem__(0);
}
+ @Override
public PyObject __rfloordiv__(PyObject left) {
return complex___rfloordiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rfloordiv___doc)
final PyObject complex___rfloordiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _divmod(coerce(left), this).__finditem__(0);
}
+ @Override
public PyObject __truediv__(PyObject right) {
return complex___truediv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___truediv___doc)
final PyObject complex___truediv__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _div(this, coerce(right));
}
+ @Override
public PyObject __rtruediv__(PyObject left) {
return complex___rtruediv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rtruediv___doc)
final PyObject complex___rtruediv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _div(coerce(left), this);
}
+ @Override
public PyObject __mod__(PyObject right) {
return complex___mod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___mod___doc)
final PyObject complex___mod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _mod(this, coerce(right));
}
+ @Override
public PyObject __rmod__(PyObject left) {
return complex___rmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rmod___doc)
final PyObject complex___rmod__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _mod(coerce(left), this);
}
@@ -502,25 +544,29 @@
return value.__sub__(z.__mul__(right));
}
+ @Override
public PyObject __divmod__(PyObject right) {
return complex___divmod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___divmod___doc)
final PyObject complex___divmod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _divmod(this, coerce(right));
}
+ @Override
public PyObject __rdivmod__(PyObject left) {
return complex___rdivmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rdivmod___doc)
final PyObject complex___rdivmod__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _divmod(coerce(left), this);
}
@@ -534,10 +580,11 @@
return new PyTuple(z, value.__sub__(z.__mul__(right)));
}
-
private static PyObject ipow(PyComplex value, int iexp) {
int pow = iexp;
- if (pow < 0) pow = -pow;
+ if (pow < 0) {
+ pow = -pow;
+ }
double xr = value.real;
double xi = value.imag;
@@ -549,25 +596,28 @@
while (pow > 0) {
if ((pow & 0x1) != 0) {
- tmp = zr*xr - zi*xi;
- zi = zi*xr + zr*xi;
+ tmp = zr * xr - zi * xi;
+ zi = zi * xr + zr * xi;
zr = tmp;
}
pow >>= 1;
- if (pow == 0)
+ if (pow == 0) {
break;
- tmp = xr*xr - xi*xi;
- xi = xr*xi*2;
+ }
+ tmp = xr * xr - xi * xi;
+ xi = xr * xi * 2;
xr = tmp;
}
PyComplex ret = new PyComplex(zr, zi);
- if (iexp < 0)
- return new PyComplex(1,0).__div__(ret);
+ if (iexp < 0) {
+ return new PyComplex(1, 0).__div__(ret);
+ }
return ret;
}
+ @Override
public PyObject __pow__(PyObject right, PyObject modulo) {
return complex___pow__(right, modulo);
}
@@ -577,19 +627,22 @@
if (modulo != null) {
throw Py.ValueError("complex modulo");
}
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _pow(this, coerce(right));
}
+ @Override
public PyObject __rpow__(PyObject left) {
return complex___rpow__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rpow___doc)
final PyObject complex___rpow__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _pow(coerce(left), this);
}
@@ -610,7 +663,7 @@
}
// Check for integral powers
- int iexp = (int)yr;
+ int iexp = (int) yr;
if (yi == 0 && yr == iexp && iexp >= -128 && iexp <= 128) {
return ipow(value, iexp);
}
@@ -619,14 +672,15 @@
double len = Math.pow(abs, yr);
double at = Math.atan2(xi, xr);
- double phase = at*yr;
+ double phase = at * yr;
if (yi != 0) {
- len /= Math.exp(at*yi);
- phase += yi*Math.log(abs);
+ len /= Math.exp(at * yi);
+ phase += yi * Math.log(abs);
}
- return new PyComplex(len*Math.cos(phase), len*Math.sin(phase));
+ return new PyComplex(len * Math.cos(phase), len * Math.sin(phase));
}
+ @Override
public PyObject __neg__() {
return complex___neg__();
}
@@ -636,6 +690,7 @@
return new PyComplex(-real, -imag);
}
+ @Override
public PyObject __pos__() {
return complex___pos__();
}
@@ -648,10 +703,12 @@
return new PyComplex(real, imag);
}
+ @Override
public PyObject __invert__() {
- throw Py.TypeError("bad operand type for unary ~");
+ throw Py.TypeError("bad operand type for unary ~");
}
+ @Override
public PyObject __abs__() {
return complex___abs__();
}
@@ -661,26 +718,27 @@
return new PyFloat(Math.hypot(real, imag));
}
+ @Override
public PyObject __int__() {
return complex___int__();
}
@ExposedMethod(doc = BuiltinDocs.complex___int___doc)
final PyInteger complex___int__() {
- throw Py.TypeError(
- "can't convert complex to int; use e.g. int(abs(z))");
+ throw Py.TypeError("can't convert complex to int; use e.g. int(abs(z))");
}
+ @Override
public PyObject __long__() {
return complex___long__();
}
@ExposedMethod(doc = BuiltinDocs.complex___long___doc)
final PyObject complex___long__() {
- throw Py.TypeError(
- "can't convert complex to long; use e.g. long(abs(z))");
+ throw Py.TypeError("can't convert complex to long; use e.g. long(abs(z))");
}
+ @Override
public PyFloat __float__() {
return complex___float__();
}
@@ -690,6 +748,7 @@
throw Py.TypeError("can't convert complex to float; use e.g. abs(z)");
}
+ @Override
public PyComplex __complex__() {
return new PyComplex(real, imag);
}
@@ -708,6 +767,7 @@
return new PyTuple(new PyComplex(real, imag));
}
+ @Override
public PyTuple __getnewargs__() {
return complex___getnewargs__();
}
Modified: trunk/jython/src/org/python/core/PyFloat.java
===================================================================
--- trunk/jython/src/org/python/core/PyFloat.java 2010-03-13 17:33:14 UTC (rev 6984)
+++ trunk/jython/src/org/python/core/PyFloat.java 2010-03-14 21:39:57 UTC (rev 6985)
@@ -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.io.Serializable;
@@ -14,16 +17,33 @@
* A builtin python float.
*/
@ExposedType(name = "float", doc = BuiltinDocs.float_doc)
-public class PyFloat extends PyObject
-{
+public class PyFloat extends PyObject {
+
+ public static final PyType TYPE = PyType.fromClass(PyFloat.class);
+
/** Precisions used by repr() and str(), respectively. */
private static final int PREC_REPR = 17;
private static final int PREC_STR = 12;
+ private double value;
+
+ public PyFloat(PyType subtype, double v) {
+ super(subtype);
+ value = v;
+ }
+
+ public PyFloat(double v) {
+ this(TYPE, v);
+ }
+
+ public PyFloat(float v) {
+ this((double) v);
+ }
+
@ExposedNew
public static PyObject float_new(PyNewWrapper new_, boolean init, PyType subtype,
- PyObject[] args, String[] keywords) {
- ArgParser ap = new ArgParser("float", args, keywords, new String[] { "x" }, 0);
+ PyObject[] args, String[] keywords) {
+ ArgParser ap = new ArgParser("float", args, keywords, new String[] {"x"}, 0);
PyObject x = ap.getPyObject(0, null);
if (x == null) {
if (new_.for_type == subtype) {
@@ -54,23 +74,6 @@
}
}
- public static final PyType TYPE = PyType.fromClass(PyFloat.class);
-
- private double value;
-
- public PyFloat(PyType subtype, double v) {
- super(subtype);
- value = v;
- }
-
- public PyFloat(double v) {
- this(TYPE, v);
- }
-
- public PyFloat(float v) {
- this((double)v);
- }
-
/**
* Determine if this float is not infinity, nor NaN.
*/
@@ -82,10 +85,12 @@
return value;
}
+ @Override
public String toString() {
return __str__().toString();
}
+ @Override
public PyString __str__() {
return float___str__();
}
@@ -95,6 +100,7 @@
return Py.newString(formatDouble(PREC_STR));
}
+ @Override
public PyString __repr__() {
return float___repr__();
}
@@ -108,7 +114,7 @@
if (Double.isNaN(value)) {
return "nan";
}
-
+
String result = String.format("%%.%dg", precision);
result = Py.newString(result).__mod__(this).toString();
@@ -127,6 +133,7 @@
return result;
}
+ @Override
public int hashCode() {
return float___hash__();
}
@@ -134,19 +141,21 @@
@ExposedMethod(doc = BuiltinDocs.float___hash___doc)
final int float___hash__() {
double intPart = Math.floor(value);
- double fractPart = value-intPart;
+ double fractPart = value - intPart;
if (fractPart == 0) {
- if (intPart <= Integer.MAX_VALUE && intPart >= Integer.MIN_VALUE)
- return (int)value;
- else
+ if (intPart <= Integer.MAX_VALUE && intPart >= Integer.MIN_VALUE) {
+ return (int) value;
+ } else {
return __long__().hashCode();
+ }
} else {
long v = Double.doubleToLongBits(value);
- return (int)v ^ (int)(v >> 32);
+ return (int) v ^ (int) (v >> 32);
}
}
+ @Override
public boolean __nonzero__() {
return float___nonzero__();
}
@@ -156,10 +165,10 @@
return value != 0;
}
- public Object __tojava__(Class c) {
- if (c == Double.TYPE || c == Number.class ||
- c == Double.class || c == Object.class || c == Serializable.class)
- {
+ @Override
+ public Object __tojava__(Class<?> c) {
+ if (c == Double.TYPE || c == Number.class || c == Double.class || c == Object.class
+ || c == Serializable.class) {
return new Double(value);
}
if (c == Float.TYPE || c == Float.class) {
@@ -168,6 +177,7 @@
return super.__tojava__(c);
}
+ @Override
public PyObject __eq__(PyObject other) {
// preclude _cmp_unsafe's this == other shortcut because NaN != anything, even
// itself
@@ -177,6 +187,7 @@
return null;
}
+ @Override
public PyObject __ne__(PyObject other) {
if (Double.isNaN(value)) {
return Py.True;
@@ -184,18 +195,19 @@
return null;
}
+ @Override
public int __cmp__(PyObject other) {
return float___cmp__(other);
}
- //XXX: needs __doc__
+ // XXX: needs __doc__
@ExposedMethod(type = MethodType.CMP)
final int float___cmp__(PyObject other) {
double i = value;
double j;
if (other instanceof PyFloat) {
- j = ((PyFloat)other).value;
+ j = ((PyFloat) other).value;
} else if (!isFinite()) {
// we're infinity: our magnitude exceeds any finite
// integer, so it doesn't matter which int we compare i
@@ -206,10 +218,10 @@
return -2;
}
} else if (other instanceof PyInteger) {
- j = ((PyInteger)other).getValue();
+ j = ((PyInteger) other).getValue();
} else if (other instanceof PyLong) {
BigDecimal v = new BigDecimal(value);
- BigDecimal w = new BigDecimal(((PyLong)other).getValue());
+ BigDecimal w = new BigDecimal(((PyLong) other).getValue());
return v.compareTo(w);
} else {
return -2;
@@ -227,6 +239,7 @@
}
}
+ @Override
public Object __coerce_ex__(PyObject other) {
return float___coerce_ex__(other);
}
@@ -236,22 +249,23 @@
return adaptToCoerceTuple(float___coerce_ex__(other));
}
- /**
+ /**
* Coercion logic for float. Implemented as a final method to avoid
- * invocation of virtual methods from the exposed coerce.
- */
+ * invocation of virtual methods from the exposed coerce.
+ */
final Object float___coerce_ex__(PyObject other) {
- if (other instanceof PyFloat)
+ if (other instanceof PyFloat) {
return other;
- else {
- if (other instanceof PyInteger)
- return new PyFloat((double)((PyInteger)other).getValue());
- if (other instanceof PyLong)
- return new PyFloat(((PyLong)other).doubleValue());
- else
+ } else {
+ if (other instanceof PyInteger) {
+ return new PyFloat((double) ((PyInteger) other).getValue());
+ }
+ if (other instanceof PyLong) {
+ return new PyFloat(((PyLong) other).doubleValue());
+ } else {
return Py.None;
+ }
}
-
}
private static final boolean canCoerce(PyObject other) {
@@ -259,28 +273,32 @@
}
private static final double coerce(PyObject other) {
- if (other instanceof PyFloat)
+ if (other instanceof PyFloat) {
return ((PyFloat) other).value;
- else if (other instanceof PyInteger)
+ } else if (other instanceof PyInteger) {
return ((PyInteger) other).getValue();
- else if (other instanceof PyLong)
+ } else if (other instanceof PyLong) {
return ((PyLong) other).doubleValue();
- else
+ } else {
throw Py.TypeError("xxx");
+ }
}
+ @Override
public PyObject __add__(PyObject right) {
return float___add__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___add___doc)
final PyObject float___add__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
double rightv = coerce(right);
return new PyFloat(value + rightv);
}
+ @Override
public PyObject __radd__(PyObject left) {
return float___radd__(left);
}
@@ -290,42 +308,49 @@
return __add__(left);
}
+ @Override
public PyObject __sub__(PyObject right) {
return float___sub__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___sub___doc)
final PyObject float___sub__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
double rightv = coerce(right);
return new PyFloat(value - rightv);
}
+ @Override
public PyObject __rsub__(PyObject left) {
return float___rsub__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rsub___doc)
final PyObject float___rsub__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
double leftv = coerce(left);
return new PyFloat(leftv - value);
}
+ @Override
public PyObject __mul__(PyObject right) {
return float___mul__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___mul___doc)
final PyObject float___mul__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
double rightv = coerce(right);
return new PyFloat(value * rightv);
}
+ @Override
public PyObject __rmul__(PyObject left) {
return float___rmul__(left);
}
@@ -335,151 +360,185 @@
return __mul__(left);
}
+ @Override
public PyObject __div__(PyObject right) {
return float___div__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___div___doc)
final PyObject float___div__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
- if (Options.divisionWarning >= 2)
+ }
+ if (Options.divisionWarning >= 2) {
Py.warning(Py.DeprecationWarning, "classic float division");
+ }
+
double rightv = coerce(right);
- if (rightv == 0)
+ if (rightv == 0) {
throw Py.ZeroDivisionError("float division");
+ }
return new PyFloat(value / rightv);
}
+ @Override
public PyObject __rdiv__(PyObject left) {
return float___rdiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rdiv___doc)
final PyObject float___rdiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
- if (Options.divisionWarning >= 2)
+ }
+ if (Options.divisionWarning >= 2) {
Py.warning(Py.DeprecationWarning, "classic float division");
+ }
+
double leftv = coerce(left);
- if (value == 0)
+ if (value == 0) {
throw Py.ZeroDivisionError("float division");
+ }
return new PyFloat(leftv / value);
}
+ @Override
public PyObject __floordiv__(PyObject right) {
return float___floordiv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___floordiv___doc)
final PyObject float___floordiv__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
double rightv = coerce(right);
- if (rightv == 0)
+ if (rightv == 0) {
throw Py.ZeroDivisionError("float division");
+ }
return new PyFloat(Math.floor(value / rightv));
}
+ @Override
public PyObject __rfloordiv__(PyObject left) {
return float___rfloordiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rfloordiv___doc)
final PyObject float___rfloordiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
double leftv = coerce(left);
- if (value == 0)
+ if (value == 0) {
throw Py.ZeroDivisionError("float division");
+ }
return new PyFloat(Math.floor(leftv / value));
}
+ @Override
public PyObject __truediv__(PyObject right) {
return float___truediv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___truediv___doc)
final PyObject float___truediv__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
double rightv = coerce(right);
- if (rightv == 0)
+ if (rightv == 0) {
throw Py.ZeroDivisionError("float division");
+ }
return new PyFloat(value / rightv);
}
+ @Override
public PyObject __rtruediv__(PyObject left) {
return float___rtruediv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rtruediv___doc)
final PyObject float___rtruediv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
double leftv = coerce(left);
- if (value == 0)
+ if (value == 0) {
throw Py.ZeroDivisionError("float division");
+ }
return new PyFloat(leftv / value);
}
private static double modulo(double x, double y) {
- if (y == 0)
+ if (y == 0) {
throw Py.ZeroDivisionError("float modulo");
+ }
double z = Math.IEEEremainder(x, y);
- if (z*y < 0)
+ if (z * y < 0) {
z += y;
+ }
return z;
}
+ @Override
public PyObject __mod__(PyObject right) {
return float___mod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___mod___doc)
final PyObject float___mod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
double rightv = coerce(right);
return new PyFloat(modulo(value, rightv));
}
+ @Override
public PyObject __rmod__(PyObject left) {
return float___rmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rmod___doc)
final PyObject float___rmod__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
double leftv = coerce(left);
return new PyFloat(modulo(leftv, value));
}
+ @Override
public PyObject __divmod__(PyObject right) {
return float___divmod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___divmod___doc)
final PyObject float___divmod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
double rightv = coerce(right);
- if (rightv == 0)
+ if (rightv == 0) {
throw Py.ZeroDivisionError("float division");
+ }
double z = Math.floor(value / rightv);
return new PyTuple(new PyFloat(z), new PyFloat(value - z * rightv));
}
+ @Override
public PyObject __rdivmod__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
double leftv = coerce(left);
- if (value == 0)
+ if (value == 0) {
throw Py.ZeroDivisionError("float division");
+ }
double z = Math.floor(leftv / value);
return new PyTuple(new PyFloat(z), new PyFloat(leftv - z * value));
@@ -487,22 +546,23 @@
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rdivmod___doc)
final PyObject float___rdivmod__(PyObject left) {
- return __rdivmod__(left);
+ return __rdivmod__(left);
}
-
+ @Override
public PyObject __pow__(PyObject right, PyObject modulo) {
return float___pow__(right, modulo);
}
- @ExposedMethod(type = MethodType.BINARY, defaults = "null", doc = BuiltinDocs.float___pow___doc)
+ @ExposedMethod(type = MethodType.BINARY, defaults = "null",
+ doc = BuiltinDocs.float___pow___doc)
final PyObject float___pow__(PyObject right, PyObject modulo) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
if (modulo != null) {
- throw Py.TypeError("pow() 3rd argument not allowed " +
- "unless all arguments are integers");
+ throw Py.TypeError("pow() 3rd argument not allowed unless all arguments are integers");
}
return _pow(value, coerce(right), modulo);
@@ -510,12 +570,14 @@
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rpow___doc)
final PyObject float___rpow__(PyObject left) {
- return __rpow__(left);
+ return __rpow__(left);
}
-
+
+ @Override
public PyObject __rpow__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _pow(coerce(left), value, null);
}
@@ -523,28 +585,27 @@
private static PyFloat _pow(double value, double iw, PyObject modulo) {
// Rely completely on Java's pow function
if (iw == 0) {
- if (modulo != null)
+ if (modulo != null) {
return new PyFloat(modulo(1.0, coerce(modulo)));
+ }
return new PyFloat(1.0);
}
if (value == 0.0) {
- if (iw < 0.0)
- throw Py.ZeroDivisionError("0.0 cannot be raised to a " +
- "negative power");
+ if (iw < 0.0) {
+ throw Py.ZeroDivisionError("0.0 cannot be raised to a negative power");
+ }
return new PyFloat(0);
}
- if (value < 0 && iw != Math.floor(iw))
+ if (value < 0 && iw != Math.floor(iw)) {
throw Py.ValueError("negative number cannot be raised to a fractional power");
-
+ }
+
double ret = Math.pow(value, iw);
- if (modulo == null) {
- return new PyFloat(ret);
- } else {
- return new PyFloat(modulo(ret, coerce(modulo)));
- }
+ return new PyFloat(modulo == null ? ret : modulo(ret, coerce(modulo)));
}
+ @Override
public PyObject __neg__() {
return float___neg__();
}
@@ -554,6 +615,7 @@
return new PyFloat(-value);
}
+ @Override
public PyObject __pos__() {
return float___pos__();
}
@@ -563,10 +625,12 @@
return float___float__();
}
+ @Override
public PyObject __invert__() {
throw Py.TypeError("bad operand type for unary ~");
}
+ @Override
public PyObject __abs__() {
return float___abs__();
}
@@ -579,6 +643,7 @@
return float___float__();
}
+ @Override
public PyObject __int__() {
return float___int__();
}
@@ -586,11 +651,12 @@
@ExposedMethod(doc = BuiltinDocs.float___int___doc)
final PyObject float___int__() {
if (value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE) {
- return new PyInteger((int)value);
+ return new PyInteger((int) value);
}
return __long__();
}
+ @Override
public PyObject __long__() {
return float___long__();
}
@@ -600,6 +666,7 @@
return new PyLong(value);
}
+ @Override
public PyFloat __float__() {
return float___float__();
}
@@ -611,6 +678,8 @@
}
return Py.newFloat(value);
}
+
+ @Override
public PyComplex __complex__() {
return new PyComplex(value, 0.);
}
@@ -620,10 +689,12 @@
return new PyTuple(new PyObject[] {new PyFloat(getValue())});
}
+ @Override
public PyTuple __getnewargs__() {
return float___getnewargs__();
}
+ @Override
public double asDouble() {
return value;
}
@@ -635,23 +706,26 @@
// standard singleton issues apply here to __getformat__/__setformat__,
// but this is what Python demands
-
public enum Format {
- UNKNOWN ("unknown"),
- BE ("IEEE, big-endian"),
- LE ("IEEE, little-endian");
-
+
+ UNKNOWN("unknown"),
+ BE("IEEE, big-endian"),
+ LE("IEEE, little-endian");
+
private final String format;
+
Format(String format) {
this.format = format;
}
- public String format() { return format; }
+
+ public String format() {
+ return format;
+ }
}
-
- // subset of IEEE-754, the JVM is big-endian
+ // subset of IEEE-754, the JVM is big-endian
public static volatile Format double_format = Format.BE;
public static volatile Format float_format = Format.BE;
-
+
@ExposedClassMethod(doc = BuiltinDocs.float___getformat___doc)
public static String float___getformat__(PyType type, String typestr) {
if ("double".equals(typestr)) {
@@ -662,7 +736,7 @@
throw Py.ValueError("__getformat__() argument 1 must be 'double' or 'float'");
}
}
-
+
@ExposedClassMethod(doc = BuiltinDocs.float___setformat___doc)
public static void float___setformat__(PyType type, String typestr, String format) {
Format new_format = null;
@@ -670,13 +744,15 @@
throw Py.ValueError("__setformat__() argument 1 must be 'double' or 'float'");
}
if (Format.LE.format().equals(format)) {
- throw Py.ValueError(String.format("can only set %s format to 'unknown' or the " + "detected platform value", typestr));
+ throw Py.ValueError(String.format("can only set %s format to 'unknown' or the "
+ + "detected platform value", typestr));
} else if (Format.BE.format().equals(format)) {
new_format = Format.BE;
} else if (Format.UNKNOWN.format().equals(format)) {
new_format = Format.UNKNOWN;
} else {
- throw Py.ValueError("__setformat__() argument 2 must be 'unknown', " + "'IEEE, little-endian' or 'IEEE, big-endian'");
+ throw Py.ValueError("__setformat__() argument 2 must be 'unknown', " +
+ "'IEEE, little-endian' or 'IEEE, big-endian'");
}
if (new_format != null) {
if ("double".equals(typestr)) {
Modified: trunk/jython/src/org/python/core/PyInteger.java
===================================================================
--- trunk/jython/src/org/python/core/PyInteger.java 2010-03-13 17:33:14 UTC (rev 6984)
+++ trunk/jython/src/org/python/core/PyInteger.java 2010-03-14 21:39:57 UTC (rev 6985)
@@ -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.io.Serializable;
@@ -14,7 +17,7 @@
*/
@ExposedType(name = "int", doc = BuiltinDocs.int_doc)
public class PyInteger extends PyObject {
-
+
public static final PyType TYPE = PyType.fromClass(PyInteger.class);
/** The minimum value of an int represented by a BigInteger */
@@ -23,31 +26,41 @@
/** The maximum value of an int represented by a BigInteger */
public static final BigInteger maxInt = BigInteger.valueOf(Integer.MAX_VALUE);
+ private int value;
+
+ public PyInteger(PyType subType, int v) {
+ super(subType);
+ value = v;
+ }
+
+ public PyInteger(int v) {
+ this(TYPE, v);
+ }
+
@ExposedNew
public static PyObject int_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
- ArgParser ap = new ArgParser("int", args, keywords, new String[] { "x",
- "base" }, 0);
+ ArgParser ap = new ArgParser("int", args, keywords, new String[] {"x", "base"}, 0);
PyObject x = ap.getPyObject(0, null);
int base = ap.getInt(1, -909);
if (new_.for_type == subtype) {
if (x == null) {
return Py.Zero;
}
- if (base == -909) {
- if (x instanceof PyBoolean) {
- return (coerce(x) == 0) ? Py.Zero : Py.One;
- }
- return asPyInteger(x);
- }
- if (!(x instanceof PyString)) {
- throw Py.TypeError("int: can't convert non-string with explicit base");
- }
+ if (base == -909) {
+ if (x instanceof PyBoolean) {
+ return (coerce(x) == 0) ? Py.Zero : Py.One;
+ }
+ return asPyInteger(x);
+ }
+ if (!(x instanceof PyString)) {
+ throw Py.TypeError("int: can't convert non-string with explicit base");
+ }
try {
- return Py.newInteger(((PyString)x).atoi(base));
+ return Py.newInteger(((PyString) x).atoi(base));
} catch (PyException pye) {
if (pye.match(Py.OverflowError)) {
- return ((PyString)x).atol(base);
+ return ((PyString) x).atol(base);
}
throw pye;
}
@@ -56,60 +69,51 @@
return new PyIntegerDerived(subtype, 0);
}
if (base == -909) {
- PyObject intOrLong = asPyInteger(x);
- if (intOrLong instanceof PyInteger) {
- return new PyIntegerDerived(subtype, ((PyInteger) intOrLong).getValue());
- } else {
- throw Py.OverflowError("long int too large to convert to int");
- }
+ PyObject intOrLong = asPyInteger(x);
+ if (intOrLong instanceof PyInteger) {
+ return new PyIntegerDerived(subtype, ((PyInteger) intOrLong).getValue());
+ } else {
+ throw Py.OverflowError("long int too large to convert to int");
+ }
}
if (!(x instanceof PyString)) {
- throw Py
- .TypeError("int: can't convert non-string with explicit base");
+ throw Py.TypeError("int: can't convert non-string with explicit base");
}
return new PyIntegerDerived(subtype, ((PyString) x).atoi(base));
}
} // xxx
/**
- * @return the result of x.__int__
+ * @return the result of x.__int__
* @throws Py.Type error if x.__int__ throws an Py.AttributeError
*/
- private static PyObject asPyInteger(PyObject x) {
- try {
- return x.__int__();
- } catch (PyException pye) {
- if (!pye.match(Py.AttributeError))
- throw pye;
- throw Py.TypeError("int() argument must be a string or a number");
- }
- }
-
- private int value;
-
- public PyInteger(PyType subType, int v) {
- super(subType);
- value = v;
+ private static PyObject asPyInteger(PyObject x) {
+ try {
+ return x.__int__();
+ } catch (PyException pye) {
+ if (!pye.match(Py.AttributeError)) {
+ throw pye;
+ }
+ throw Py.TypeError("int() argument must be a string or a number");
+ }
}
- public PyInteger(int v) {
- this(TYPE, v);
- }
-
public int getValue() {
return value;
}
+ @Override
public String toString() {
return int_toString();
}
- //XXX: need separate __doc__ for __repr__
+ // XXX: need separate __doc__ for __repr__
@ExposedMethod(names = {"__str__", "__repr__"}, doc = BuiltinDocs.int___str___doc)
final String int_toString() {
return Integer.toString(getValue());
}
+ @Override
public int hashCode() {
return int_hashCode();
}
@@ -119,6 +123,7 @@
return getValue();
}
+ @Override
public boolean __nonzero__() {
return int___nonzero__();
}
@@ -128,42 +133,50 @@
return getValue() != 0;
}
+ @Override
public Object __tojava__(Class<?> c) {
- if (c == Integer.TYPE || c == Number.class ||
- c == Object.class || c == Integer.class ||
- c == Serializable.class)
- {
+ if (c == Integer.TYPE || c == Number.class || c == Object.class || c == Integer.class
+ || c == Serializable.class) {
return new Integer(getValue());
}
- if (c == Boolean.TYPE || c == Boolean.class)
+ if (c == Boolean.TYPE || c == Boolean.class) {
return new Boolean(getValue() != 0);
- if (c == Byte.TYPE || c == Byte.class)
- return new Byte((byte)getValue());
- if (c == Short.TYPE || c == Short.class)
- return new Short((short)getValue());
+ }
+ if (c == Byte.TYPE || c == Byte.class) {
+ return new Byte((byte) getValue());
+ }
+ if (c == Short.TYPE || c == Short.class) {
+ return new Short((short) getValue());
+ }
- if (c == Long.TYPE || c == Long.class)
+ if (c == Long.TYPE || c == Long.class) {
return new Long(getValue());
- if (c == Float.TYPE || c == Float.class)
+ }
+ if (c == Float.TYPE || c == Float.class) {
return new Float(getValue());
- if (c == Double.TYPE || c == Double.class)
+ }
+ if (c == Double.TYPE || c == Double.class) {
return new Double(getValue());
+ }
return super.__tojava__(c);
}
+ @Override
public int __cmp__(PyObject other) {
return int___cmp__(other);
}
@ExposedMethod(type = MethodType.CMP, doc = BuiltinDocs.int___cmp___doc)
final int int___cmp__(PyObject other) {
- if (!canCoerce(other))
- return -2;
+ if (!canCoerce(other)) {
+ return -2;
+ }
int v = coerce(other);
return getValue() < v ? -1 : getValue() > v ? 1 : 0;
}
+ @Override
public Object __coerce_ex__(PyObject other) {
return int___coerce_ex__(other);
}
@@ -173,15 +186,12 @@
return adaptToCoerceTuple(int___coerce_ex__(other));
}
- /**
+ /**
* Coercion logic for int. Implemented as a final method to avoid
- * invocation of virtual methods from the exposed coerced.
- */
+ * invocation of virtual methods from the exposed coerced.
+ */
final Object int___coerce_ex__(PyObject other) {
- if (other instanceof PyInteger)
- return other;
- else
- return Py.None;
+ return other instanceof PyInteger ? other : Py.None;
}
private static final boolean canCoerce(PyObject other) {
@@ -189,30 +199,33 @@
}
private static final int coerce(PyObject other) {
- if (other instanceof PyInteger)
+ if (other instanceof PyInteger) {
return ((PyInteger) other).getValue();
- else
- throw Py.TypeError("xxx");
+ }
+ throw Py.TypeError("xxx");
}
-
+ @Override
public PyObject __add__(PyObject right) {
return int___add__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___add___doc)
final PyObject int___add__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
int rightv = coerce(right);
int a = getValue();
int b = rightv;
int x = a + b;
- if ((x^a) >= 0 || (x^b) >= 0)
+ if ((x ^ a) >= 0 || (x ^ b) >= 0) {
return Py.newInteger(x);
- return new PyLong((long) a + (long)b);
+ }
+ return new PyLong((long) a + (long) b);
}
+ @Override
public PyObject __radd__(PyObject left) {
return int___radd__(left);
}
@@ -224,57 +237,64 @@
private static PyObject _sub(int a, int b) {
int x = a - b;
- if ((x^a) >= 0 || (x^~b) >= 0)
+ if ((x ^ a) >= 0 || (x ^ ~b) >= 0) {
return Py.newInteger(x);
- return new PyLong((long) a - (long)b);
+ }
+ return new PyLong((long) a - (long) b);
}
+ @Override
public PyObject __sub__(PyObject right) {
return int___sub__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___sub___doc)
final PyObject int___sub__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return _sub(getValue(), coerce(right));
}
+ @Override
public PyObject __rsub__(PyObject left) {
return int___rsub__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rsub___doc)
final PyObject int___rsub__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _sub(coerce(left), getValue());
}
+ @Override
public PyObject __mul__(PyObject right) {
return int___mul__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___mul___doc)
final PyObject int___mul__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
int rightv = coerce(right);
double x = getValue();
x *= rightv;
- //long x = ((long)getValue())*((PyInteger)right).getValue();
- //System.out.println("mul: "+this+" * "+right+" = "+x);
- if (x <= Integer.MAX_VALUE && x >= Integer.MIN_VALUE)
- return Py.newInteger((int)x);
+ if (x <= Integer.MAX_VALUE && x >= Integer.MIN_VALUE) {
+ return Py.newInteger((int) x);
+ }
return __long__().__mul__(right);
}
+ @Override
public PyObject __rmul__(PyObject left) {
return int___rmul__(left);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rmul___doc)
final PyObject int___rmul__(PyObject left) {
return __mul__(left);
@@ -288,13 +308,11 @@
}
long xdivy = x / y;
long xmody = x - xdivy * y;
-
- /* If the signs of x and y differ, and the remainder is non-0,
- * C89 doesn't define whether xdivy is now the floor or the
- * ceiling of the infinitely precise quotient. We want the floor,
- * and we have it iff the remainder's sign matches y's.
- */
-
+
+ // If the signs of x and y differ, and the remainder is non-0, C89 doesn't define
+ // whether xdivy is now the floor or the ceiling of the infinitely precise
+ // quotient. We want the floor, and we have it iff the remainder's sign matches
+ // y's.
if (xmody != 0 && ((y < 0 && xmody > 0) || (y > 0 && xmody < 0))) {
xmody += y;
--xdivy;
@@ -307,128 +325,148 @@
public PyObject __div__(PyObject right) {
return int___div__(right);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___div___doc)
final PyObject int___div__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
- if (Options.divisionWarning > 0)
+ }
+ if (Options.divisionWarning > 0) {
Py.warning(Py.DeprecationWarning, "classic int division");
+ }
return Py.newInteger(divide(getValue(), coerce(right)));
}
+ @Override
public PyObject __rdiv__(PyObject left) {
return int___rdiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rdiv___doc)
final PyObject int___rdiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
- if (Options.divisionWarning > 0)
+ }
+ if (Options.divisionWarning > 0) {
Py.warning(Py.DeprecationWarning, "classic int division");
+ }
return Py.newInteger(divide(coerce(left), getValue()));
}
+ @Override
public PyObject __floordiv__(PyObject right) {
return int___floordiv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___floordiv___doc)
final PyObject int___floordiv__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return Py.newInteger(divide(getValue(), coerce(right)));
}
+ @Override
public PyObject __rfloordiv__(PyObject left) {
return int___rfloordiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rfloordiv___doc)
final PyObject int___rfloordiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return Py.newInteger(divide(coerce(left), getValue()));
}
+ @Override
public PyObject __truediv__(PyObject right) {
return int___truediv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___truediv___doc)
final PyObject int___truediv__(PyObject right) {
- if (right instanceof PyInteger)
+ if (right instanceof PyInteger) {
return __float__().__truediv__(right);
- else if(right instanceof PyLong)
+ } else if (right instanceof PyLong) {
return int___long__().__truediv__(right);
- else
+ } else {
return null;
+ }
}
+ @Override
public PyObject __rtruediv__(PyObject left) {
return int___rtruediv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rtruediv___doc)
final PyObject int___rtruediv__(PyObject left) {
- if (left instanceof PyInteger)
+ if (left instanceof PyInteger) {
return left.__float__().__truediv__(this);
- else if(left instanceof PyLong)
+ } else if (left instanceof PyLong) {
return left.__truediv__(int___long__());
- else
+ } else {
return null;
+ }
}
private static long modulo(long x, long y, long xdivy) {
- return x - xdivy*y;
+ return x - xdivy * y;
}
+ @Override
public PyObject __mod__(PyObject right) {
return int___mod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___mod___doc)
final PyObject int___mod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
int rightv = coerce(right);
int v = getValue();
return Py.newInteger(modulo(v, rightv, divide(v, rightv)));
}
+ @Override
public PyObject __rmod__(PyObject left) {
return int___rmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rmod___doc)
final PyObject int___rmod__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
int leftv = coerce(left);
int v = getValue();
return Py.newInteger(modulo(leftv, v, divide(leftv, v)));
}
+ @Override
public PyObject __divmod__(PyObject right) {
return int___divmod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___divmod___doc)
final PyObject int___divmod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
int rightv = coerce(right);
int v = getValue();
long xdivy = divide(v, rightv);
return new PyTuple(Py.newInteger(xdivy), Py.newInteger(modulo(v, rightv, xdivy)));
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rdivmod___doc)
- final PyObject int___rdivmod__(PyObject left){
- if (!canCoerce(left))
+ final PyObject int___rdivmod__(PyObject left) {
+ if (!canCoerce(left)) {
return null;
+ }
int leftv = coerce(left);
int v = getValue();
@@ -436,37 +474,44 @@
return new PyTuple(Py.newInteger(xdivy), Py.newInteger(modulo(leftv, v, xdivy)));
}
+ @Override
public PyObject __pow__(PyObject right, PyObject modulo) {
- return int___pow__(right,modulo);
+ return int___pow__(right, modulo);
}
- @ExposedMethod(type = MethodType.BINARY, defaults = {"null"}, doc = BuiltinDocs.int___pow___doc)
+ @ExposedMethod(type = MethodType.BINARY, defaults = {"null"},
+ doc = BuiltinDocs.int___pow___doc)
final PyObject int___pow__(PyObject right, PyObject modulo) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
- if (modulo != null && !canCoerce(modulo))
+ if (modulo != null && !canCoerce(modulo)) {
return null;
+ }
return _pow(getValue(), coerce(right), modulo, this, right);
}
public PyObject __rpow__(PyObject left, PyObject modulo) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
- if (modulo != null && !canCoerce(modulo))
+ if (modulo != null && !canCoerce(modulo)) {
return null;
+ }
return _pow(coerce(left), getValue(), modulo, left, this);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rpow___doc)
- final PyObject int___rpow__(PyObject left){
- return __rpow__(left, null);
+ final PyObject int___rpow__(PyObject left) {
+ return __rpow__(left, null);
}
- private static PyObject _pow(int value, int pow, PyObject modulo, PyObject left, PyObject right) {
+ private static PyObject _pow(int value, int pow, PyObject modulo, PyObject left,
+ PyObject right) {
int mod = 0;
long tmp = value;
boolean neg = false;
@@ -477,11 +522,11 @@
long result = 1;
if (pow < 0) {
- if (value != 0)
+ if (value != 0) {
return left.__float__().__pow__(right, modulo);
- else
- throw Py.ZeroDivisionError("cannot raise 0 to a " +
- "negative power");
+ } else {
+ throw Py.ZeroDivisionError("cannot raise 0 to a negative power");
+ }
}
if (modulo != null) {
@@ -504,8 +549,9 @@
}
}
pow >>= 1;
- if (pow == 0)
+ if (pow == 0) {
break;
+ }
tmp *= tmp;
if (mod != 0) {
@@ -517,8 +563,9 @@
}
}
- if (neg)
+ if (neg) {
result = -result;
+ }
// Cleanup result of modulo
if (mod != 0) {
@@ -527,19 +574,22 @@
return Py.newInteger(result);
}
+
+ @Override
public PyObject __lshift__(PyObject right) {
return int___lshift__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___lshift___doc)
final PyObject int___lshift__(PyObject right) {
- int rightv;
- if (right instanceof PyInteger)
- rightv = ((PyInteger) right).getValue();
- else if (right instanceof PyLong)
- return int___long__().__lshift__(right);
- else
- return null;
+ int rightv;
+ if (right instanceof PyInteger) {
+ rightv = ((PyInteger) right).getValue();
+ } else if (right instanceof PyLong) {
+ return int___long__().__lshift__(right);
+ } else {
+ return null;
+ }
if (rightv >= Integer.SIZE) {
return __long__().__lshift__(right);
@@ -552,16 +602,17 @@
}
return Py.newInteger(result);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rlshift___doc)
final PyObject int___rlshift__(PyObject left) {
- int leftv;
- if (left instanceof PyInteger)
- leftv = ((PyInteger) left).getValue();
- else if (left instanceof PyLong)
- return left.__rlshift__(int___long__());
- else
- return null;
+ int leftv;
+ if (left instanceof PyInteger) {
+ leftv = ((PyInteger) left).getValue();
+ } else if (left instanceof PyLong) {
+ return left.__rlshift__(int___long__());
+ } else {
+ return null;
+ }
if (getValue() >= Integer.SIZE) {
return left.__long__().__lshift__(this);
@@ -575,60 +626,56 @@
return Py.newInteger(result);
}
+ @Override
public PyObject __rshift__(PyObject right) {
return int___rshift__(right);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rshift___doc)
- final PyObject int___rshift__(PyObject right) {
- int rightv;
- if (right instanceof PyInteger)
- rightv = ((PyInteger) right).getValue();
- else if (right instanceof PyLong)
- return int___long__().__rshift__(right);
- else
- return null;
+ final PyObject int___rshift__(PyObject right) {
+ int rightv;
+ if (right instanceof PyInteger) {
+ rightv = ((PyInteger) right).getValue();
+ } else if (right instanceof PyLong) {
+ return int___long__().__rshift__(right);
+ } else {
+ return null;
+ }
- if (rightv < 0) {
- throw Py.ValueError("negative shift count");
+ if (rightv < 0) {
+ throw Py.ValueError("negative shift count");
}
if (rightv >= Integer.SIZE) {
- if (getValue() < 0) {
- return Py.newInteger(-1);
- } else {
- return Py.newInteger(0);
- }
+ return Py.newInteger(getValue() < 0 ? -1 : 0);
}
- return Py.newInteger(getValue() >> rightv);
- }
+ return Py.newInteger(getValue() >> rightv);
+ }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rrshift___doc)
final PyObject int___rrshift__(PyObject left) {
int leftv;
- if (left instanceof PyInteger)
- leftv = ((PyInteger)left).getValue();
- else if(left instanceof PyLong)
- return left.__rshift__(int___long__());
- else
- return null;
+ if (left instanceof PyInteger) {
+ leftv = ((PyInteger) left).getValue();
+ } else if (left instanceof PyLong) {
+ return left.__rshift__(int___long__());
+ } else {
+ return null;
+ }
- if(getValue() < 0) {
+ if (getValue() < 0) {
throw Py.ValueError("negative shift count");
}
if (getValue() >= Integer.SIZE) {
- if (leftv < 0) {
- return Py.newInteger(-1);
- } else {
- return Py.newInteger(0);
- }
+ return Py.newInteger(leftv < 0 ? -1 : 0);
}
return Py.newInteger(leftv >> getValue());
}
+ @Override
public PyObject __and__(PyObject right) {
return int___and__(right);
}
@@ -636,73 +683,80 @@
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___and___doc)
final PyObject int___and__(PyObject right) {
int rightv;
- if (right instanceof PyInteger)
- rightv = ((PyInteger) right).getValue();
- else if (right instanceof PyLong)
- return int___long__().__and__(right);
- else
- return null;
+ if (right instanceof PyInteger) {
+ rightv = ((PyInteger) right).getValue();
+ } else if (right instanceof PyLong) {
+ return int___long__().__and__(right);
+ } else {
+ return null;
+ }
return Py.newInteger(getValue() & rightv);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rand___doc)
- final PyObject int___rand__(PyObject left){
- return int___and__(left);
+ final PyObject int___rand__(PyObject left) {
+ return int___and__(left);
}
+ @Override
public PyObject __xor__(PyObject right) {
return int___xor__(right);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___xor___doc)
- final PyObject int___xor__(PyObject right) {
- int rightv;
- if (right instanceof PyInteger)
- rightv = ((PyInteger) right).getValue();
- else if (right instanceof PyLong)
- return int___long__().__xor__(right);
- else
- return null;
+ final PyObject int___xor__(PyObject right) {
+ int rightv;
+ if (right instanceof PyInteger) {
+ rightv = ((PyInteger) right).getValue();
+ } else if (right instanceof PyLong) {
+ return int___long__().__xor__(right);
+ } else {
+ return null;
+ }
- return Py.newInteger(getValue() ^ rightv);
+ return Py.newInteger(getValue() ^ rightv);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rxor___doc)
- final PyObject int___rxor__(PyObject left){
+ final PyObject int___rxor__(PyObject left) {
int leftv;
- if (left instanceof PyInteger)
- leftv = ((PyInteger)left).getValue();
- else if(left instanceof PyLong)
- return left.__rxor__(int___long__());
- else
- return null;
+ if (left instanceof PyInteger) {
+ leftv = ((PyInteger) left).getValue();
+ } else if (left instanceof PyLong) {
+ return left.__rxor__(int___long__());
+ } else {
+ return null;
+ }
return Py.newInteger(leftv ^ getValue());
}
+ @Override
public PyObject __or__(PyObject right) {
return int___or__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___or___doc)
- final PyObject int___or__(PyObject right) {
- int rightv;
- if (right instanceof PyInteger)
- rightv = ((PyInteger) right).getValue();
- else if (right instanceof PyLong)
- return int___long__().__or__(right);
- else
- return null;
+ final PyObject int___or__(PyObject right) {
+ int rightv;
+ if (right instanceof PyInteger) {
+ rightv = ((PyInteger) right).getValue();
+ } else if (right instanceof PyLong) {
+ return int___long__().__or__(right);
+ } else {
+ return null;
+ }
- return Py.newInteger(getValue() | rightv);
+ return Py.newInteger(getValue() | rightv);
}
-
+
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___ror___doc)
- final PyObject int___ror__(PyObject left){
+ final PyObject int___ror__(PyObject left) {
return int___or__(left);
}
+ @Override
public PyObject __neg__() {
return int___neg__();
}
@@ -713,6 +767,7 @@
return Py.newInteger(x);
}
+ @Override
public PyObject __pos__() {
return int___pos__();
}
@@ -722,10 +777,11 @@
return int___int__();
}
+ @Override
public PyObject __abs__() {
return int___abs__();
}
-
+
@ExposedMethod(doc = BuiltinDocs.int___abs___doc)
final PyObject int___abs__() {
if (getValue() < 0) {
@@ -734,6 +790,7 @@
return int___int__();
}
+ @Override
public PyObject __invert__() {
return int___invert__();
}
@@ -743,6 +800,7 @@
return Py.newInteger(~getValue());
}
+ @Override
public PyObject __int__() {
return int___int__();
}
@@ -755,6 +813,7 @@
return Py.newInteger(getValue());
}
+ @Override
public PyObject __long__() {
return int___long__();
}
@@ -764,19 +823,22 @@
return new PyLong(getValue());
}
+ @Override
public PyFloat __float__() {
return int___float__();
}
@ExposedMethod(doc = BuiltinDocs.int___float___doc)
final PyFloat int___float__() {
- return new PyFloat((double)getValue());
+ return new PyFloat((double) getValue());
}
+ @Override
public PyComplex __complex__() {
return new PyComplex(getValue(), 0.);
}
+ @Override
public PyString __oct__() {
return int___oct__();
}
@@ -785,12 +847,12 @@
final PyString int___oct__() {
if (getValue() < 0) {
return new PyString("-0" + Integer.toString(getValue() * -1, 8));
- }
- else {
+ } else {
return new PyString("0" + Integer.toString(getValue(), 8));
}
}
+ @Override
public PyString __hex__() {
return int___hex__();
}
@@ -799,17 +861,17 @@
final PyString int___hex__() {
if (getValue() < 0) {
return new PyString("-0x" + Integer.toString(getValue() * -1, 16));
- }
- else {
+ } else {
return new PyString("0x" + Integer.toString(getValue(), 16));
}
}
-
+
@ExposedMethod(doc = BuiltinDocs.int___getnewargs___doc)
final PyTuple int___getnewargs__() {
return new PyTuple(new PyObject[]{new PyInteger(this.getValue())});
}
+ @Override
public PyTuple __getnewargs__() {
return int___getnewargs__();
}
@@ -825,27 +887,36 @@
}
@Override
- public boolean isNumberType() {
+ public boolean isIndex() {
return true;
}
@Override
- public boolean isIndex() {
+ public int asIndex(PyObject err) {
+ return getValue();
+ }
+
+ @Override
+ public boolean isMappingType() {
+ return false;
+ }
+
+ @Override
+ public boolean isNumberType() {
return true;
}
@Override
- public int asIndex(PyObject err) {
- return getValue();
+ public boolean isSequenceType() {
+ return false;
}
- public boolean isMappingType() { return false; }
- public boolean isSequenceType() { return false; }
-
+ @Override
public long asLong(int index) {
return getValue();
}
+ @Override
public int asInt(int index) {
return getValue();
}
Modified: trunk/jython/src/org/python/core/PyLong.java
===================================================================
--- trunk/jython/src/org/python/core/PyLong.java 2010-03-13 17:33:14 UTC (rev 6984)
+++ trunk/jython/src/org/python/core/PyLong.java 2010-03-14 21:39:57 UTC (rev 6985)
@@ -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.io.Serializable;
@@ -11,8 +14,7 @@
import org.python.expose.MethodType;
/**
- * A builtin python long. This is implemented as a
- * java.math.BigInteger.
+ * A builtin python long. This is implemented as a java.math.BigInteger.
*/
@ExposedType(name = "long", doc = BuiltinDocs.long_doc)
public class PyLong extends PyObject {
@@ -22,10 +24,31 @@
public static final BigInteger minLong = BigInteger.valueOf(Long.MIN_VALUE);
public static final BigInteger maxLong = BigInteger.valueOf(Long.MAX_VALUE);
public static final BigInteger maxULong =
- BigInteger.valueOf(1).shiftLeft(64).subtract(BigInteger.valueOf(1));
+ BigInteger.valueOf(1).shiftLeft(64).subtract(BigInteger.valueOf(1));
private BigInteger value;
+ public PyLong(PyType subType, BigInteger v) {
+ super(subType);
+ value = v;
+ }
+
+ public PyLong(BigInteger v) {
+ this(TYPE, v);
+ }
+
+ public PyLong(double v) {
+ this(toBigInteger(v));
+ }
+
+ public PyLong(long v) {
+ this(BigInteger.valueOf(v));
+ }
+
+ public PyLong(String s) {
+ this(new BigInteger(s));
+ }
+
@ExposedNew
public static PyObject long___new__(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
@@ -36,7 +59,7 @@
ArgParser ap = new ArgParser("long", args, keywords, new String[] {"x", "base"}, 0);
PyObject x = ap.getPyObject(0, null);
if (x != null && x.getJavaProxy() instanceof BigInteger) {
- return new PyLong((BigInteger)x.getJavaProxy());
+ return new PyLong((BigInteger) x.getJavaProxy());
}
int base = ap.getInt(1, -909);
@@ -57,7 +80,7 @@
if (!(x instanceof PyString)) {
throw Py.TypeError("long: can't convert non-string with explicit base");
}
- return ((PyString)x).atol(base);
+ return ((PyString) x).atol(base);
}
/**
@@ -71,34 +94,13 @@
PyObject[] args, String[] keywords) {
PyObject tmp = long___new__(new_, init, TYPE, args, keywords);
if (tmp instanceof PyInteger) {
- int intValue = ((PyInteger)tmp).getValue();
+ int intValue = ((PyInteger) tmp).getValue();
return new PyLongDerived(subtype, BigInteger.valueOf(intValue));
} else {
- return new PyLongDerived(subtype, ((PyLong)tmp).value);
+ return new PyLongDerived(subtype, ((PyLong) tmp).value);
}
}
- public PyLong(PyType subType, BigInteger v) {
- super(subType);
- value = v;
- }
-
- public PyLong(BigInteger v) {
- this(TYPE, v);
- }
-
- public PyLong(double v) {
- this(toBigInteger(v));
- }
-
- public PyLong(long v) {
- this(BigInteger.valueOf(v));
- }
-
- public PyLong(String s) {
- this(new BigInteger(s));
- }
-
/**
* Convert a double to BigInteger, raising an OverflowError if
* infinite.
@@ -117,15 +119,17 @@
return value;
}
+ @Override
public String toString() {
return long_toString();
}
@ExposedMethod(names = {"__str__", "__repr__"}, doc = BuiltinDocs.long___str___doc)
final String long_toString() {
- return value.toString()+"L";
+ return value.toString() + "L";
}
+ @Override
public int hashCode() {
return long___hash__();
}
@@ -135,6 +139,7 @@
return value.intValue();
}
+ @Override
public boolean __nonzero__() {
return !value.equals(BigInteger.valueOf(0));
}
@@ -152,7 +157,7 @@
return v;
}
- private static final double scaledDoubleValue(BigInteger val, int[] exp){
+ private static final double scaledDoubleValue(BigInteger val, int[] exp) {
double x = 0;
int signum = val.signum();
byte[] digits;
@@ -170,21 +175,20 @@
i++;
count++;
}
- count = count <= digits.length?count:digits.length;
+ count = count <= digits.length ? count : digits.length;
while (i < count) {
x = x * 256 + (digits[i] & 0xff);
i++;
}
exp[0] = digits.length - i;
- return signum*x;
+ return signum * x;
}
- public double scaledDoubleValue(int[] exp){
- return scaledDoubleValue(value,exp);
+ public double scaledDoubleValue(int[] exp) {
+ return scaledDoubleValue(value, exp);
}
-
public long getLong(long min, long max) {
return getLong(min, max, "long int too large to convert");
}
@@ -192,25 +196,28 @@
public long getLong(long min, long max, String overflowMsg) {
if (value.compareTo(maxLong) <= 0 && value.compareTo(minLong) >= 0) {
long v = value.longValue();
- if (v >= min && v <= max)
+ if (v >= min && v <= max) {
return v;
+ }
}
throw Py.OverflowError(overflowMsg);
}
+ @Override
public long asLong(int index) {
return asLong();
}
+ @Override
public int asInt(int index) {
- return (int)getLong(Integer.MIN_VALUE, Integer.MAX_VALUE,
- "long int too large to convert to int");
+ return (int) getLong(Integer.MIN_VALUE, Integer.MAX_VALUE,
+ "long int too large to convert to int");
}
@Override
public int asInt() {
- return (int)getLong(Integer.MIN_VALUE, Integer.MAX_VALUE,
- "long int too large to convert to int");
+ return (int) getLong(Integer.MIN_VALUE, Integer.MAX_VALUE,
+ "long int too large to convert to int");
}
@Override
@@ -218,32 +225,26 @@
return getLong(Long.MIN_VALUE, Long.MAX_VALUE, "long too big to convert");
}
- public Object __tojava__(Class c) {
+ @Override
+ public Object __tojava__(Class<?> c) {
try {
if (c == Byte.TYPE || c == Byte.class) {
- return new Byte((byte)getLong(Byte.MIN_VALUE,
- Byte.MAX_VALUE));
+ return new Byte((byte) getLong(Byte.MIN_VALUE, Byte.MAX_VALUE));
}
if (c == Short.TYPE || c == Short.class) {
- return new Short((short)getLong(Short.MIN_VALUE,
- Short.MAX_VALUE));
+ return new Short((short) getLong(Short.MIN_VALUE, Short.MAX_VALUE));
}
if (c == Integer.TYPE || c == Integer.class) {
- return new Integer((int)getLong(Integer.MIN_VALUE,
- Integer.MAX_VALUE));
+ return new Integer((int) getLong(Integer.MIN_VALUE, Integer.MAX_VALUE));
}
if (c == Long.TYPE || c == Long.class) {
- return new Long(getLong(Long.MIN_VALUE,
- Long.MAX_VALUE));
+ return new Long(getLong(Long.MIN_VALUE, Long.MAX_VALUE));
}
- if (c == Float.TYPE || c == Double.TYPE || c == Float.class ||
- c == Double.class)
- {
+ if (c == Float.TYPE || c == Double.TYPE || c == Float.class || c == Double.class) {
return __float__().__tojava__(c);
}
- if (c == BigInteger.class || c == Number.class ||
- c == Object.class || c == Serializable.class)
- {
+ if (c == BigInteger.class || c == Number.class || c == Object.class
+ || c == Serializable.class) {
return value;
}
} catch (PyException e) {
@@ -252,17 +253,20 @@
return super.__tojava__(c);
}
+ @Override
public int __cmp__(PyObject other) {
return long___cmp__(other);
}
@ExposedMethod(type = MethodType.CMP, doc = BuiltinDocs.long___cmp___doc)
final int long___cmp__(PyObject other) {
- if (!canCoerce(other))
+ if (!canCoerce(other)) {
return -2;
+ }
return value.compareTo(coerce(other));
}
+ @Override
public Object __coerce_ex__(PyObject other) {
return long___coerce_ex__(other);
}
@@ -277,14 +281,13 @@
* invocation of virtual methods from the exposed coerce.
*/
final Object long___coerce_ex__(PyObject other) {
- if (other instanceof PyLong)
+ if (other instanceof PyLong) {
return other;
- else
- if (other instanceof PyInteger) {
- return Py.newLong(((PyInteger)other).getValue());
- } else {
- return Py.None;
- }
+ } else if (other instanceof PyInteger) {
+ return Py.newLong(((PyInteger) other).getValue());
+ } else {
+ return Py.None;
+ }
}
private static final boolean canCoerce(PyObject other) {
@@ -292,26 +295,29 @@
}
private static final BigInteger coerce(PyObject other) {
- if (other instanceof PyLong)
+ if (other instanceof PyLong) {
return ((PyLong) other).value;
- else if (other instanceof PyInteger)
- return BigInteger.valueOf(
- ((PyInteger) other).getValue());
- else
+ } else if (other instanceof PyInteger) {
+ return BigInteger.valueOf(((PyInteger) other).getValue());
+ } else {
throw Py.TypeError("xxx");
+ }
}
+ @Override
public PyObject __add__(PyObject right) {
return long___add__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___add___doc)
final PyObject long___add__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return Py.newLong(value.add(coerce(right)));
}
+ @Override
public PyObject __radd__(PyObject left) {
return long___radd__(left);
}
@@ -321,17 +327,20 @@
return __add__(left);
}
+ @Override
public PyObject __sub__(PyObject right) {
return long___sub__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___sub___doc)
final PyObject long___sub__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return Py.newLong(value.subtract(coerce(right)));
}
+ @Override
public PyObject __rsub__(PyObject left) {
return long___rsub__(left);
}
@@ -341,30 +350,36 @@
return Py.newLong(coerce(left).subtract(value));
}
+ @Override
public PyObject __mul__(PyObject right) {
return long___mul__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___mul___doc)
final PyObject long___mul__(PyObject right) {
- if (right instanceof PySequence)
+ if (right instanceof PySequence) {
return ((PySequence) right).repeat(coerceInt(this));
+ }
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return Py.newLong(value.multiply(coerce(right)));
}
+ @Override
public PyObject __rmul__(PyObject left) {
return long___rmul__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rmul___doc)
final PyObject long___rmul__(PyObject left) {
- if (left instanceof PySequence)
+ if (left instanceof PySequence) {
return ((PySequence) left).repeat(coerceInt(this));
- if (!canCoerce(left))
+ }
+ if (!canCoerce(left)) {
return null;
+ }
return Py.newLong(coerce(left).multiply(value));
}
@@ -372,88 +387,102 @@
// This convention makes sense when you consider it in tandem with modulo
private BigInteger divide(BigInteger x, BigInteger y) {
BigInteger zero = BigInteger.valueOf(0);
- if (y.equals(zero))
+ if (y.equals(zero)) {
throw Py.ZeroDivisionError("long division or modulo");
+ }
if (y.compareTo(zero) < 0) {
- if (x.compareTo(zero) > 0)
- return (x.subtract(y).subtract(
- BigInteger.valueOf(1))).divide(y);
+ if (x.compareTo(zero) > 0) {
+ return (x.subtract(y).subtract(BigInteger.valueOf(1))).divide(y);
+ }
} else {
- if (x.compareTo(zero) < 0)
+ if (x.compareTo(zero) < 0) {
return (x.subtract(y).add(BigInteger.valueOf(1))).divide(y);
+ }
}
return x.divide(y);
}
+ @Override
public PyObject __div__(PyObject right) {
return long___div__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___div___doc)
final PyObject long___div__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
- if (Options.divisionWarning > 0)
+ }
+ if (Options.divisionWarning > 0) {
Py.warning(Py.DeprecationWarning, "classic long division");
+ }
return Py.newLong(divide(value, coerce(right)));
}
+ @Override
public PyObject __rdiv__(PyObject left) {
return long___rdiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rdiv___doc)
final PyObject long___rdiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
- if (Options.divisionWarning > 0)
+ }
+ if (Options.divisionWarning > 0) {
Py.warning(Py.DeprecationWarning, "classic long division");
+ }
return Py.newLong(divide(coerce(left), value));
}
+ @Override
public PyObject __floordiv__(PyObject right) {
return long___floordiv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___floordiv___doc)
final PyObject long___floordiv__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return Py.newLong(divide(value, coerce(right)));
}
+ @Override
public PyObject __rfloordiv__(PyObject left) {
return long___rfloordiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rfloordiv___doc)
final PyObject long___rfloordiv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return Py.newLong(divide(coerce(left), value));
}
- private static final PyFloat true_divide(BigInteger a,BigInteger b) {
+ private static final PyFloat true_divide(BigInteger a, BigInteger b) {
int[] ae = new int[1];
int[] be = new int[1];
- double ad,bd;
+ double ad, bd;
- ad = scaledDoubleValue(a,ae);
- bd = scaledDoubleValue(b,be);
+ ad = scaledDoubleValue(a, ae);
+ bd = scaledDoubleValue(b, be);
- if (bd == 0 ) throw Py.ZeroDivisionError("long division or modulo");
+ if (bd == 0) {
+ throw Py.ZeroDivisionError("long division or modulo");
+ }
ad /= bd;
- int aexp = ae[0]-be[0];
+ int aexp = ae[0] - be[0];
- if (aexp > Integer.MAX_VALUE/8) {
+ if (aexp > Integer.MAX_VALUE / 8) {
throw Py.OverflowError("long/long too large for a float");
- } else if ( aexp < -(Integer.MAX_VALUE/8)) {
+ } else if (aexp < -(Integer.MAX_VALUE / 8)) {
return new PyFloat(0.0);
}
- ad = ad * Math.pow(2.0, aexp*8);
+ ad = ad * Math.pow(2.0, aexp * 8);
if (Double.isInfinite(ad)) {
throw Py.OverflowError("long/long too large for a float");
@@ -462,137 +491,157 @@
return new PyFloat(ad);
}
+ @Override
public PyObject __truediv__(PyObject right) {
return long___truediv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___truediv___doc)
final PyObject long___truediv__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
- return true_divide(this.value,coerce(right));
+ }
+ return true_divide(this.value, coerce(right));
}
+ @Override
public PyObject __rtruediv__(PyObject left) {
return long___rtruediv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rtruediv___doc)
final PyObject long___rtruediv__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
- return true_divide(coerce(left),this.value);
+ }
+ return true_divide(coerce(left), this.value);
}
private BigInteger modulo(BigInteger x, BigInteger y, BigInteger xdivy) {
return x.subtract(xdivy.multiply(y));
}
+ @Override
public PyObject __mod__(PyObject right) {
return long___mod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___mod___doc)
final PyObject long___mod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
BigInteger rightv = coerce(right);
return Py.newLong(modulo(value, rightv, divide(value, rightv)));
}
+ @Override
public PyObject __rmod__(PyObject left) {
return long___rmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rmod___doc)
final PyObject long___rmod__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
BigInteger leftv = coerce(left);
return Py.newLong(modulo(leftv, value, divide(leftv, value)));
}
+ @Override
public PyObject __divmod__(PyObject right) {
return long___divmod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___divmod___doc)
final PyObject long___divmod__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
BigInteger rightv = coerce(right);
BigInteger xdivy = divide(value, rightv);
return new PyTuple(Py.newLong(xdivy), Py.newLong(modulo(value, rightv, xdivy)));
}
+ @Override
public PyObject __rdivmod__(PyObject left) {
return long___rdivmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rdivmod___doc)
final PyObject long___rdivmod__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
BigInteger leftv = coerce(left);
BigInteger xdivy = divide(leftv, value);
return new PyTuple(Py.newLong(xdivy), Py.newLong(modulo(leftv, value, xdivy)));
}
+ @Override
public PyObject __pow__(PyObject right, PyObject modulo) {
return long___pow__(right, modulo);
}
- @ExposedMethod(type = MethodType.BINARY, defaults = {"null"}, doc = BuiltinDocs.long___pow___doc)
+ @ExposedMethod(type = MethodType.BINARY, defaults = {"null"},
+ doc = BuiltinDocs.long___pow___doc)
final PyObject long___pow__(PyObject right, PyObject modulo) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
- if (modulo != null && !canCoerce(right))
+ if (modulo != null && !canCoerce(right)) {
return null;
+ }
return _pow(value, coerce(right), modulo, this, right);
}
+ @Override
public PyObject __rpow__(PyObject left) {
return long___rpow__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rpow___doc)
final PyObject long___rpow__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
+ }
return _pow(coerce(left), value, null, left, this);
}
- public static PyObject _pow(BigInteger value, BigInteger y,
- PyObject modulo, PyObject left, PyObject right)
- {
+ public static PyObject _pow(BigInteger value, BigInteger y, PyObject modulo, PyObject left,
+ PyObject right) {
if (y.compareTo(BigInteger.valueOf(0)) < 0) {
- if (value.compareTo(BigInteger.valueOf(0)) != 0)
+ if (value.compareTo(BigInteger.valueOf(0)) != 0) {
return left.__float__().__pow__(right, modulo);
- else
+ } else {
throw Py.ZeroDivisionError("zero to a negative power");
+ }
}
- if (modulo == null)
+ if (modulo == null) {
return Py.newLong(value.pow(y.intValue()));
- else {
+ } else {
// This whole thing can be trivially rewritten after bugs
// in modPow are fixed by SUN
BigInteger z = coerce(modulo);
int zi = z.intValue();
// Clear up some special cases right away
- if (zi == 0)
+ if (zi == 0) {
throw Py.ValueError("pow(x, y, z) with z == 0");
- if (zi == 1 || zi == -1)
+ }
+ if (zi == 1 || zi == -1) {
return Py.newLong(0);
+ }
if (z.compareTo(BigInteger.valueOf(0)) <= 0) {
// Handle negative modulo's specially
/*if (z.compareTo(BigInteger.valueOf(0)) == 0) {
- throw Py.ValueError("pow(x, y, z) with z == 0");
- }*/
+ throw Py.ValueError("pow(x, y, z) with z == 0");
+ }*/
y = value.modPow(y, z.negate());
if (y.compareTo(BigInteger.valueOf(0)) > 0) {
return Py.newLong(z.add(y));
@@ -611,126 +660,150 @@
}
private static final int coerceInt(PyObject other) {
- if (other instanceof PyLong)
- return ((PyLong)other).asInt();
- else if (other instanceof PyInteger)
+ if (other instanceof PyLong) {
+ return ((PyLong) other).asInt();
+ } else if (other instanceof PyInteger) {
return ((PyInteger) other).getValue();
- else
+ } else {
throw Py.TypeError("xxx");
+ }
}
+ @Override
public PyObject __lshift__(PyObject right) {
return long___lshift__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___lshift___doc)
final PyObject long___lshift__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
int rightv = coerceInt(right);
- if(rightv < 0)
+ if (rightv < 0) {
throw Py.ValueError("negative shift count");
+ }
return Py.newLong(value.shiftLeft(rightv));
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rlshift___doc)
final PyObject long___rlshift__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
- if(value.intValue() < 0)
+ }
+ if (value.intValue() < 0) {
throw Py.ValueError("negative shift count");
+ }
return Py.newLong(coerce(left).shiftLeft(coerceInt(this)));
}
+ @Override
public PyObject __rshift__(PyObject right) {
return long___rshift__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rshift___doc)
final PyObject long___rshift__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
int rightv = coerceInt(right);
- if(rightv < 0)
+ if (rightv < 0) {
throw Py.ValueError("negative shift count");
+ }
return Py.newLong(value.shiftRight(rightv));
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rrshift___doc)
final PyObject long___rrshift__(PyObject left) {
- if (!canCoerce(left))
+ if (!canCoerce(left)) {
return null;
- if(value.intValue() < 0)
+ }
+ if (value.intValue() < 0) {
throw Py.ValueError("negative shift count");
+ }
return Py.newLong(coerce(left).shiftRight(coerceInt(this)));
}
+ @Override
public PyObject __and__(PyObject right) {
return long___and__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___and___doc)
final PyObject long___and__(PyObject right) {
- if (!canCoerce(right))
+ if (!canCoerce(right)) {
return null;
+ }
return Py.newLong(value.and(coerce(right)));
}
+ @Override
public PyObject __rand__(PyObject left) {
return long___rand__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.long___rand___doc)
final PyObject long___rand__(PyObject left) {
@@ Diff output truncated at 100000 characters. @@
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|