Skip to content

Commit d6b3327

Browse files
committed
Allow registering listeners in Groovy DSL
Theres a few things I've identified that need to be looked into: - Generic bounds in both the mapping model and Groovy DSL - Removing usage of the Groovy DSL in the standard Lorenz tests - Splitting up the Groovy DSL unit test
1 parent f7b4a28 commit d6b3327

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

lorenz-dsl-groovy/src/main/java/org/cadixdev/lorenz/dsl/groovy/ClassMappingDsl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @author Jamie Mansfield
4444
* @since 0.6.0
4545
*/
46-
public class ClassMappingDsl<T extends ClassMapping<?, ?>> extends MappingDsl<T> {
46+
public class ClassMappingDsl<T extends ClassMapping<T, P>, P> extends MappingDsl<T, P> {
4747

4848
public ClassMappingDsl(final T mapping) {
4949
super(mapping);

lorenz-dsl-groovy/src/main/java/org/cadixdev/lorenz/dsl/groovy/MappingDsl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.cadixdev.lorenz.model.ExtensionKey;
2929
import org.cadixdev.lorenz.model.Mapping;
30+
import org.cadixdev.lorenz.util.MappingChangedListener;
3031

3132
/**
3233
* A DSL to simplify the manipulation of {@link Mapping}s in Groovy.
@@ -35,7 +36,7 @@
3536
* @author Jamie Mansfield
3637
* @since 0.6.0
3738
*/
38-
public class MappingDsl<T extends Mapping<?, ?>> {
39+
public class MappingDsl<T extends Mapping<T, P>, P> {
3940

4041
/**
4142
* The mapping manipulated by this DSL.
@@ -68,4 +69,14 @@ public <K> void extension(final ExtensionKey<K> key, final K value) {
6869
this.mapping.set(key, value);
6970
}
7071

72+
/**
73+
* Adds the given listener to the mapping.
74+
*
75+
* @param listener The mapping listener
76+
* @see Mapping#addListener(MappingChangedListener)
77+
*/
78+
public void listener(final MappingChangedListener<T, P> listener) {
79+
this.mapping.addListener(listener);
80+
}
81+
7182
}

lorenz-dsl-groovy/src/main/java/org/cadixdev/lorenz/dsl/groovy/MethodMappingDsl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import groovy.lang.Closure;
2929
import groovy.lang.DelegatesTo;
30+
import org.cadixdev.lorenz.model.ClassMapping;
3031
import org.cadixdev.lorenz.model.MethodMapping;
3132
import org.cadixdev.lorenz.model.MethodParameterMapping;
3233

@@ -36,7 +37,7 @@
3637
* @author Jamie Mansfield
3738
* @since 0.6.0
3839
*/
39-
public class MethodMappingDsl extends MappingDsl<MethodMapping> {
40+
public class MethodMappingDsl extends MappingDsl<MethodMapping, ClassMapping> {
4041

4142
public MethodMappingDsl(final MethodMapping mapping) {
4243
super(mapping);

lorenz/src/test/groovy/org/cadixdev/lorenz/test/MappingObserveSpec.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ class MappingObserveSpec extends Specification {
3636
given:
3737
final MappingSet mappings = MappingSetDsl.create {
3838
klass('a') {
39-
}.addListener({ TopLevelClassMapping mapping, String newName ->
40-
throw new IllegalArgumentException("beep boop")
41-
})
39+
listener { TopLevelClassMapping mapping, String newName ->
40+
throw new IllegalArgumentException("beep boop")
41+
}
42+
}
4243
}
4344

4445
when:

0 commit comments

Comments
 (0)