File: PyBoolean.java

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (178 lines) | stat: -rw-r--r-- 5,251 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright (c) Jython Developers */
package org.python.core;

import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.expose.MethodType;

/**
 * The builtin python bool. It would be nice if it didn't extend PyInteger,
 * but too hard to avoid pre-Python 2.2 semantics here.
 */
@ExposedType(name = "bool", isBaseType = false, doc = BuiltinDocs.bool_doc)
public class PyBoolean extends PyInteger {

    public static final PyType TYPE = PyType.fromClass(PyBoolean.class);

    private final boolean value;

    public boolean getBooleanValue() {
        return value;
    }

    @Override
    public int getValue() {
        return getBooleanValue() ? 1 : 0;
    }

    public PyBoolean(boolean value) {
        super(TYPE, value ? 1 : 0); // XXX is this necessary?
        this.value = value;
    }

    @ExposedNew
    public static PyObject bool_new(PyNewWrapper new_, boolean init, PyType subtype,
                                    PyObject[] args, String[] keywords) {
        ArgParser ap = new ArgParser("bool", args, keywords, new String[] {"x"}, 0);
        PyObject obj = ap.getPyObject(0, null);
        if (obj == null) {
            return Py.False;
        }
        return obj.__nonzero__() ? Py.True : Py.False;
    }

    @Override
    public String toString() {
        return bool_toString();
    }

    @ExposedMethod(names = {"__str__", "__repr__"}, doc = BuiltinDocs.bool___str___doc)
    final String bool_toString() {
        return getBooleanValue() ? "True" : "False";
    }

    @Override
    public int hashCode() {
        return bool___hash__();
    }

    @ExposedMethod(doc = BuiltinDocs.bool___hash___doc)
    final int bool___hash__() {
        return getBooleanValue() ? 1 : 0;
    }

    @Override
    public boolean __nonzero__() {
        return bool___nonzero__();
    }

    @ExposedMethod(doc = BuiltinDocs.bool___nonzero___doc)
    final boolean bool___nonzero__() {
        return getBooleanValue();
    }

    @Override
    public Object __tojava__(Class<?> c) {
        if (c == Boolean.TYPE || c == Boolean.class || c == Object.class ) {
            return Boolean.valueOf(getBooleanValue());
        }
        if (c == Integer.TYPE || c == Number.class || c == Integer.class) {
            return Integer.valueOf(getValue());
        }
        if (c == Byte.TYPE || c == Byte.class) {
            return Byte.valueOf((byte)(getValue()));
        }
        if (c == Short.TYPE || c == Short.class) {
            return Short.valueOf((short)(getValue()));
        }
        if (c == Long.TYPE || c == Long.class) {
            return Long.valueOf(getValue());
        }
        if (c == Float.TYPE || c == Float.class) {
            return Float.valueOf(getValue());
        }
        if (c == Double.TYPE || c == Double.class) {
            return Double.valueOf(getValue());
        }
        return super.__tojava__(c);
    }

    @Override
    public PyObject __and__(PyObject right) {
        return bool___and__(right);
    }

    @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.bool___and___doc)
    final PyObject bool___and__(PyObject right) {
    	if (right instanceof PyBoolean) {
	        return Py.newBoolean(getBooleanValue() & ((PyBoolean) right).getBooleanValue());
    	} else if (right instanceof PyInteger) {
            return Py.newInteger(getValue() & ((PyInteger)right).getValue());
        } else {
	    	return null;
	    }
    }

    @Override
    public PyObject __xor__(PyObject right) {
        return bool___xor__(right);
    }

    @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.bool___xor___doc)
    final PyObject bool___xor__(PyObject right) {
    	if (right instanceof PyBoolean) {
	        return Py.newBoolean(getBooleanValue() ^ ((PyBoolean) right).getBooleanValue());
    	} else if (right instanceof PyInteger) {
            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(getBooleanValue() | ((PyBoolean) right).getBooleanValue());
    	} else if (right instanceof PyInteger) {
            return Py.newInteger(getValue() | ((PyInteger)right).getValue());
        } else {
	    	return null;
	    }
    }

    @Override
    public PyObject __neg__() {
        return bool___neg__();
    }

    @ExposedMethod(doc = BuiltinDocs.bool___neg___doc)
    final PyObject bool___neg__() {
        return Py.newInteger(getBooleanValue() ? -1 : 0);
    }

    @Override
    public PyObject __pos__() {
        return bool___pos__();
    }

    @ExposedMethod(doc = BuiltinDocs.bool___pos___doc)
    final PyObject bool___pos__() {
        return Py.newInteger(getValue());
    }

    @Override
    public PyObject __abs__() {
        return bool___abs__();
    }

    @ExposedMethod(doc = BuiltinDocs.bool___abs___doc)
    final PyObject bool___abs__() {
        return Py.newInteger(getValue());
    }
}