import scala.
util.
control.
Exception.
catching
object FilterFunctionCompositionAbstractSingletonProxyFactoryBean
{ type YobaKostyl
= PartialFunction
[AnyRef, Option
[AnyRef
]]
def and
(f
: YobaKostyl
*) = f.
reduce((f1, f2
) ⇒ f1.
andThen(_.
flatMap(f2
))) def or
(obj
: AnyRef, f
: YobaKostyl
*) = f.
reduce((f1, f2
) ⇒ f1.
andThen(_.
orElse(f2
(obj
)))) }
object FieldFilterFunctionAbstractSingletonProxyFactoryBean
{ import FilterFunctionCompositionAbstractSingletonProxyFactoryBean.
YobaKostyl
def fieldFilterFunction
(name
: String
)(f
: Option
[AnyRef
] ⇒ Option
[AnyRef
]): YobaKostyl
= { catching
(classOf
[NoSuchFieldException
]).
either(obj.
getClass.
getDeclaredField(name
)) match { None
field.
setAccessible(true) f(Some(field.get(obj))).map(_ ⇒ obj)
}
}
}
object TestObjectsAbstractSingletonProxyFactoryBean
{ case class A1
(yoba
: String, petushok
: String
= "kukareku") case class A2
(peka
: Int, list
: Seq
[String
] = Nil
) }
object FilterFunctionAbstractSingletonProxyFactoryBean
{ import FieldFilterFunctionAbstractSingletonProxyFactoryBean.
_ import FilterFunctionCompositionAbstractSingletonProxyFactoryBean.
YobaKostyl
def hasField
(name
: String
): YobaKostyl
= fieldFilterFunction(name)(f ⇒ f)
def hasFieldValue
[T
](name
: String, value
: T
): YobaKostyl
= fieldFilterFunction(name)(_.filter(value ==))
def hasListValue
[T
](name
: String, value
: T
): YobaKostyl
= fieldFilterFunction
(name
)(_.
collect { case list
: Iterable
[_] ⇒ list
}.
filter(_.
exists(value
==)))}
import FilterFunctionAbstractSingletonProxyFactoryBean.
_ import FilterFunctionCompositionAbstractSingletonProxyFactoryBean.
_ import TestObjectsAbstractSingletonProxyFactoryBean.
_
val data
= Vector
(A1
("123"), A2
(321, Vector
("PODI",
"PODMOJSA",
"MANYA")), A1
("ALLOU",
"ko-ko-ko"), A2
(900))
Iterator.
continually(readLine
()).
takeWhile(null ne
).
foreach(f ⇒
{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