mirror of
https://github.com/Noratrieb/UselessClasses.git
synced 2026-01-14 08:35:10 +01:00
cool new classes that are very cool
This commit is contained in:
parent
707063f8ae
commit
b8dfd00554
10 changed files with 324 additions and 5 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
/target
|
||||||
|
/.idea
|
||||||
|
*.iml
|
||||||
|
|
@ -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> T IF(boolean condition, Supplier<T> then, Supplier<T> other) {
|
||||||
|
if (condition) {
|
||||||
|
return then.get();
|
||||||
|
} else {
|
||||||
|
return other.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
package com.github.nilstrieb.uselessclasses;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class tries to create a huge array of longs (1e+22 in total)
|
* This class tries to create a huge array of longs (1e+22 in total)
|
||||||
* This obviously causes an {@code OutOfMemoryError}
|
* This obviously causes an {@code OutOfMemoryError}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package com.github.nilstrieb.uselessclasses;
|
package com.github.nilstrieb.uselessclasses;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class adds a brand new syntax for if-statements!
|
* This class adds a brand new syntax for if-statements!
|
||||||
* You just need to statically import the methods
|
* You just need to statically import the methods
|
||||||
|
|
@ -19,9 +17,6 @@ public class Ifs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
interface Statements {
|
interface Statements {
|
||||||
void execute();
|
void execute();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.github.nilstrieb.uselessclasses;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Just like {@link Pointer} but Nullable. And always null
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
public class Nullpointer<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
140
src/main/java/com/github/nilstrieb/uselessclasses/Option.java
Normal file
140
src/main/java/com/github/nilstrieb/uselessclasses/Option.java
Normal file
|
|
@ -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 <T>
|
||||||
|
*/
|
||||||
|
public class Option<T> {
|
||||||
|
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 <V> Any Type
|
||||||
|
* @return A new Option
|
||||||
|
*/
|
||||||
|
public static <V> Option<V> of(V value) {
|
||||||
|
return new Option<>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPresent() {
|
||||||
|
return some != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an empty Option
|
||||||
|
*
|
||||||
|
* @param <V> Any Type
|
||||||
|
* @return A new Option with none
|
||||||
|
*/
|
||||||
|
public static <V> Option<V> none() {
|
||||||
|
return new Option<>(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map the Value inside the Optional
|
||||||
|
*
|
||||||
|
* @param function The operation
|
||||||
|
* @param <V> The new Type
|
||||||
|
* @return A new Optional
|
||||||
|
*/
|
||||||
|
public <V> Option<V> map(Function<? super T, V> 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<T> alternative) {
|
||||||
|
if (isPresent()) {
|
||||||
|
return some;
|
||||||
|
} else {
|
||||||
|
return alternative.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the value is present, get the value, else get the supplied argument.
|
||||||
|
* Is <b>not</b>not 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<T> or(Option<T> 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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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 <T>
|
||||||
|
*/
|
||||||
|
public class Pointer<T> {
|
||||||
|
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<Pointer<T>> pointToThis() {
|
||||||
|
return new Pointer<>(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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<String> some = Option.of("hallo");
|
||||||
|
Option<String> 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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<String> p = new Pointer<>(s);
|
||||||
|
Pointer<String> p2 = new Pointer<>(s2);
|
||||||
|
Pointer<String> p3 = new Pointer<>(s3);
|
||||||
|
|
||||||
|
assertEquals("nein", p2.deref());
|
||||||
|
|
||||||
|
Pointer<Pointer<String>> pp3 = p3.pointToThis();
|
||||||
|
assertEquals("vielleicht", pp3.deref().deref());
|
||||||
|
|
||||||
|
pp3.set(p);
|
||||||
|
assertEquals("ja", pp3.deref().deref());
|
||||||
|
|
||||||
|
Pointer<String> ja = new Pointer<>(s);
|
||||||
|
Pointer<Pointer<String>> op = ja.pointToThis();
|
||||||
|
Pointer<Pointer<String>> np = ja.pointToThis();
|
||||||
|
|
||||||
|
op.deref().set("lol");
|
||||||
|
assertEquals("lol", np.deref().deref());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void nullPointer() {
|
||||||
|
Nullpointer<Integer> n = new Nullpointer<>(null);
|
||||||
|
assertThrows(NullPointerException.class, n::deref);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue