1 package net
.kezvh
.functional
;
3 import java
.util
.Collection
;
4 import java
.util
.Iterator
;
6 import java
.util
.Map
.Entry
;
8 import net
.kezvh
.collections
.DualMap
.DualMap
;
9 import net
.kezvh
.functional
.lambda
.L0
;
10 import net
.kezvh
.functional
.lambda
.L1
;
11 import net
.kezvh
.functional
.lambda
.L2
;
12 import net
.kezvh
.functional
.lambda
.Predicate2
;
13 import net
.kezvh
.lang
.UtilityClass
;
18 public final class Operations
extends UtilityClass
{
22 public static final Predicate2
<?
, ?
> EQUALS
= new Predicate2
<Object
, Object
>() {
24 * @see net.kezvh.functional.lambda.L2#op(java.lang.Object,
26 * @param param1 param1
27 * @param param2 param2
31 public Boolean
op(final Object param1
, final Object param2
) {
32 return (param1
== null) ? param2
== null : param1
.equals(param2
);
38 * @param <V> value type
39 * @return function which returns the value of a map entry
41 public static <K
, V
> L1
<V
, Map
.Entry
<K
, V
>> getValue() {
42 return new L1
<V
, Entry
<K
, V
>>() {
44 public V
op(final Entry
<K
, V
> arg0
) {
45 return arg0
.getValue();
52 * @param <V> value type
53 * @return function which returns the value of a map entry
55 public static <K
, V
> L1
<K
, Map
.Entry
<K
, V
>> getKey() {
56 return new L1
<K
, Entry
<K
, V
>>() {
58 public K
op(final Entry
<K
, V
> arg0
) {
68 @SuppressWarnings("unchecked")
69 public static final <T
> Predicate2
<T
, T
> equals() {
70 return (Predicate2
<T
, T
>) Operations
.EQUALS
;
78 public static final <T
> L0
<T
> constant0(final T t
) {
93 public static final <S
, T
> L1
<S
, T
> constant1(final S s
) {
94 return new L1
<S
, T
>() {
95 public S
op(final T param
) {
108 public static final <S
, T
, U
> L2
<S
, T
, U
> constant2(final S s
) {
109 return new L2
<S
, T
, U
>() {
110 public S
op(final T param
, final U param2
) {
124 public static final <S
, T
, U
> L1
<S
, U
> compose(final L1
<S
, T
> a
, final L1
<T
, U
> b
) {
125 return new L1
<S
, U
>() {
126 public S
op(final U param
) {
127 return a
.op(b
.op(param
));
136 * @param defaultValue COMMENT
139 public static final <S
, T
> L1
<S
, T
> forMap(final Map
<T
, S
> map
, final S defaultValue
) {
140 return new L1
<S
, T
>() {
141 public S
op(final T param
) {
142 if (map
.containsKey(param
))
143 return map
.get(param
);
151 * @param <K1> COMMENT
152 * @param <K2> COMMENT
154 * @param defaultValue COMMENT
157 public static final <D
, K1
, K2
> L2
<D
, K1
, K2
> forMap2(final DualMap
<K1
, K2
, D
> map
, final D defaultValue
) {
158 return new L2
<D
, K1
, K2
>() {
159 public D
op(final K1 param1
, final K2 param2
) {
160 return map
.get(param1
, param2
);
167 * @return map which returns its argument
169 public static final <T
> L1
<T
, T
> identity() {
170 return new L1
<T
, T
>() {
171 public T
op(final T param
) {
178 * returns the size of a collection
180 public static final L1
<Integer
, Collection
<?
>> COLLECTION_SIZE
= new L1
<Integer
, Collection
<?
>>() {
181 public Integer
op(final Collection
<?
> value
) {
186 * returns the number of elements in the iterator (by iterating them, silly)
188 public static final L1
<Integer
, ?
super Iterator
<?
>> ITERATOR_COUNT
= new L1
<Integer
, Iterator
<?
>>() {
190 * @see net.kezvh.functional.lambda.L1#op(java.lang.Object)
191 * @param arg0 some argument
192 * @return number of elements
195 public Integer
op(final Iterator
<?
> arg0
) {
197 for (i
= 0; arg0
.hasNext(); i
++)
205 public static final L1
<?
, ?
> UNSUPPORTED
= new L1
<?
, ?
>() {
206 public Object
op(final Object param
) {
207 throw new UnsupportedOperationException();
213 public static final L2
<Integer
, ?
super Integer
, ?
super Integer
> SUMI
= new L2
<Integer
, Integer
, Integer
>() {
215 * @see net.kezvh.functional.lambda.L2#op(java.lang.Object,
217 * @param arg0 some argument
218 * @param arg1 some argument
219 * @return the sum of the arguments
222 public Integer
op(final Integer arg0
, final Integer arg1
) {
223 return Integer
.valueOf(arg0
.intValue() + arg1
.intValue());
229 public static final L1
<Integer
, Integer
> INTEGER_INCREMENTER
= new L1
<Integer
, Integer
>() {
230 public Integer
op(final Integer param
) {