| 1 |
/* |
| 2 |
* Copyright (c) 2021 Goldman Sachs. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* and Eclipse Distribution License v. 1.0 which accompany this distribution. |
| 6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* and the Eclipse Distribution License is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
*/ |
| 10 |
|
| 11 |
package org.eclipse.collections.impl |
| 12 |
|
| 13 |
import java.util.Comparator |
| 14 |
|
| 15 |
import org.eclipse.collections.api.block.function.primitive.{DoubleFunction, FloatFunction, IntFunction, LongFunction} |
| 16 |
import org.eclipse.collections.api.block.function.{Function, Function0, Function2, Function3} |
| 17 |
import org.eclipse.collections.api.block.predicate.{Predicate, Predicate2} |
| 18 |
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure |
| 19 |
import org.eclipse.collections.api.block.procedure.{Procedure, Procedure2} |
| 20 |
|
| 21 |
object Prelude |
| 22 |
{ |
| 23 |
// A "singleton" object to hold the implicit conversion methods |
| 24 |
/* |
| 25 |
* These three methods each take a closure and return an anonymous instance |
| 26 |
* of the corresponding Eclipse Collections interface |
| 27 |
*/ |
| 28 |
implicit def closure2Procedure[T](closure: (T) => Unit): Procedure[T] = |
| 29 |
new Procedure[T] |
| 30 |
{ |
| 31 |
def value(each: T) = closure(each) |
| 32 |
} |
| 33 |
|
| 34 |
implicit def closure2Procedure2[T1, T2](closure: (T1, T2) => Unit): Procedure2[T1, T2] = |
| 35 |
new Procedure2[T1, T2] |
| 36 |
{ |
| 37 |
def value(t1: T1, t2: T2) = closure(t1, t2) |
| 38 |
} |
| 39 |
|
| 40 |
implicit def closure2Function[T, V](closure: (T) => V): Function[T, V] = |
| 41 |
new Function[T, V] |
| 42 |
{ |
| 43 |
def valueOf(t: T) = closure(t) |
| 44 |
} |
| 45 |
|
| 46 |
implicit def closure2Function2[T1, T2, V](closure: (T1, T2) => V): Function2[T1, T2, V] = |
| 47 |
new Function2[T1, T2, V] |
| 48 |
{ |
| 49 |
def value(t1: T1, t2: T2) = closure(t1, t2) |
| 50 |
} |
| 51 |
|
| 52 |
implicit def closure2Function3[T1, T2, T3, V](closure: (T1, T2, T3) => V): Function3[T1, T2, T3, V] = |
| 53 |
new Function3[T1, T2, T3, V] |
| 54 |
{ |
| 55 |
def value(t1: T1, t2: T2, t3: T3) = closure(t1, t2, t3) |
| 56 |
} |
| 57 |
|
| 58 |
implicit def closure2Predicate[T](closure: (T) => Boolean): Predicate[T] = |
| 59 |
new Predicate[T] |
| 60 |
{ |
| 61 |
def accept(each: T) = closure(each) |
| 62 |
} |
| 63 |
|
| 64 |
implicit def closure2Predicate2[T1, T2](closure: (T1, T2) => Boolean): Predicate2[T1, T2] = |
| 65 |
new Predicate2[T1, T2] |
| 66 |
{ |
| 67 |
def accept(t1: T1, t2: T2) = closure(t1, t2) |
| 68 |
} |
| 69 |
|
| 70 |
implicit def closure2ObjectIntProcedure[T](closure: (T, Int) => Unit): ObjectIntProcedure[T] = |
| 71 |
new ObjectIntProcedure[T] |
| 72 |
{ |
| 73 |
def value(each: T, index: Int) = closure(each, index) |
| 74 |
} |
| 75 |
|
| 76 |
implicit def closure2Runnable(closure: () => Unit): Runnable = |
| 77 |
new Runnable |
| 78 |
{ |
| 79 |
def run() = closure() |
| 80 |
} |
| 81 |
|
| 82 |
implicit def closure2CodeBlock[T](closure: () => T): Function0[T] = |
| 83 |
new Function0[T] |
| 84 |
{ |
| 85 |
def value = closure() |
| 86 |
} |
| 87 |
|
| 88 |
implicit def closure2Comparator[T](closure: (T, T) => Int): Comparator[T] = |
| 89 |
new Comparator[T] |
| 90 |
{ |
| 91 |
def compare(o1: T, o2: T) = closure(o1, o2) |
| 92 |
} |
| 93 |
|
| 94 |
implicit def closure2IntFunction[T](closure: (T) => Int): IntFunction[T] = |
| 95 |
new IntFunction[T] |
| 96 |
{ |
| 97 |
def intValueOf(each: T) = closure(each) |
| 98 |
} |
| 99 |
|
| 100 |
implicit def closure2LongFunction[T](closure: (T) => Long): LongFunction[T] = |
| 101 |
new LongFunction[T] |
| 102 |
{ |
| 103 |
def longValueOf(each: T) = closure(each) |
| 104 |
} |
| 105 |
|
| 106 |
implicit def closure2DoubleFunction[T](closure: (T) => Double): DoubleFunction[T] = |
| 107 |
new DoubleFunction[T] |
| 108 |
{ |
| 109 |
def doubleValueOf(each: T) = closure(each) |
| 110 |
} |
| 111 |
|
| 112 |
implicit def closure2FloatFunction[T](closure: (T) => Float): FloatFunction[T] = |
| 113 |
new FloatFunction[T] |
| 114 |
{ |
| 115 |
def floatValueOf(each: T) = closure(each) |
| 116 |
} |
| 117 |
} |