From: <pj...@us...> - 2009-10-26 21:15:18
|
Revision: 6909 http://jython.svn.sourceforge.net/jython/?rev=6909&view=rev Author: pjenvey Date: 2009-10-26 21:15:12 +0000 (Mon, 26 Oct 2009) Log Message: ----------- PyBoolean can just inherit various things from PyInteger, cleanup Modified Paths: -------------- trunk/jython/src/org/python/core/PyBoolean.java Modified: trunk/jython/src/org/python/core/PyBoolean.java =================================================================== --- trunk/jython/src/org/python/core/PyBoolean.java 2009-10-26 21:05:42 UTC (rev 6908) +++ trunk/jython/src/org/python/core/PyBoolean.java 2009-10-26 21:15:12 UTC (rev 6909) @@ -1,3 +1,4 @@ +/* Copyright (c) Jython Developers */ package org.python.core; import org.python.expose.ExposedMethod; @@ -6,13 +7,20 @@ import org.python.expose.MethodType; /** - * A builtin python bool. + * The builtin python bool. */ @ExposedType(name = "bool", isBaseType = false, doc = BuiltinDocs.bool_doc) public class PyBoolean extends PyInteger { - + public static final PyType TYPE = PyType.fromClass(PyBoolean.class); - + + private boolean value; + + public PyBoolean(boolean value) { + super(TYPE, value ? 1 : 0); + this.value = value; + } + @ExposedNew public static PyObject bool_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { @@ -23,18 +31,8 @@ } return obj.__nonzero__() ? Py.True : Py.False; } - - private boolean value; - public PyBoolean(boolean value) { - super(TYPE, value ? 1 : 0); - this.value = value; - } - - public int getValue() { - return value ? 1 : 0; - } - + @Override public String toString() { return bool_toString(); } @@ -44,6 +42,7 @@ return value ? "True" : "False"; } + @Override public int hashCode() { return bool___hash__(); } @@ -53,6 +52,7 @@ return value ? 1 : 0; } + @Override public boolean __nonzero__() { return bool___nonzero__(); } @@ -62,28 +62,33 @@ return value; } - public Object __tojava__(Class c) { - if (c == Boolean.TYPE || c == Boolean.class || - c == Object.class ) { + @Override + public Object __tojava__(Class<?> c) { + if (c == Boolean.TYPE || c == Boolean.class || c == Object.class ) { return Boolean.valueOf(value); } - if (c == Integer.TYPE || c == Number.class || - c == Integer.class) { + if (c == Integer.TYPE || c == Number.class || c == Integer.class) { return Integer.valueOf(value ? 1 : 0); } - if (c == Byte.TYPE || c == Byte.class) + if (c == Byte.TYPE || c == Byte.class) { return Byte.valueOf((byte)(value ? 1 : 0)); - if (c == Short.TYPE || c == Short.class) + } + if (c == Short.TYPE || c == Short.class) { return Short.valueOf((short)(value ? 1 : 0)); - if (c == Long.TYPE || c == Long.class) + } + if (c == Long.TYPE || c == Long.class) { return Long.valueOf(value ? 1 : 0); - if (c == Float.TYPE || c == Float.class) + } + if (c == Float.TYPE || c == Float.class) { return Float.valueOf(value ? 1 : 0); - if (c == Double.TYPE || c == Double.class) + } + if (c == Double.TYPE || c == Double.class) { return Double.valueOf(value ? 1 : 0); + } return super.__tojava__(c); } + @Override public PyObject __and__(PyObject right) { return bool___and__(right); } @@ -93,12 +98,13 @@ if (right instanceof PyBoolean) { return Py.newBoolean(value & ((PyBoolean)right).value); } else if (right instanceof PyInteger) { - return Py.newInteger(getValue() & ((PyInteger)right).getValue()); + return Py.newInteger(getValue() & ((PyInteger)right).getValue()); } else { return null; } } + @Override public PyObject __xor__(PyObject right) { return bool___xor__(right); } @@ -108,27 +114,29 @@ if (right instanceof PyBoolean) { return Py.newBoolean(value ^ ((PyBoolean)right).value); } else if (right instanceof PyInteger) { - return Py.newInteger(getValue() ^ ((PyInteger)right).getValue()); + return Py.newInteger(getValue() ^ ((PyInteger)right).getValue()); } else { return null; } } + @Override public PyObject __or__(PyObject right) { return bool___or__(right); } - + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.bool___or___doc) final PyObject bool___or__(PyObject right) { if (right instanceof PyBoolean) { return Py.newBoolean(value | ((PyBoolean)right).value); } else if (right instanceof PyInteger) { - return Py.newInteger(getValue() | ((PyInteger)right).getValue()); + return Py.newInteger(getValue() | ((PyInteger)right).getValue()); } else { return null; } } + @Override public PyObject __neg__() { return bool___neg__(); } @@ -138,6 +146,7 @@ return Py.newInteger(value ? -1 : 0); } + @Override public PyObject __pos__() { return bool___pos__(); } @@ -147,6 +156,7 @@ return Py.newInteger(value ? 1 : 0); } + @Override public PyObject __abs__() { return bool___abs__(); } @@ -155,18 +165,4 @@ final PyObject bool___abs__() { return Py.newInteger(value ? 1 : 0); } - - public long asLong(int index) { - return value ? 1 : 0; - } - - public int asInt(int index) { - return value ? 1 : 0; - } - - @Override - public int asInt() { - return value ? 1 : 0; - } - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |