11package org .scijava .ops .util ;
22
3+ import java .util .concurrent .Callable ;
34import java .util .function .BiFunction ;
45import java .util .function .Function ;
56
910import org .scijava .ops .core .computer .BiComputer ;
1011import org .scijava .ops .core .computer .Computer ;
1112import org .scijava .ops .core .computer .Computer3 ;
13+ import org .scijava .ops .core .computer .Computer4 ;
1214import org .scijava .ops .core .function .Function3 ;
15+ import org .scijava .ops .core .function .Function4 ;
1316
1417/**
1518 * Utility providing adaptation between {@link Op} types.
@@ -42,16 +45,13 @@ public static <I1, I2, O> BiComputer<I1, I2, O> asBiComputer(final BiFunction<I1
4245 };
4346 }
4447
45- public static <I , O > OneToOneCommand <I , O > asCommand (final Function <I , O > function , I input ) {
46- OneToOneCommand <I , O > command = new OneToOneCommand <I , O >() {
47- @ Override
48- public void run () {
49- output = function .apply (input );
50- }
51- };
52- // Populate the input member of the function command
53- Inject .Commands .inputs (command , input );
54- return command ;
48+ public static <I , O > Callable <O > asNullaryFunction (final Function <I , O > function , I input ) {
49+ return () -> function .apply (input );
50+ }
51+
52+ public static <I1 , I2 , O > Callable <O > asNullaryFunction (final BiFunction <I1 , I2 , O > function , I1 input1 ,
53+ I2 input2 ) {
54+ return () -> function .apply (input1 , input2 );
5555 }
5656
5757 /**
@@ -85,6 +85,7 @@ public static <I1, I2, I3, O> BiFunction<I1, I2, O> asBiFunction(final Function3
8585 return function3 .apply (in1 , in2 , in3 );
8686 };
8787 }
88+
8889 }
8990
9091 /**
@@ -111,6 +112,11 @@ public static <I, O> Function<I, O> asFunction(final Computer<I, O> computer,
111112 };
112113 }
113114
115+ public static <I , O > Callable <O > asNullaryFunction (final Computer <I , O > computer , final I input ,
116+ final Function <I , O > inputAwareSource ) {
117+ return Functions .asNullaryFunction (asFunction (computer , inputAwareSource ), input );
118+ }
119+
114120 public static <I1 , I2 , O > BiFunction <I1 , I2 , O > asBiFunction (final BiComputer <I1 , I2 , O > computer ,
115121 final Source <O > source ) {
116122 return (in1 , in2 ) -> {
@@ -129,6 +135,42 @@ public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(final BiComputer<I1
129135 };
130136 }
131137
138+ public static <I1 , I2 , I3 , O > Function3 <I1 , I2 , I3 , O > asFunction3 (final Computer3 <I1 , I2 , I3 , O > computer ,
139+ final Source <O > source ) {
140+ return (in1 , in2 , in3 ) -> {
141+ O out = source .create ();
142+ computer .compute (in1 , in2 , in3 , out );
143+ return out ;
144+ };
145+ }
146+
147+ public static <I1 , I2 , I3 , O > Function3 <I1 , I2 , I3 , O > asFunction3 (Computer3 <I1 , I2 , I3 , O > computer ,
148+ Function3 <I1 , I2 , I3 , O > inputAwareSource ) {
149+ return (in1 , in2 , in3 ) -> {
150+ O out = inputAwareSource .apply (in1 , in2 , in3 );
151+ computer .compute (in1 , in2 , in3 , out );
152+ return out ;
153+ };
154+ }
155+
156+ public static <I1 , I2 , I3 , I4 , O > Function4 <I1 , I2 , I3 , I4 , O > asFunction4 (
157+ final Computer4 <I1 , I2 , I3 , I4 , O > computer , final Source <O > source ) {
158+ return (in1 , in2 , in3 , in4 ) -> {
159+ O out = source .create ();
160+ computer .compute (in1 , in2 , in3 , in4 , out );
161+ return out ;
162+ };
163+ }
164+
165+ public static <I1 , I2 , I3 , I4 , O > Function4 <I1 , I2 , I3 , I4 , O > asFunction4 (
166+ Computer4 <I1 , I2 , I3 , I4 , O > computer , Function4 <I1 , I2 , I3 , I4 , O > inputAwareSource ) {
167+ return (in1 , in2 , in3 , in4 ) -> {
168+ O out = inputAwareSource .apply (in1 , in2 , in3 , in4 );
169+ computer .compute (in1 , in2 , in3 , in4 , out );
170+ return out ;
171+ };
172+ }
173+
132174 public static <I , O > OneToOneCommand <I , O > asCommand (final Computer <I , O > computer , I input , O output ) {
133175 OneToOneCommand <I , O > command = new OneToOneCommand <I , O >() {
134176 @ Override
@@ -162,4 +204,5 @@ public static <I1, I2, I3, O> BiComputer<I1, I2, O> asBiComputer(final Computer3
162204 }
163205
164206 }
207+
165208}
0 commit comments