Skip to content

Commit 2f844e7

Browse files
committed
Adds a new *Factory interface unique to each descriptor.
1 parent 4fc6ad9 commit 2f844e7

11 files changed

Lines changed: 194 additions & 24 deletions

File tree

CONTRIBUTORS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The following individuals and organizations have given their time and energy to help
22
with the creation and maintenance of Flapi.
33

4-
* Jiri Jetmar - FLAPI-126
5-
* Chris Wensel ([@cwensel](https://github.com/cwensel)) - [#5, #8, FLAPI-114]
4+
* Jiri Jetmar - [#159]
5+
* Chris Wensel ([@cwensel](https://github.com/cwensel)) - [#5, #8, #12, #197]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*********************************************************************
2+
Copyright 2015 the Flapi authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
********************************************************************/
16+
17+
package unquietcode.tools.flapi;
18+
19+
/**
20+
* Simple little ditty to provide things.
21+
*
22+
* This is meant to be replaced by the real type
23+
* contained within Java 8, whenever JDK 7 support
24+
* is discontinued.
25+
*
26+
* TODO delete after propagation
27+
*
28+
* @author Ben Fagin
29+
* @version 2015-01-16
30+
*/
31+
public interface Supplier<T> {
32+
T get();
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*********************************************************************
2+
Copyright 2015 the Flapi authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
********************************************************************/
16+
17+
package unquietcode.tools.flapi;
18+
19+
/**
20+
* Simple little ditty to provide things.
21+
*
22+
* This is meant to be replaced by the real type
23+
* contained within Java 8, whenever JDK 7 support
24+
* is discontinued.
25+
*
26+
* @author Ben Fagin
27+
* @version 2015-01-16
28+
*/
29+
public interface Supplier<T> {
30+
T get();
31+
}

src/main/java/unquietcode/tools/flapi/generator/AbstractGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ public JDefinedClass createStrongType(GeneratorContext ctx, StateClass state) {
140140
}
141141
};
142142

143+
protected static final TypeCreationStrategy FACTORY_INTERFACE_STRATEGY = new DefaultTypeCreationStrategy() {
144+
public JDefinedClass createStrongType(GeneratorContext ctx, StateClass state) {
145+
String name = state.getName()+"Factory";
146+
return ctx.getOrCreateInterface(state.getName(), name).first;
147+
}
148+
};
149+
143150
public static final TypeCreationStrategy WRAPPER_INTERFACE_STRATEGY = new DefaultTypeCreationStrategy() {
144151
public @Override JDefinedClass createStrongType(GeneratorContext ctx, StateClass state) {
145152
String name = state.getName()+"Builder";

src/main/java/unquietcode/tools/flapi/generator/GeneratorGenerator.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package unquietcode.tools.flapi.generator;
1818

1919
import com.sun.codemodel.*;
20+
import unquietcode.tools.flapi.Supplier;
2021
import unquietcode.tools.flapi.graph.components.StateClass;
2122
import unquietcode.tools.flapi.outline.GeneratorOutline;
2223
import unquietcode.tools.flapi.runtime.BlockInvocationHandler;
@@ -54,6 +55,11 @@ public JDefinedClass generate(StateClass topLevel, GeneratorOutline outline) {
5455
createDefaultFactoryMethod(helper, returnType, createMethod);
5556
}
5657

58+
// also add a static factory method
59+
JDefinedClass factory = FACTORY_INTERFACE_STRATEGY.createStrongType(ctx, topLevel);
60+
JMethod factoryInterfaceMethod = factory.method(0, returnType, createMethod.name());
61+
createFactoryMethod(generator, createMethod, helper, factory, factoryInterfaceMethod);
62+
5763
return generator;
5864
}
5965

@@ -115,7 +121,38 @@ private void addDefaultMethodBody(JVar pListeners, JVar handler, JClass returnTy
115121

116122
// return handler._proxy(Wrapper.class);
117123
createMethod.body()._return(
118-
handler.invoke("_proxy").arg(returnType.dotclass())
124+
handler.invoke("_proxy").arg(returnType.dotclass())
119125
);
120126
}
127+
128+
private void createFactoryMethod(JDefinedClass generator, JMethod createMethod, JClass helper, JClass factory, JMethod factoryInterfaceMethod) {
129+
130+
// Supplier<Helper>
131+
JClass helperSupplier = ref(Supplier.class).narrow(helper);
132+
133+
// public static Factory factory(Supplier<Helper> provider, ExecutionListener...listeners)
134+
JMethod factoryMethod = generator.method(JMod.PUBLIC+JMod.STATIC, factory, "factory");
135+
JVar pHelper = factoryMethod.param(JMod.FINAL, helperSupplier, "provider");
136+
JVar pListeners = factoryMethod.varParam(JMod.FINAL, ref(ExecutionListener.class), "listeners");
137+
138+
// return a new anonymous class
139+
JDefinedClass factoryImplementation = ctx.model.anonymousClass(factory);
140+
factoryMethod.body()._return(JExpr._new(factoryImplementation));
141+
142+
// implementation of the anonymous class
143+
JMethod factoryImplMethod = factoryImplementation.method(JMod.PUBLIC, factoryInterfaceMethod.type(), factoryInterfaceMethod.name());
144+
145+
// Helper helper = provider.get();
146+
JVar suppliedHelper = factoryImplMethod.body().decl(helper, "helper", pHelper.invoke("get"));
147+
148+
// return
149+
factoryImplMethod.body()._return(
150+
151+
// Generator.start(helper, listeners)
152+
generator.staticInvoke(createMethod)
153+
.arg(suppliedHelper)
154+
.arg(pListeners)
155+
)
156+
;
157+
}
121158
}

src/test/java/unquietcode/tools/flapi/examples/calculator/CalculatorBuilderExample.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import unquietcode.tools.flapi.Descriptor;
1212
import unquietcode.tools.flapi.DescriptorMaker;
1313
import unquietcode.tools.flapi.Flapi;
14+
import unquietcode.tools.flapi.Supplier;
1415
import unquietcode.tools.flapi.examples.calculator.builder.Calculation.CalculationBuilder;
1516
import unquietcode.tools.flapi.examples.calculator.builder.Calculator.CalculatorBuilder;
17+
import unquietcode.tools.flapi.examples.calculator.builder.Calculator.CalculatorFactory;
1618
import unquietcode.tools.flapi.examples.calculator.builder.Calculator.CalculatorGenerator;
1719

1820
import java.math.BigInteger;
@@ -37,17 +39,15 @@ public Descriptor descriptor() {
3739

3840
@Test
3941
public void basicUsage() {
40-
Result _result = CalculatorGenerator.begin(new CalculatorHelperImpl())
42+
Result result = CalculatorGenerator.begin(new CalculatorHelperImpl())
4143
.$(0)
4244
.plus(1)
4345
.plus(1)
4446
.power(5)
4547
.divide(2)
4648
.equals();
4749

48-
BigInteger result = _result.get();
49-
System.out.println(result);
50-
assertEquals(BigInteger.valueOf(16), result);
50+
assertEquals(16, result.get().intValue());
5151
}
5252

5353
// ------- - - ------- -------- - -- -- - - ---- - - - -----------
@@ -57,19 +57,36 @@ public static class Result extends AtomicReference<BigInteger> { }
5757
static CalculationBuilder.Start<Void> begin(int startingValue) {
5858
CalculatorBuilder.Start<Void> begin = CalculatorGenerator.begin(new CalculatorHelperImpl());
5959
CalculationBuilder.Start<Void> start = begin.$(startingValue);
60+
6061
return start;
6162
}
6263

6364
@Test
6465
public void cleanedUpUsage() {
65-
AtomicReference<BigInteger> result
66-
= begin(0)
66+
Result result = begin(0)
6767
.plus(1)
6868
.plus(1)
6969
.power(5)
7070
.divide(2)
7171
.equals();
7272

73-
System.out.println(result.get());
73+
assertEquals(16, result.get().intValue());
74+
}
75+
76+
@Test
77+
public void factoryUsage() {
78+
CalculatorFactory factory = CalculatorGenerator.factory(new Supplier<Calculator>() {
79+
public @Override Calculator get() {
80+
return new CalculatorHelperImpl();
81+
}
82+
});
83+
84+
Result result = factory.begin()
85+
.$(0)
86+
.plus(10)
87+
.minus(5)
88+
.equals();
89+
90+
assertEquals(5, result.get().intValue());
7491
}
7592
}

src/test/java/unquietcode/tools/flapi/examples/calculator/builder/Calculation/CalculationBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package unquietcode.tools.flapi.examples.calculator.builder.Calculation;
32

43
import javax.annotation.Generated;
@@ -12,9 +11,9 @@
1211
* Visit https://github.com/UnquietCode/Flapi for more information.
1312
*
1413
*
15-
* Generated on August 13, 2014 16:08:20 PDT using version 0.0-DEVELOPMENT
14+
* Generated on January 16, 2015 21:49:46 PST using version 0.0-DEVELOPMENT
1615
*/
17-
@Generated(value = "unquietcode.tools.flapi", date = "2014-08-13T16:08:20-07:00", comments = "generated using Flapi, the fluent API generator for Java")
16+
@Generated(value = "unquietcode.tools.flapi", date = "2015-01-16T21:49:46-08:00", comments = "generated using Flapi, the fluent API generator for Java, version 0.0-DEVELOPMENT")
1817
public interface CalculationBuilder {
1918
public interface Start<_ReturnType>
2019
extends CalculationBuilder_2abs_2divide_2minus_2mod_2plus_2power_2times<_ReturnType>

src/test/java/unquietcode/tools/flapi/examples/calculator/builder/Calculation/CalculationBuilder_2abs_2divide_2minus_2mod_2plus_2power_2times.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package unquietcode.tools.flapi.examples.calculator.builder.Calculation;
32

43
import unquietcode.tools.flapi.examples.calculator.CalculatorBuilderExample.Result;
@@ -17,10 +16,10 @@
1716
* Visit https://github.com/UnquietCode/Flapi for more information.
1817
*
1918
*
20-
* Generated on August 13, 2014 16:08:20 PDT using version 0.0-DEVELOPMENT
19+
* Generated on January 16, 2015 21:49:46 PST using version 0.0-DEVELOPMENT
2120
* @see unquietcode.tools.flapi.examples.calculator.Calculation
2221
*/
23-
@Generated(value = "unquietcode.tools.flapi", date = "2014-08-13T16:08:20-07:00", comments = "generated using Flapi, the fluent API generator for Java")
22+
@Generated(value = "unquietcode.tools.flapi", date = "2015-01-16T21:49:46-08:00", comments = "generated using Flapi, the fluent API generator for Java, version 0.0-DEVELOPMENT")
2423
public interface CalculationBuilder_2abs_2divide_2minus_2mod_2plus_2power_2times<_ReturnType> {
2524
@MethodInfo(type = TransitionType.Recursive)
2625
Start<_ReturnType> abs();

src/test/java/unquietcode/tools/flapi/examples/calculator/builder/Calculator/CalculatorBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
21
package unquietcode.tools.flapi.examples.calculator.builder.Calculator;
32

3+
import unquietcode.tools.flapi.runtime.ChainInfo;
44
import unquietcode.tools.flapi.runtime.MethodInfo;
55
import unquietcode.tools.flapi.runtime.TransitionType;
66

@@ -15,12 +15,12 @@
1515
* Visit https://github.com/UnquietCode/Flapi for more information.
1616
*
1717
*
18-
* Generated on August 13, 2014 16:08:20 PDT using version 0.0-DEVELOPMENT
18+
* Generated on January 16, 2015 21:49:46 PST using version 0.0-DEVELOPMENT
1919
*/
20-
@Generated(value = "unquietcode.tools.flapi", date = "2014-08-13T16:08:20-07:00", comments = "generated using Flapi, the fluent API generator for Java")
20+
@Generated(value = "unquietcode.tools.flapi", date = "2015-01-16T21:49:46-08:00", comments = "generated using Flapi, the fluent API generator for Java, version 0.0-DEVELOPMENT")
2121
public interface CalculatorBuilder<_ReturnType> {
22-
@MethodInfo(type = TransitionType.Ascending, chain = {
23-
unquietcode.tools.flapi.examples.calculator.builder.Calculation.CalculationBuilder.Start.class
22+
@MethodInfo(type = TransitionType.Ascending, chainInfo = {
23+
@ChainInfo(type = unquietcode.tools.flapi.examples.calculator.builder.Calculation.CalculationBuilder.Start.class, position = 1)
2424
})
2525
unquietcode.tools.flapi.examples.calculator.builder.Calculation.CalculationBuilder.Start<_ReturnType> $(int p0);
2626

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
/*********************************************************************
3+
Copyright 2015 the Flapi authors
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
********************************************************************/
17+
18+
package unquietcode.tools.flapi.examples.calculator.builder.Calculator;
19+
20+
import unquietcode.tools.flapi.examples.calculator.builder.Calculator.CalculatorBuilder.Start;
21+
22+
import javax.annotation.Generated;
23+
24+
25+
/**
26+
* This class was generated using Flapi, the fluent API generator for Java.
27+
* Modifications to this file will be lost upon regeneration.
28+
* You have been warned!
29+
*
30+
* Visit https://github.com/UnquietCode/Flapi for more information.
31+
*
32+
*
33+
* Generated on January 16, 2015 21:49:46 PST using version 0.0-DEVELOPMENT
34+
*/
35+
@Generated(value = "unquietcode.tools.flapi", date = "2015-01-16T21:49:46-08:00", comments = "generated using Flapi, the fluent API generator for Java, version 0.0-DEVELOPMENT")
36+
public interface CalculatorFactory {
37+
Start<Void> begin();
38+
}

0 commit comments

Comments
 (0)