Skip to content

Commit

Permalink
Support of user defined annotated anotations, overriding and templati…
Browse files Browse the repository at this point in the history
…ng. Fix #56
  • Loading branch information
jbgi committed Oct 17, 2016
1 parent 5a05de6 commit 11ca757
Show file tree
Hide file tree
Showing 64 changed files with 2,501 additions and 1,680 deletions.
2 changes: 1 addition & 1 deletion LICENSES
Expand Up @@ -5,7 +5,7 @@ Other useful modules (annotation and processor-api) are published under the GNU

Build scripts and examples are published under BSD-3 license:

Copyright (c) 2015, Jean-Baptiste Giraudeau <jb@giraudeau.info>
Copyright (c) 2016, Jean-Baptiste Giraudeau <jb@giraudeau.info>
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
15 changes: 13 additions & 2 deletions annotation/src/main/java/org/derive4j/Derive.java
Expand Up @@ -18,25 +18,36 @@
*/
package org.derive4j;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

import static org.derive4j.Make.catamorphism;
import static org.derive4j.Make.constructors;
import static org.derive4j.Make.getters;
import static org.derive4j.Make.hktCoerce;
import static org.derive4j.Make.lambdaVisitor;
import static org.derive4j.Make.lazyConstructor;
import static org.derive4j.Make.modifiers;
import static org.derive4j.Make.patternMatching;
import static org.derive4j.Visibility.Same;

@Target({})
@Target(ElementType.TYPE)
@Documented
public @interface Derive {

String inClass() default ":auto";

Visibility withVisibility() default Same;

Make[] make() default { lambdaVisitor, constructors, getters, modifiers, lazyConstructor, patternMatching, catamorphism };
Make[] make() default { lambdaVisitor,
constructors,
getters,
modifiers,
lazyConstructor,
patternMatching,
catamorphism,
hktCoerce };

Instances[] value() default {};

Expand Down
4 changes: 2 additions & 2 deletions annotation/src/main/java/org/derive4j/ExportAsPublic.java
Expand Up @@ -22,8 +22,8 @@
import java.lang.annotation.Target;

/**
* Annotate package private static methods of your @Data class with this annotation to have them re-exported as public the the corresponding
* generated class.
* Annotate package private static methods of your @Data class with this annotation to have them re-exported as public the the
* corresponding generated class.
*/
@Target(ElementType.METHOD)
public @interface ExportAsPublic {
Expand Down
4 changes: 2 additions & 2 deletions annotation/src/main/java/org/derive4j/Flavour.java
Expand Up @@ -77,8 +77,6 @@ public <R> R match(Cases<R> cases) {
}
};

public abstract <R> R match(Cases<R> cases);

public interface Cases<R> {
R Jdk();

Expand All @@ -95,4 +93,6 @@ public interface Cases<R> {
R Guava();
}

public abstract <R> R match(Cases<R> cases);

}
14 changes: 12 additions & 2 deletions annotation/src/main/java/org/derive4j/Make.java
Expand Up @@ -53,6 +53,14 @@ public <R> R match(Cases<R> cases) {
}
},

firstClassPatternMatching {
@Override
public <R> R match(Cases<R> cases) {

return cases.firstClassPatternMatching();
}
},

getters {
@Override
public <R> R match(Cases<R> cases) {
Expand Down Expand Up @@ -85,8 +93,6 @@ public <R> R match(Cases<R> cases) {
}
};

public abstract <R> R match(Cases<R> cases);

public interface Cases<R> {
R lambdaVisitor();

Expand All @@ -96,6 +102,8 @@ public interface Cases<R> {

R patternMatching();

R firstClassPatternMatching();

R getters();

R modifiers();
Expand All @@ -105,4 +113,6 @@ public interface Cases<R> {
R hktCoerce();
}

public abstract <R> R match(Cases<R> cases);

}
4 changes: 2 additions & 2 deletions annotation/src/main/java/org/derive4j/Visibility.java
Expand Up @@ -45,8 +45,6 @@ public <R> R match(VisibilityCases<R> cases) {
}
};

public abstract <R> R match(VisibilityCases<R> cases);

public interface VisibilityCases<R> {
R Same();

Expand All @@ -55,4 +53,6 @@ public interface VisibilityCases<R> {
R Smart();
}

public abstract <R> R match(VisibilityCases<R> cases);

}
2 changes: 1 addition & 1 deletion examples/LICENSE.txt
@@ -1,4 +1,4 @@
Copyright (c) 2015, Jean-Baptiste Giraudeau <jb@giraudeau.info>
Copyright (c) 2016, Jean-Baptiste Giraudeau <jb@giraudeau.info>
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/java/org/derive4j/example/Address.java
Expand Up @@ -30,10 +30,10 @@
@Data
public abstract class Address {

public abstract <R> R match(Cases<R> cases);

interface Cases<R> {
R Address(int number, String street);
}

public abstract <R> R match(Cases<R> cases);

}
4 changes: 2 additions & 2 deletions examples/src/main/java/org/derive4j/example/Contact.java
Expand Up @@ -30,6 +30,8 @@
@Data
public abstract class Contact {

public abstract <R> R match(Cases<R> cases);

interface Cases<R> {
R byEmail(String email);

Expand All @@ -38,6 +40,4 @@ interface Cases<R> {
R byMail(Address postalAddress);
}

public abstract <R> R match(Cases<R> cases);

}
26 changes: 17 additions & 9 deletions examples/src/main/java/org/derive4j/example/Event.java
Expand Up @@ -25,17 +25,17 @@
*/
package org.derive4j.example;

@JavasLangData
import org.derive4j.ArgOption;
import org.derive4j.Data;
import org.derive4j.Derive;
import org.derive4j.Make;
import org.derive4j.Visibility;

@data
@Data(arguments = ArgOption.checkedNotNull)
@Derive(withVisibility = Visibility.Smart, make = Make.getters)
public abstract class Event<T> {

interface Cases<T, R> {

R itemAdded(String itemName);

R itemRemoved(T ref, String itemName);

}

public abstract <X> X match(Cases<T, X> cases);

@Override
Expand All @@ -46,4 +46,12 @@ interface Cases<T, R> {

@Override
public abstract String toString();

interface Cases<T, R> {

R itemAdded(String itemName);

R itemRemoved(T ref, String itemName);

}
}
6 changes: 4 additions & 2 deletions examples/src/main/java/org/derive4j/example/Expr.java
Expand Up @@ -40,7 +40,8 @@ public abstract class Expr {

public static Integer eval(Expr expression) {

return expression.match(i -> i, (left, right) -> eval(left) + eval(right), (left, right) -> eval(left) * eval(right), (expr) -> -eval(expr));
return expression.match(i -> i, (left, right) -> eval(left) + eval(right), (left, right) -> eval(left) * eval(right),
(expr) -> -eval(expr));
}

public static void main(String[] args) {
Expand All @@ -49,7 +50,8 @@ public static void main(String[] args) {
System.out.println(eval(expr)); // (1+(2*(3*3))) = 19
}

public abstract <R> R match(@FieldNames("value") IntFunction<R> Const, @FieldNames({ "left", "right" }) BiFunction<Expr, Expr, R> Add,
public abstract <R> R match(@FieldNames("value") IntFunction<R> Const,
@FieldNames({ "left", "right" }) BiFunction<Expr, Expr, R> Add,
@FieldNames({ "left", "right" }) BiFunction<Expr, Expr, R> Mult, @FieldNames("expr") Function<Expr, R> Neg);

}
6 changes: 3 additions & 3 deletions examples/src/main/java/org/derive4j/example/Expression.java
Expand Up @@ -35,9 +35,6 @@
@Data
public abstract class Expression {

private static final Function<Expression, Integer> eval = Expressions.cata(value -> value, (left, right) -> left.get() + right.get(),
(left, right) -> left.get() * right.get(), expr -> -expr.get());

public static Integer eval(Expression expression) {

return eval.apply(expression);
Expand All @@ -49,6 +46,9 @@ public static void main(String[] args) {
System.out.println(eval(expr)); // (1+(2*(3*3))) = 19
}

private static final Function<Expression, Integer> eval = Expressions.cata(value -> value,
(left, right) -> left.get() + right.get(), (left, right) -> left.get() * right.get(), expr -> -expr.get());

public abstract <R> R match(Cases<R> cases);

interface Cases<R> {
Expand Down
12 changes: 6 additions & 6 deletions examples/src/main/java/org/derive4j/example/ExtendedEvent.java
Expand Up @@ -30,12 +30,6 @@
@Data
public abstract class ExtendedEvent<U> {

interface Cases<A, X> extends Event.Cases<A, X> {

X itemRenamed(A ref, String itemName);

}

public abstract <X> X match(Cases<U, X> cases);

@Override
Expand All @@ -46,4 +40,10 @@ interface Cases<A, X> extends Event.Cases<A, X> {

@Override
public abstract String toString();

interface Cases<A, X> extends Event.Cases<A, X> {

X itemRenamed(A ref, String itemName);

}
}
4 changes: 2 additions & 2 deletions examples/src/main/java/org/derive4j/example/GuavaRequest.java
Expand Up @@ -32,6 +32,8 @@
@Data(flavour = Guava)
public abstract class GuavaRequest {

public abstract <R> R match(Cases<R> cases);

interface Cases<R> {
R GET(String path);

Expand All @@ -42,6 +44,4 @@ interface Cases<R> {
R POST(String path, String body);
}

public abstract <R> R match(Cases<R> cases);

}
25 changes: 25 additions & 0 deletions examples/src/main/java/org/derive4j/example/IntNewType.java
@@ -1,3 +1,28 @@
/*
* Copyright (c) 2015, Jean-Baptiste Giraudeau <jb@giraudeau.info>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.derive4j.example;

import java.util.function.IntFunction;
Expand Down
20 changes: 10 additions & 10 deletions examples/src/main/java/org/derive4j/example/List.java
Expand Up @@ -41,10 +41,6 @@
@Data
public abstract class List<A> {

List() {

}

public static List<Integer> naturals() {

return integersFrom(0);
Expand All @@ -58,15 +54,19 @@ public static List<Integer> integersFrom(final int s) {
public static List<Integer> range(final int from, int toExclusive) {

return (from == toExclusive)
? nil()
: cons(from, lazy(() -> range(from + 1, toExclusive)));
? nil()
: cons(from, lazy(() -> range(from + 1, toExclusive)));
}

public static <A> List<A> iterate(A seed, UnaryOperator<A> op) {

return lazy(() -> cons(seed, iterate(op.apply(seed), op)));
}

List() {

}

public abstract <X> X list(Supplier<X> nil, @FieldNames({ "head", "tail" }) BiFunction<A, List<A>, X> cons);

public final <B> List<B> map(Function<A, B> f) {
Expand All @@ -82,8 +82,8 @@ public final List<A> append(final List<A> list) {
public final List<A> filter(Predicate<A> p) {

return lazy(() -> list(Lists::nil, (h, tail) -> p.test(h)
? cons(h, tail.filter(p))
: tail.filter(p)));
? cons(h, tail.filter(p))
: tail.filter(p)));
}

public final <B> List<B> bind(Function<A, List<B>> f) {
Expand All @@ -96,8 +96,8 @@ public final <B> List<B> bind(Function<A, List<B>> f) {
public final List<A> take(int n) {

return (n <= 0)
? nil()
: lazy(() -> list(Lists::nil, (head, tail) -> cons(head, tail.take(n - 1))));
? nil()
: lazy(() -> list(Lists::nil, (head, tail) -> cons(head, tail.take(n - 1))));
}

public final void forEach(Consumer<A> effect) {
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/java/org/derive4j/example/Person.java
Expand Up @@ -43,8 +43,6 @@
@Data
public abstract class Person {

public abstract <R> R match(@FieldNames({ "name", "contact" }) BiFunction<PersonName, Contact, R> Person);

public static void main(String[] args) {

Person joe = Person(Name0("Joe"), Contacts.byMail(Address(10, "Main St")));
Expand All @@ -62,4 +60,6 @@ public static void main(String[] args) {
System.out.println(newStreetNumber); // print "Optional[11]" !!
}

public abstract <R> R match(@FieldNames({ "name", "contact" }) BiFunction<PersonName, Contact, R> Person);

}
4 changes: 2 additions & 2 deletions examples/src/main/java/org/derive4j/example/PersonName.java
Expand Up @@ -52,8 +52,8 @@ public abstract class PersonName {
static Optional<PersonName> parseName(String value) {
// A name cannot be only spaces, must not start or and with space.
return (value.trim().isEmpty() || value.endsWith(" ") || value.startsWith(" "))
? Optional.empty()
: Optional.of(PersonNames.Name0(value));
? Optional.empty()
: Optional.of(PersonNames.Name0(value));
}

}

0 comments on commit 11ca757

Please sign in to comment.