forked from Thorfusion/Mekanism-Community-Edition
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
2,285 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mekanism.api; | ||
|
||
public enum AutomationType { | ||
/** | ||
* External interaction (third party interacting with a machine) | ||
*/ | ||
EXTERNAL, | ||
/** | ||
* Internal interaction (machine interacting with its own contents) | ||
*/ | ||
INTERNAL, | ||
/** | ||
* Manual interaction (player interacting manually, such as in a GUI) | ||
*/ | ||
MANUAL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package mekanism.api; | ||
|
||
import java.util.function.Predicate; | ||
import mekanism.api.annotations.NothingNullByDefault; | ||
import mekanism.api.functions.ConstantPredicates; | ||
|
||
/** | ||
* Interface for enum's to make them easily incremental | ||
*/ | ||
@NothingNullByDefault | ||
public interface IIncrementalEnum<TYPE extends Enum<TYPE> & IIncrementalEnum<TYPE>> { | ||
|
||
/** | ||
* Gets the next "valid" element | ||
* | ||
* @param isValid Predicate defining if an element is valid | ||
* | ||
* @return The next "valid" element | ||
*/ | ||
default TYPE getNext(Predicate<TYPE> isValid) { | ||
TYPE next = byIndex(ordinal() + 1); | ||
while (!isValid.test(next)) { | ||
if (next == this) { | ||
//Don't loop forever, and just return our self instead given we got back to our self | ||
return next; | ||
} | ||
next = byIndex(next.ordinal() + 1); | ||
} | ||
//Once we break out of the loop we know we have a valid entry | ||
return next; | ||
} | ||
|
||
/** | ||
* Gets the previous "valid" element | ||
* | ||
* @param isValid Predicate defining if an element is valid | ||
* | ||
* @return The previous "valid" element | ||
*/ | ||
default TYPE getPrevious(Predicate<TYPE> isValid) { | ||
TYPE previous = byIndex(ordinal() - 1); | ||
while (!isValid.test(previous)) { | ||
if (previous == this) { | ||
//Don't loop forever, and just return our self instead given we got back to our self | ||
return previous; | ||
} | ||
previous = byIndex(previous.ordinal() - 1); | ||
} | ||
//Once we break out of the loop we know we have a valid entry | ||
return previous; | ||
} | ||
|
||
/** | ||
* Helper method to get a value by index rather than having to duplicate all the previous/next logic. | ||
*/ | ||
TYPE byIndex(int index); | ||
|
||
/** | ||
* {@link Enum#ordinal()} | ||
*/ | ||
int ordinal(); | ||
|
||
/** | ||
* Gets the next "valid" element | ||
* | ||
* @return The next "valid" element | ||
*/ | ||
default TYPE getNext() { | ||
return getNext(ConstantPredicates.alwaysTrue()); | ||
} | ||
|
||
/** | ||
* Gets the previous "valid" element | ||
* | ||
* @return The previous "valid" element | ||
*/ | ||
default TYPE getPrevious() { | ||
return getPrevious(ConstantPredicates.alwaysTrue()); | ||
} | ||
|
||
/** | ||
* Gets the "valid" element that is offset by the given shift | ||
* | ||
* @param shift Shift to perform, may be negative to indicate going backwards | ||
* | ||
* @return The "valid" element that is offset by the given shift | ||
* | ||
* @implNote Default implementation assumes all elements are "valid", override this if that is not the case. | ||
*/ | ||
default TYPE adjust(int shift) { | ||
return shift == 0 ? (TYPE) this : byIndex(ordinal() + shift); | ||
} | ||
|
||
/** | ||
* Gets the "valid" element that is offset by the given shift with the given validity predicate. | ||
* | ||
* @param shift Shift to perform, may be negative to indicate going backwards | ||
* @param isValid Predicate defining if an element is valid | ||
* | ||
* @return The "valid" element that is offset by the given shift. If no elements are "valid" returns the current element. | ||
*/ | ||
default TYPE adjust(int shift, Predicate<TYPE> isValid) { | ||
TYPE result = (TYPE) this; | ||
while (shift < 0) { | ||
shift++; | ||
result = result.getPrevious(isValid); | ||
} | ||
while (shift > 0) { | ||
shift--; | ||
result = result.getNext(isValid); | ||
} | ||
return result; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/mekanism/api/annotations/FieldsAreNotNullByDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mekanism.api.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.meta.TypeQualifierDefault; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Interface to declare that all fields in a class are {@link @NotNull} | ||
*/ | ||
@NotNull | ||
@Nonnull//Note: Must use the javax nonnull for intellij to recognize it properly in warnings | ||
@TypeQualifierDefault(ElementType.FIELD) | ||
@Retention(RetentionPolicy.CLASS) | ||
public @interface FieldsAreNotNullByDefault { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/mekanism/api/annotations/NonNullSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mekanism.api.annotations; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Equivalent to {@link Supplier}, except with nonnull contract. | ||
* | ||
* @see Supplier | ||
*/ | ||
@FunctionalInterface | ||
public interface NonNullSupplier<T> | ||
{ | ||
@NotNull T get(); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/mekanism/api/annotations/NothingNullByDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mekanism.api.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.meta.TypeQualifierDefault; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Interface to declare that all fields, methods, and parameters in a class are {@link @NotNull} | ||
*/ | ||
@NotNull | ||
@Nonnull//Note: Must use the javax nonnull for intellij to recognize it properly in override warnings | ||
@TypeQualifierDefault({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.CLASS) | ||
public @interface NothingNullByDefault { | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/mekanism/api/annotations/ParametersAreNotNullByDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mekanism.api.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.meta.TypeQualifierDefault; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Interface to declare that all parameters in a class are {@link @NotNull} | ||
*/ | ||
@NotNull | ||
@Nonnull//Note: Must use the javax nonnull for intellij to recognize it properly in warnings | ||
@TypeQualifierDefault(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.CLASS) | ||
public @interface ParametersAreNotNullByDefault { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/mekanism/api/functions/ConstantPredicates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package mekanism.api.functions; | ||
|
||
import java.util.function.BiPredicate; | ||
import java.util.function.Predicate; | ||
import mekanism.api.AutomationType; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Helper class to reduce having to create duplicate objects for constant predicates. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public class ConstantPredicates { | ||
|
||
private ConstantPredicates() { | ||
} | ||
|
||
private static final Predicate<Object> alwaysTrue = t -> true; | ||
private static final BiPredicate<Object, Object> alwaysTrueBi = (t, u) -> true; | ||
private static final TriPredicate<Object, Object, Object> alwaysTrueTri = (t, u, v) -> true; | ||
|
||
private static final Predicate<Object> alwaysFalse = t -> false; | ||
private static final BiPredicate<Object, Object> alwaysFalseBi = (t, u) -> false; | ||
private static final TriPredicate<Object, Object, Object> alwaysFalseTri = (t, u, v) -> false; | ||
|
||
private static final BiPredicate<Object, @NotNull AutomationType> internalOnly = (t, automationType) -> automationType == AutomationType.INTERNAL; | ||
private static final BiPredicate<Object, @NotNull AutomationType> notExternal = (t, automationType) -> automationType != AutomationType.EXTERNAL; | ||
|
||
/** | ||
* Returns a predicate that returns {@code true} for any input. | ||
*/ | ||
public static <T> Predicate<T> alwaysTrue() { | ||
return (Predicate<T>) alwaysTrue; | ||
} | ||
|
||
/** | ||
* Returns a bi predicate that returns {@code true} for any input. | ||
*/ | ||
public static <T, U> BiPredicate<T, U> alwaysTrueBi() { | ||
return (BiPredicate<T, U>) alwaysTrueBi; | ||
} | ||
|
||
/** | ||
* Returns a tri predicate that returns {@code true} for any input. | ||
*/ | ||
public static <T, U, V> TriPredicate<T, U, V> alwaysTrueTri() { | ||
return (TriPredicate<T, U, V>) alwaysTrueTri; | ||
} | ||
|
||
/** | ||
* Returns a predicate that returns {@code false} for any input. | ||
*/ | ||
public static <T> Predicate<T> alwaysFalse() { | ||
return (Predicate<T>) alwaysFalse; | ||
} | ||
|
||
/** | ||
* Returns a bi predicate that returns {@code false} for any input. | ||
*/ | ||
public static <T, V> BiPredicate<T, V> alwaysFalseBi() { | ||
return (BiPredicate<T, V>) alwaysFalseBi; | ||
} | ||
|
||
/** | ||
* Returns a tri predicate that returns {@code false} for any input. | ||
*/ | ||
public static <T, U, V> TriPredicate<T, U, V> alwaysFalseTri() { | ||
return (TriPredicate<T, U, V>) alwaysFalseTri; | ||
} | ||
|
||
/** | ||
* Returns a bi predicate that returns {@code true} for any input when the automation type is internal. | ||
* | ||
* @since 10.3.3 | ||
*/ | ||
public static <T> BiPredicate<T, @NotNull AutomationType> internalOnly() { | ||
return (BiPredicate<T, @NotNull AutomationType>) internalOnly; | ||
} | ||
|
||
/** | ||
* Returns a bi predicate that returns {@code true} for any input when the automation type is not external. | ||
* | ||
* @since 10.3.3 | ||
*/ | ||
public static <T> BiPredicate<T, @NotNull AutomationType> notExternal() { | ||
return (BiPredicate<T, @NotNull AutomationType>) notExternal; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package mekanism.api.functions; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Represents a supplier of {@code float}-valued results. This is the {@code float}-producing primitive specialization of {@link Supplier}. | ||
* | ||
* <p>There is no requirement that a new or distinct result be returned each time the supplier is invoked. | ||
* | ||
* <p>This is a <a href="package-summary.html">functional interface</a> whose functional method is {@link #getAsFloat()}. | ||
* | ||
* @see Supplier | ||
*/ | ||
@FunctionalInterface | ||
public interface FloatSupplier { | ||
|
||
/** | ||
* Gets a result. | ||
* | ||
* @return a result | ||
*/ | ||
float getAsFloat(); | ||
} |
Oops, something went wrong.