From b3d3b2cdb91d4bcbec792c3ee62d039d676d14a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Mu=C3=B1oz?= Date: Tue, 14 Mar 2023 23:29:19 +0100 Subject: [PATCH 1/2] refined types WIP --- refined/build.gradle | 3 + .../purefun/refined/RefinedDouble.java | 20 ++++++ .../purefun/refined/RefinedInteger.java | 20 ++++++ .../purefun/refined/RefinedString.java | 20 ++++++ .../tonivade/purefun/refined/TestInt.java | 8 +++ .../tonivade/purefun/refined/TestIntImpl.java | 68 ++++++++++++++++++ .../tonivade/purefun/refined/TestString.java | 8 +++ .../purefun/refined/TestStringImpl.java | 70 +++++++++++++++++++ settings.gradle | 2 +- 9 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 refined/build.gradle create mode 100644 refined/src/main/java/com/github/tonivade/purefun/refined/RefinedDouble.java create mode 100644 refined/src/main/java/com/github/tonivade/purefun/refined/RefinedInteger.java create mode 100644 refined/src/main/java/com/github/tonivade/purefun/refined/RefinedString.java create mode 100644 refined/src/test/java/com/github/tonivade/purefun/refined/TestInt.java create mode 100644 refined/src/test/java/com/github/tonivade/purefun/refined/TestIntImpl.java create mode 100644 refined/src/test/java/com/github/tonivade/purefun/refined/TestString.java create mode 100644 refined/src/test/java/com/github/tonivade/purefun/refined/TestStringImpl.java diff --git a/refined/build.gradle b/refined/build.gradle new file mode 100644 index 000000000..f7dc2b89a --- /dev/null +++ b/refined/build.gradle @@ -0,0 +1,3 @@ +dependencies { + api projects.purefunCore +} \ No newline at end of file diff --git a/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedDouble.java b/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedDouble.java new file mode 100644 index 000000000..4e6daf620 --- /dev/null +++ b/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedDouble.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2018-2023, Antonio Gabriel Muñoz Conejo + * Distributed under the terms of the MIT License + */ +package com.github.tonivade.purefun.refined; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.SOURCE; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(SOURCE) +@Target(TYPE) +public @interface RefinedDouble { + + double min() default Double.MIN_VALUE; + double max() default Double.MAX_VALUE; + +} diff --git a/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedInteger.java b/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedInteger.java new file mode 100644 index 000000000..20c64c764 --- /dev/null +++ b/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedInteger.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2018-2023, Antonio Gabriel Muñoz Conejo + * Distributed under the terms of the MIT License + */ +package com.github.tonivade.purefun.refined; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.SOURCE; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(SOURCE) +@Target(TYPE) +public @interface RefinedInteger { + + int min() default Integer.MIN_VALUE; + int max() default Integer.MAX_VALUE; + +} diff --git a/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedString.java b/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedString.java new file mode 100644 index 000000000..9a9859664 --- /dev/null +++ b/refined/src/main/java/com/github/tonivade/purefun/refined/RefinedString.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2018-2023, Antonio Gabriel Muñoz Conejo + * Distributed under the terms of the MIT License + */ +package com.github.tonivade.purefun.refined; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.SOURCE; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(SOURCE) +@Target(TYPE) +public @interface RefinedString { + + int minSize() default 0; + int maxSize() default Integer.MAX_VALUE; + String regex() default ""; +} diff --git a/refined/src/test/java/com/github/tonivade/purefun/refined/TestInt.java b/refined/src/test/java/com/github/tonivade/purefun/refined/TestInt.java new file mode 100644 index 000000000..6619e67b6 --- /dev/null +++ b/refined/src/test/java/com/github/tonivade/purefun/refined/TestInt.java @@ -0,0 +1,8 @@ +package com.github.tonivade.purefun.refined; + +import java.io.Serializable; + +@RefinedInteger(min = 0) +public interface TestInt extends Comparable, Serializable { + +} diff --git a/refined/src/test/java/com/github/tonivade/purefun/refined/TestIntImpl.java b/refined/src/test/java/com/github/tonivade/purefun/refined/TestIntImpl.java new file mode 100644 index 000000000..9b0f640d5 --- /dev/null +++ b/refined/src/test/java/com/github/tonivade/purefun/refined/TestIntImpl.java @@ -0,0 +1,68 @@ +package com.github.tonivade.purefun.refined; + +import java.io.Serial; +import java.util.Objects; + +import javax.annotation.processing.Generated; + +import com.github.tonivade.purefun.Precondition; + +@Generated("xxx") +public final class TestIntImpl extends Number implements TestInt { + + @Serial + private static final long serialVersionUID = 2426275725152618668L; + + private final int value; + + public TestIntImpl(int value) { + this.value = Precondition.checkPositive(value); + } + + @Override + public int intValue() { + return value; + } + + @Override + public long longValue() { + return value; + } + + @Override + public float floatValue() { + return value; + } + + @Override + public double doubleValue() { + return value; + } + + @Override + public int compareTo(TestIntImpl other) { + return this.value - other.value; + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TestIntImpl other = (TestIntImpl) obj; + return value == other.value; + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/refined/src/test/java/com/github/tonivade/purefun/refined/TestString.java b/refined/src/test/java/com/github/tonivade/purefun/refined/TestString.java new file mode 100644 index 000000000..97e44adf7 --- /dev/null +++ b/refined/src/test/java/com/github/tonivade/purefun/refined/TestString.java @@ -0,0 +1,8 @@ +package com.github.tonivade.purefun.refined; + +import java.io.Serializable; + +@RefinedString(minSize = 12, maxSize = 12) +public interface TestString extends Comparable, Serializable, CharSequence { + +} diff --git a/refined/src/test/java/com/github/tonivade/purefun/refined/TestStringImpl.java b/refined/src/test/java/com/github/tonivade/purefun/refined/TestStringImpl.java new file mode 100644 index 000000000..a93ef4e09 --- /dev/null +++ b/refined/src/test/java/com/github/tonivade/purefun/refined/TestStringImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2018-2023, Antonio Gabriel Muñoz Conejo + * Distributed under the terms of the MIT License + */ +package com.github.tonivade.purefun.refined; + +import java.io.Serial; +import java.util.Arrays; + +import javax.annotation.processing.Generated; + +import com.github.tonivade.purefun.Precondition; + +@Generated("xxx") +public final class TestStringImpl implements TestString { + + @Serial + private static final long serialVersionUID = 3297705339043486610L; + + private final byte[] value; + + public TestStringImpl(String value) { + this.value = Precondition.checkNonEmpty(value).getBytes(); + } + + @Override + public int length() { + return toString().length(); + } + + @Override + public char charAt(int index) { + return toString().charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return toString().subSequence(start, end); + } + + @Override + public int compareTo(TestString other) { + return this.toString().compareTo(other.toString()); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(value); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TestStringImpl other = (TestStringImpl) obj; + return Arrays.equals(value, other.value); + } + + @Override + public String toString() { + return new String(value); + } +} diff --git a/settings.gradle b/settings.gradle index 62ec1e07f..01c3ee597 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,7 +5,7 @@ rootProject.name = 'purefun-parent' include "core", "monad", "stream", "typeclasses", "optics", "effect", "free", "transformer", "instances", "annotation", "processor", - "generic", "control" + "generic", "control", "refined" rootProject.children.each { it.name = "purefun-" + it.name } From ab60d4721084ddd83815a1fc32517dd350eaf055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Mu=C3=B1oz?= Date: Sat, 15 Apr 2023 23:56:01 +0200 Subject: [PATCH 2/2] add refined module to aggregation reports --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index 3ae681895..cf5a3765b 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ dependencies { jacocoAggregation projects.purefunStream jacocoAggregation projects.purefunTransformer jacocoAggregation projects.purefunTypeclasses + jacocoAggregation projects.purefunRefined testReportAggregation projects.purefunControl testReportAggregation projects.purefunCore @@ -38,6 +39,7 @@ dependencies { testReportAggregation projects.purefunStream testReportAggregation projects.purefunTransformer testReportAggregation projects.purefunTypeclasses + testReportAggregation projects.purefunRefined } reporting {