From b8dfd00554df5e3206375c6f50284a303c0b1834 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Tue, 25 May 2021 15:48:41 +0200 Subject: [PATCH] cool new classes that are very cool --- .gitignore | 3 + .../uselessclasses/ExpressionIf.java | 17 +++ .../github/nilstrieb/uselessclasses/Hell.java | 2 + .../github/nilstrieb/uselessclasses/Ifs.java | 5 - .../nilstrieb/uselessclasses/Nullpointer.java | 24 +++ .../nilstrieb/uselessclasses/Option.java | 140 ++++++++++++++++++ .../nilstrieb/uselessclasses/Pointer.java | 55 +++++++ .../uselessclasses/ExpressionIfTest.java | 16 ++ .../nilstrieb/uselessclasses/OptionTest.java | 27 ++++ .../nilstrieb/uselessclasses/PointerTest.java | 40 +++++ 10 files changed, 324 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 src/main/java/com/github/nilstrieb/uselessclasses/ExpressionIf.java create mode 100644 src/main/java/com/github/nilstrieb/uselessclasses/Nullpointer.java create mode 100644 src/main/java/com/github/nilstrieb/uselessclasses/Option.java create mode 100644 src/main/java/com/github/nilstrieb/uselessclasses/Pointer.java create mode 100644 src/main/test/com/github/nilstrieb/uselessclasses/ExpressionIfTest.java create mode 100644 src/main/test/com/github/nilstrieb/uselessclasses/OptionTest.java create mode 100644 src/main/test/com/github/nilstrieb/uselessclasses/PointerTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d114b83 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/.idea +*.iml \ No newline at end of file diff --git a/src/main/java/com/github/nilstrieb/uselessclasses/ExpressionIf.java b/src/main/java/com/github/nilstrieb/uselessclasses/ExpressionIf.java new file mode 100644 index 0000000..f09221f --- /dev/null +++ b/src/main/java/com/github/nilstrieb/uselessclasses/ExpressionIf.java @@ -0,0 +1,17 @@ +package com.github.nilstrieb.uselessclasses; + +import java.util.function.Supplier; + +/** +A bit like {@link Ifs} except a tiny bit useful + */ +public class ExpressionIf { + + public static T IF(boolean condition, Supplier then, Supplier other) { + if (condition) { + return then.get(); + } else { + return other.get(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/github/nilstrieb/uselessclasses/Hell.java b/src/main/java/com/github/nilstrieb/uselessclasses/Hell.java index aa41bde..e8c37e1 100644 --- a/src/main/java/com/github/nilstrieb/uselessclasses/Hell.java +++ b/src/main/java/com/github/nilstrieb/uselessclasses/Hell.java @@ -1,3 +1,5 @@ +package com.github.nilstrieb.uselessclasses; + /** * This class tries to create a huge array of longs (1e+22 in total) * This obviously causes an {@code OutOfMemoryError} diff --git a/src/main/java/com/github/nilstrieb/uselessclasses/Ifs.java b/src/main/java/com/github/nilstrieb/uselessclasses/Ifs.java index 02358a8..0d1f1aa 100644 --- a/src/main/java/com/github/nilstrieb/uselessclasses/Ifs.java +++ b/src/main/java/com/github/nilstrieb/uselessclasses/Ifs.java @@ -1,7 +1,5 @@ package com.github.nilstrieb.uselessclasses; -import java.util.function.Consumer; - /** * This class adds a brand new syntax for if-statements! * You just need to statically import the methods @@ -19,9 +17,6 @@ public class Ifs { } } - - - interface Statements { void execute(); } diff --git a/src/main/java/com/github/nilstrieb/uselessclasses/Nullpointer.java b/src/main/java/com/github/nilstrieb/uselessclasses/Nullpointer.java new file mode 100644 index 0000000..1f4f7f6 --- /dev/null +++ b/src/main/java/com/github/nilstrieb/uselessclasses/Nullpointer.java @@ -0,0 +1,24 @@ +package com.github.nilstrieb.uselessclasses; + +/** + * Just like {@link Pointer} but Nullable. And always null + * @param + */ +public class Nullpointer { + + /** + * Create a new Nullpointer with a value. + * @param value Should be null + */ + @SuppressWarnings("unused") + public Nullpointer(T value) { + } + + /** + * Dereference the Nullpointer + * @return nothing ever + */ + public T deref() { + throw new NullPointerException("yeah this is a nullpointer what a surprise"); + } +} diff --git a/src/main/java/com/github/nilstrieb/uselessclasses/Option.java b/src/main/java/com/github/nilstrieb/uselessclasses/Option.java new file mode 100644 index 0000000..ce0966b --- /dev/null +++ b/src/main/java/com/github/nilstrieb/uselessclasses/Option.java @@ -0,0 +1,140 @@ +package com.github.nilstrieb.uselessclasses; + +import java.util.NoSuchElementException; +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * Like {@link java.util.Optional} but a bit different and worse + * Terminology derived from Rusts Option enum which is superior in every way to Javas {@link java.util.Optional} + * + * @param + */ +public class Option { + private final T some; + + private Option(T value) { + this.some = value; + } + + /** + * Construct a new Option from a Value, will be none if it is nulls + * + * @param value The value + * @param Any Type + * @return A new Option + */ + public static Option of(V value) { + return new Option<>(value); + } + + public boolean isPresent() { + return some != null; + } + + /** + * Construct an empty Option + * + * @param Any Type + * @return A new Option with none + */ + public static Option none() { + return new Option<>(null); + } + + /** + * Map the Value inside the Optional + * + * @param function The operation + * @param The new Type + * @return A new Optional + */ + public Option map(Function function) { + if (isPresent()) { + return Option.of(function.apply(some)); + } else { + return Option.none(); + } + } + + /** + * Get the value, throws an Exception if no value is present + * + * @return the value + */ + public T unwrap() { + if (isPresent()) { + return some; + } else { + throw new NoSuchElementException("Tried unwrapping none"); + } + } + + /** + * Get the value, throws an Exception with a message if no value is present + * + * @param msg The error message + * @return the value + */ + public T expect(String msg) { + if (isPresent()) { + return some; + } else { + throw new NoSuchElementException("Tried unwrapping none: " + msg); + } + } + + /** + * If the value is present, get the value, else get the supplied argument. + * Is lazy + * + * @param alternative The other value + * @return A non-null value + */ + public T getOrElse(Supplier alternative) { + if (isPresent()) { + return some; + } else { + return alternative.get(); + } + } + + /** + * If the value is present, get the value, else get the supplied argument. + * Is notnot lazy + * + * @param alternative The other value + * @return A non-null value + */ + public T getOrElseTake(T alternative) { + if (isPresent()) { + return some; + } else { + return alternative; + } + } + + /** + * Returns this if it is Some, else the other if it is Some, and this if both are None + * @param other Another Option of the same type + * @return An Option + */ + public Option or(Option other) { + if (isPresent()) { + return this; + } else if (other.isPresent()) { + return other; + } else { + return this; + } + } + + @Override + public String toString() { + if (isPresent()) { + return "Some(" + some.toString() + ")"; + } else { + return "None"; + } + } +} diff --git a/src/main/java/com/github/nilstrieb/uselessclasses/Pointer.java b/src/main/java/com/github/nilstrieb/uselessclasses/Pointer.java new file mode 100644 index 0000000..7c1774a --- /dev/null +++ b/src/main/java/com/github/nilstrieb/uselessclasses/Pointer.java @@ -0,0 +1,55 @@ +package com.github.nilstrieb.uselessclasses; + +/** + * We did it, pointers in Java! + * Is basically a wrapper around a single reference. Can be used to have deep pointers, like several levels deep + * This amazing pointer can also NOT be null, for nulls use an {@link Option} wrapper + * Is also always safe since there is a Garbage Collector + * + * @param + */ +public class Pointer { + private T value; + + /** + * Create a new pointer to some object + * + * @param value non-null + */ + public Pointer(T value) { + if (value == null) { + throw new NullPointerException("Literally a null pointer which is not allowed in my nice null safe world"); + } + this.value = value; + } + + /** + * Get the value this pointer is pointer to + * + * @return The value + */ + public T deref() { + return value; + } + + /** + * Set the pointer value + * + * @param newValue non-null + */ + public void set(T newValue) { + if (value == null) { + throw new NullPointerException("Literally a null pointer which is not allowed in my nice null safe world"); + } + this.value = newValue; + } + + /** + * Gets a new pointer which points to this pointer for simple nesting + * + * @return A new pointer + */ + public Pointer> pointToThis() { + return new Pointer<>(this); + } +} diff --git a/src/main/test/com/github/nilstrieb/uselessclasses/ExpressionIfTest.java b/src/main/test/com/github/nilstrieb/uselessclasses/ExpressionIfTest.java new file mode 100644 index 0000000..6159d5c --- /dev/null +++ b/src/main/test/com/github/nilstrieb/uselessclasses/ExpressionIfTest.java @@ -0,0 +1,16 @@ +package com.github.nilstrieb.uselessclasses; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import static com.github.nilstrieb.uselessclasses.ExpressionIf.*; + +class ExpressionIfTest { + + @Test + void normal() { + String result = IF(1 == 1, () -> "Hallo", () -> "Nein"); + assertEquals("Hallo", result); + } + +} \ No newline at end of file diff --git a/src/main/test/com/github/nilstrieb/uselessclasses/OptionTest.java b/src/main/test/com/github/nilstrieb/uselessclasses/OptionTest.java new file mode 100644 index 0000000..185ba68 --- /dev/null +++ b/src/main/test/com/github/nilstrieb/uselessclasses/OptionTest.java @@ -0,0 +1,27 @@ +package com.github.nilstrieb.uselessclasses; + +import org.junit.jupiter.api.Test; + +import java.util.NoSuchElementException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class OptionTest { + + @Test + void test() { + Option some = Option.of("hallo"); + Option none = Option.none(); + + assertThrows(NoSuchElementException.class, none::unwrap); + assertEquals("hallo", some.unwrap()); + + assertEquals("Ttest", none.getOrElse(() -> "Ttest")); + assertEquals("Ttest", none.getOrElseTake("Ttest")); + + assertEquals("Some(hallo)", some.toString()); + assertEquals("None", none.toString()); + } + +} \ No newline at end of file diff --git a/src/main/test/com/github/nilstrieb/uselessclasses/PointerTest.java b/src/main/test/com/github/nilstrieb/uselessclasses/PointerTest.java new file mode 100644 index 0000000..9a66b27 --- /dev/null +++ b/src/main/test/com/github/nilstrieb/uselessclasses/PointerTest.java @@ -0,0 +1,40 @@ +package com.github.nilstrieb.uselessclasses; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PointerTest { + @Test + void test() { + String s = "ja"; + String s2 = "nein"; + String s3 = "vielleicht"; + + Pointer p = new Pointer<>(s); + Pointer p2 = new Pointer<>(s2); + Pointer p3 = new Pointer<>(s3); + + assertEquals("nein", p2.deref()); + + Pointer> pp3 = p3.pointToThis(); + assertEquals("vielleicht", pp3.deref().deref()); + + pp3.set(p); + assertEquals("ja", pp3.deref().deref()); + + Pointer ja = new Pointer<>(s); + Pointer> op = ja.pointToThis(); + Pointer> np = ja.pointToThis(); + + op.deref().set("lol"); + assertEquals("lol", np.deref().deref()); + } + + @Test + void nullPointer() { + Nullpointer n = new Nullpointer<>(null); + assertThrows(NullPointerException.class, n::deref); + } + +} \ No newline at end of file