- android/ 167个模块 (app/content/os/view/widget/graphics/media/hardware/net/telephony/provider等) - java/ 35个模块 (lang/io/util/net/nio/security/crypto/sql/beans/rmi/awt等) - javax/ 4个模块 (net/crypto/security/sql) - org/ 5个模块 (json/w3c_dom/xml_sax/xml_pull/apache_http) - dalvik/ 1个模块 (system) - ~3053个类/接口,方法签名完全对齐Android SDK - 桌面跨平台运行 (Windows/macOS/Linux) - 修复所有导入错误和Python关键字冲突
599 lines
15 KiB
Python
599 lines
15 KiB
Python
"""
|
|
Java java.util.function standard library APIs implemented in Python.
|
|
java.util.function package - Function, Consumer, Predicate, Supplier, etc.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from .._core import Stub
|
|
from .lang import RuntimeException
|
|
import typing as _typing
|
|
|
|
__all__ = [
|
|
"BiConsumer", "BiFunction", "BiPredicate", "BinaryOperator",
|
|
"Consumer", "DoubleBinaryOperator", "DoubleConsumer",
|
|
"DoubleFunction", "DoublePredicate", "DoubleSupplier",
|
|
"DoubleToIntFunction", "DoubleToLongFunction",
|
|
"Function", "IntBinaryOperator", "IntConsumer",
|
|
"IntFunction", "IntPredicate", "IntSupplier",
|
|
"IntToDoubleFunction", "IntToLongFunction",
|
|
"LongBinaryOperator", "LongConsumer", "LongFunction",
|
|
"LongPredicate", "LongSupplier", "LongToDoubleFunction",
|
|
"LongToIntFunction",
|
|
"ObjDoubleConsumer", "ObjIntConsumer", "ObjLongConsumer",
|
|
"Predicate", "Supplier",
|
|
"ToDoubleBiFunction", "ToDoubleFunction",
|
|
"ToIntBiFunction", "ToIntFunction",
|
|
"ToLongBiFunction", "ToLongFunction",
|
|
"UnaryOperator",
|
|
]
|
|
|
|
T = _typing.TypeVar("T")
|
|
R = _typing.TypeVar("R")
|
|
|
|
|
|
# ============================================================
|
|
# Core functional interfaces
|
|
# ============================================================
|
|
|
|
class Function(Stub):
|
|
"""java.util.function.Function"""
|
|
def apply(self, t):
|
|
raise NotImplementedError
|
|
|
|
def compose(self, before):
|
|
class Composed(Function):
|
|
def apply(self, t):
|
|
return self.apply(before.apply(t))
|
|
return Composed()
|
|
|
|
def andThen(self, after):
|
|
class ComposedAndThen(Function):
|
|
def apply(self, t):
|
|
return after.apply(self.apply(t))
|
|
return ComposedAndThen()
|
|
|
|
@staticmethod
|
|
def identity():
|
|
class Identity(Function):
|
|
def apply(self, t):
|
|
return t
|
|
return Identity()
|
|
|
|
|
|
class BiFunction(Stub):
|
|
"""java.util.function.BiFunction"""
|
|
def apply(self, t, u):
|
|
raise NotImplementedError
|
|
|
|
def andThen(self, after):
|
|
class Composed(BiFunction):
|
|
def apply(self, t, u):
|
|
return after.apply(self.apply(t, u))
|
|
return Composed()
|
|
|
|
|
|
class UnaryOperator(Function):
|
|
"""java.util.function.UnaryOperator"""
|
|
@staticmethod
|
|
def identity():
|
|
class Identity(UnaryOperator):
|
|
def apply(self, t):
|
|
return t
|
|
return Identity()
|
|
|
|
|
|
class BinaryOperator(BiFunction):
|
|
"""java.util.function.BinaryOperator"""
|
|
@staticmethod
|
|
def min_by(comparator):
|
|
class MinBy(BinaryOperator):
|
|
def apply(self, a, b):
|
|
return a if comparator.compare(a, b) <= 0 else b
|
|
return MinBy()
|
|
|
|
@staticmethod
|
|
def max_by(comparator):
|
|
class MaxBy(BinaryOperator):
|
|
def apply(self, a, b):
|
|
return a if comparator.compare(a, b) >= 0 else b
|
|
return MaxBy()
|
|
|
|
|
|
class Predicate(Stub):
|
|
"""java.util.function.Predicate"""
|
|
def test(self, t):
|
|
raise NotImplementedError
|
|
|
|
def and_(self, other):
|
|
class And(Predicate):
|
|
def test(self, t):
|
|
return self.test(t) and other.test(t)
|
|
return And()
|
|
|
|
def negate(self):
|
|
class Negate(Predicate):
|
|
def test(self, t):
|
|
return not self.test(t)
|
|
return Negate()
|
|
|
|
def or_(self, other):
|
|
class Or(Predicate):
|
|
def test(self, t):
|
|
return self.test(t) or other.test(t)
|
|
return Or()
|
|
|
|
@staticmethod
|
|
def is_equal(target_ref):
|
|
class IsEqual(Predicate):
|
|
def test(self, t):
|
|
return t == target_ref
|
|
return IsEqual()
|
|
|
|
@staticmethod
|
|
def not_(target):
|
|
class Not(Predicate):
|
|
def test(self, t):
|
|
return not target.test(t)
|
|
return Not()
|
|
|
|
|
|
class BiPredicate(Stub):
|
|
"""java.util.function.BiPredicate"""
|
|
def test(self, t, u):
|
|
raise NotImplementedError
|
|
|
|
def and_(self, other):
|
|
class And(BiPredicate):
|
|
def test(self, t, u):
|
|
return self.test(t, u) and other.test(t, u)
|
|
return And()
|
|
|
|
def negate(self):
|
|
class Negate(BiPredicate):
|
|
def test(self, t, u):
|
|
return not self.test(t, u)
|
|
return Negate()
|
|
|
|
def or_(self, other):
|
|
class Or(BiPredicate):
|
|
def test(self, t, u):
|
|
return self.test(t, u) or other.test(t, u)
|
|
return Or()
|
|
|
|
|
|
class Consumer(Stub):
|
|
"""java.util.function.Consumer"""
|
|
def accept(self, t):
|
|
raise NotImplementedError
|
|
|
|
def andThen(self, after):
|
|
class Composed(Consumer):
|
|
def accept(self, t):
|
|
self.accept(t)
|
|
after.accept(t)
|
|
return Composed()
|
|
|
|
|
|
class BiConsumer(Stub):
|
|
"""java.util.function.BiConsumer"""
|
|
def accept(self, t, u):
|
|
raise NotImplementedError
|
|
|
|
def andThen(self, after):
|
|
class Composed(BiConsumer):
|
|
def accept(self, t, u):
|
|
self.accept(t, u)
|
|
after.accept(t, u)
|
|
return Composed()
|
|
|
|
|
|
class Supplier(Stub):
|
|
"""java.util.function.Supplier"""
|
|
def get(self):
|
|
raise NotImplementedError
|
|
|
|
|
|
# ============================================================
|
|
# Primitive specializations
|
|
# ============================================================
|
|
|
|
class IntFunction(Stub):
|
|
"""java.util.function.IntFunction"""
|
|
def apply(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class LongFunction(Stub):
|
|
"""java.util.function.LongFunction"""
|
|
def apply(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class DoubleFunction(Stub):
|
|
"""java.util.function.DoubleFunction"""
|
|
def apply(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class IntConsumer(Stub):
|
|
"""java.util.function.IntConsumer"""
|
|
def accept(self, value):
|
|
raise NotImplementedError
|
|
|
|
def andThen(self, after):
|
|
class Composed(IntConsumer):
|
|
def accept(self, value):
|
|
self.accept(value)
|
|
after.accept(value)
|
|
return Composed()
|
|
|
|
|
|
class LongConsumer(Stub):
|
|
"""java.util.function.LongConsumer"""
|
|
def accept(self, value):
|
|
raise NotImplementedError
|
|
|
|
def andThen(self, after):
|
|
class Composed(LongConsumer):
|
|
def accept(self, value):
|
|
self.accept(value)
|
|
after.accept(value)
|
|
return Composed()
|
|
|
|
|
|
class DoubleConsumer(Stub):
|
|
"""java.util.function.DoubleConsumer"""
|
|
def accept(self, value):
|
|
raise NotImplementedError
|
|
|
|
def andThen(self, after):
|
|
class Composed(DoubleConsumer):
|
|
def accept(self, value):
|
|
self.accept(value)
|
|
after.accept(value)
|
|
return Composed()
|
|
|
|
|
|
class IntPredicate(Stub):
|
|
"""java.util.function.IntPredicate"""
|
|
def test(self, value):
|
|
raise NotImplementedError
|
|
|
|
def and_(self, other):
|
|
class And(IntPredicate):
|
|
def test(self, value):
|
|
return self.test(value) and other.test(value)
|
|
return And()
|
|
|
|
def negate(self):
|
|
class Negate(IntPredicate):
|
|
def test(self, value):
|
|
return not self.test(value)
|
|
return Negate()
|
|
|
|
def or_(self, other):
|
|
class Or(IntPredicate):
|
|
def test(self, value):
|
|
return self.test(value) or other.test(value)
|
|
return Or()
|
|
|
|
|
|
class LongPredicate(Stub):
|
|
"""java.util.function.LongPredicate"""
|
|
def test(self, value):
|
|
raise NotImplementedError
|
|
|
|
def and_(self, other):
|
|
class And(LongPredicate):
|
|
def test(self, value):
|
|
return self.test(value) and other.test(value)
|
|
return And()
|
|
|
|
def negate(self):
|
|
class Negate(LongPredicate):
|
|
def test(self, value):
|
|
return not self.test(value)
|
|
return Negate()
|
|
|
|
def or_(self, other):
|
|
class Or(LongPredicate):
|
|
def test(self, value):
|
|
return self.test(value) or other.test(value)
|
|
return Or()
|
|
|
|
|
|
class DoublePredicate(Stub):
|
|
"""java.util.function.DoublePredicate"""
|
|
def test(self, value):
|
|
raise NotImplementedError
|
|
|
|
def and_(self, other):
|
|
class And(DoublePredicate):
|
|
def test(self, value):
|
|
return self.test(value) and other.test(value)
|
|
return And()
|
|
|
|
def negate(self):
|
|
class Negate(DoublePredicate):
|
|
def test(self, value):
|
|
return not self.test(value)
|
|
return Negate()
|
|
|
|
def or_(self, other):
|
|
class Or(DoublePredicate):
|
|
def test(self, value):
|
|
return self.test(value) or other.test(value)
|
|
return Or()
|
|
|
|
|
|
class IntSupplier(Stub):
|
|
"""java.util.function.IntSupplier"""
|
|
def getAsInt(self):
|
|
raise NotImplementedError
|
|
|
|
|
|
class LongSupplier(Stub):
|
|
"""java.util.function.LongSupplier"""
|
|
def getAsLong(self):
|
|
raise NotImplementedError
|
|
|
|
|
|
class DoubleSupplier(Stub):
|
|
"""java.util.function.DoubleSupplier"""
|
|
def getAsDouble(self):
|
|
raise NotImplementedError
|
|
|
|
|
|
class IntBinaryOperator(Stub):
|
|
"""java.util.function.IntBinaryOperator"""
|
|
def applyAsInt(self, left, right):
|
|
raise NotImplementedError
|
|
|
|
|
|
class LongBinaryOperator(Stub):
|
|
"""java.util.function.LongBinaryOperator"""
|
|
def applyAsLong(self, left, right):
|
|
raise NotImplementedError
|
|
|
|
|
|
class DoubleBinaryOperator(Stub):
|
|
"""java.util.function.DoubleBinaryOperator"""
|
|
def applyAsDouble(self, left, right):
|
|
raise NotImplementedError
|
|
|
|
|
|
# ============================================================
|
|
# Int-Unary conversion functions
|
|
# ============================================================
|
|
|
|
class IntToDoubleFunction(Stub):
|
|
"""java.util.function.IntToDoubleFunction"""
|
|
def applyAsDouble(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class IntToLongFunction(Stub):
|
|
"""java.util.function.IntToLongFunction"""
|
|
def applyAsLong(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class LongToDoubleFunction(Stub):
|
|
"""java.util.function.LongToDoubleFunction"""
|
|
def applyAsDouble(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class LongToIntFunction(Stub):
|
|
"""java.util.function.LongToIntFunction"""
|
|
def applyAsInt(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class DoubleToIntFunction(Stub):
|
|
"""java.util.function.DoubleToIntFunction"""
|
|
def applyAsInt(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class DoubleToLongFunction(Stub):
|
|
"""java.util.function.DoubleToLongFunction"""
|
|
def applyAsLong(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
# ============================================================
|
|
# Obj*Consumer (mix of object and primitive)
|
|
# ============================================================
|
|
|
|
class ObjIntConsumer(Stub):
|
|
"""java.util.function.ObjIntConsumer"""
|
|
def accept(self, t, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class ObjLongConsumer(Stub):
|
|
"""java.util.function.ObjLongConsumer"""
|
|
def accept(self, t, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class ObjDoubleConsumer(Stub):
|
|
"""java.util.function.ObjDoubleConsumer"""
|
|
def accept(self, t, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
# ============================================================
|
|
# To* functions
|
|
# ============================================================
|
|
|
|
class ToIntFunction(Stub):
|
|
"""java.util.function.ToIntFunction"""
|
|
def applyAsInt(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class ToLongFunction(Stub):
|
|
"""java.util.function.ToLongFunction"""
|
|
def applyAsLong(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class ToDoubleFunction(Stub):
|
|
"""java.util.function.ToDoubleFunction"""
|
|
def applyAsDouble(self, value):
|
|
raise NotImplementedError
|
|
|
|
|
|
class ToIntBiFunction(Stub):
|
|
"""java.util.function.ToIntBiFunction"""
|
|
def applyAsInt(self, t, u):
|
|
raise NotImplementedError
|
|
|
|
|
|
class ToLongBiFunction(Stub):
|
|
"""java.util.function.ToLongBiFunction"""
|
|
def applyAsLong(self, t, u):
|
|
raise NotImplementedError
|
|
|
|
|
|
class ToDoubleBiFunction(Stub):
|
|
"""java.util.function.ToDoubleBiFunction"""
|
|
def applyAsDouble(self, t, u):
|
|
raise NotImplementedError
|
|
|
|
|
|
# ============================================================
|
|
# Convenience wrapper helpers
|
|
# ============================================================
|
|
|
|
class _FunctionWrapper(Function):
|
|
"""Wraps a Python callable as a java.util.function.Function"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def apply(self, t):
|
|
return self._fn(t)
|
|
|
|
|
|
class _ConsumerWrapper(Consumer):
|
|
"""Wraps a Python callable as a java.util.function.Consumer"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def accept(self, t):
|
|
self._fn(t)
|
|
|
|
|
|
class _PredicateWrapper(Predicate):
|
|
"""Wraps a Python callable as a java.util.function.Predicate"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def test(self, t):
|
|
return self._fn(t)
|
|
|
|
|
|
class _SupplierWrapper(Supplier):
|
|
"""Wraps a Python callable as a java.util.function.Supplier"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def get(self):
|
|
return self._fn()
|
|
|
|
|
|
class _BiFunctionWrapper(BiFunction):
|
|
"""Wraps a Python callable as a java.util.function.BiFunction"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def apply(self, t, u):
|
|
return self._fn(t, u)
|
|
|
|
|
|
class _BiConsumerWrapper(BiConsumer):
|
|
"""Wraps a Python callable as a java.util.function.BiConsumer"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def accept(self, t, u):
|
|
self._fn(t, u)
|
|
|
|
|
|
class _BiPredicateWrapper(BiPredicate):
|
|
"""Wraps a Python callable as a java.util.function.BiPredicate"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def test(self, t, u):
|
|
return self._fn(t, u)
|
|
|
|
|
|
class _IntFunctionWrapper(IntFunction):
|
|
"""Wraps a Python callable as a java.util.function.IntFunction"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def apply(self, value):
|
|
return self._fn(value)
|
|
|
|
|
|
class _LongFunctionWrapper(LongFunction):
|
|
"""Wraps a Python callable as a java.util.function.LongFunction"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def apply(self, value):
|
|
return self._fn(value)
|
|
|
|
|
|
class _DoubleFunctionWrapper(DoubleFunction):
|
|
"""Wraps a Python callable as a java.util.function.DoubleFunction"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def apply(self, value):
|
|
return self._fn(value)
|
|
|
|
|
|
class _IntConsumerWrapper(IntConsumer):
|
|
"""Wraps a Python callable as a java.util.function.IntConsumer"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def accept(self, value):
|
|
self._fn(value)
|
|
|
|
|
|
class _LongConsumerWrapper(LongConsumer):
|
|
"""Wraps a Python callable as a java.util.function.LongConsumer"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def accept(self, value):
|
|
self._fn(value)
|
|
|
|
|
|
class _DoubleConsumerWrapper(DoubleConsumer):
|
|
"""Wraps a Python callable as a java.util.function.DoubleConsumer"""
|
|
def __init__(self, fn):
|
|
super().__init__()
|
|
self._fn = fn
|
|
|
|
def accept(self, value):
|
|
self._fn(value) |