Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API-8] Improve Tristate #2137

Merged
merged 6 commits into from
Jul 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions src/main/java/org/spongepowered/api/util/Tristate.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,27 @@
*/
package org.spongepowered.api.util;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Represents a simple tristate.
*/
public enum Tristate {
TRUE(true) {
@Override
public Tristate and(Tristate other) {
return other == TRUE || other == UNDEFINED ? TRUE : FALSE;
return other == FALSE ? FALSE : TRUE;
}

@Override
public Tristate or(Tristate other) {
return TRUE;
}

@Override
public Tristate not() {
return FALSE;
}
},
FALSE(false) {
@Override
Expand All @@ -49,6 +56,11 @@ public Tristate and(Tristate other) {
public Tristate or(Tristate other) {
return other == TRUE ? TRUE : FALSE;
}

@Override
public Tristate not() {
return TRUE;
}
},
UNDEFINED(false) {
@Override
Expand All @@ -60,6 +72,11 @@ public Tristate and(Tristate other) {
public Tristate or(Tristate other) {
return other;
}

@Override
public Tristate not() {
return UNDEFINED;
}
};

private final boolean val;
Expand All @@ -69,7 +86,7 @@ public Tristate or(Tristate other) {
}

/**
* Return the appropriate tristate for a given boolean value.
* Returns the appropriate tristate for a given boolean value.
*
* @param val The boolean value
* @return The appropriate tristate
Expand All @@ -78,6 +95,21 @@ public static Tristate fromBoolean(boolean val) {
return val ? TRUE : FALSE;
}

/**
* Returns the appropriate tristate for a given nullable boolean value,
* where null equates to {@link Tristate#UNDEFINED}.
*
* @param val The nullable boolean value
* @return The appropriate tristate
*/
public static Tristate fromNullableBoolean(@Nullable Boolean val) {
if (val == null) {
return UNDEFINED;
} else {
return val ? TRUE : FALSE;
}
}

/**
* ANDs this tristate with another tristate.
*
Expand All @@ -94,6 +126,13 @@ public static Tristate fromBoolean(boolean val) {
*/
public abstract Tristate or(Tristate other);

/**
* Negates this tristate.
*
* @return The result
*/
public abstract Tristate not();

/**
* Returns the boolean representation of this tristate.
*
Expand All @@ -102,4 +141,23 @@ public static Tristate fromBoolean(boolean val) {
public boolean asBoolean() {
return this.val;
}

/**
* Returns the nullable boolean representation of this tristate,
* where {@link Tristate#UNDEFINED} equates to null.
*
* <p>Note: To ensure that there is a one-to-one mapping of nullable
* booleans to and from {@link Tristate}, this method will return null
* rather than use an {@link java.util.Optional Optional}.</p>
*
* @return The nullable boolean tristate representation
*/
@Nullable
public Boolean asNullableBoolean() {
if (this == Tristate.UNDEFINED) {
return null;
} else {
return this.val;
}
}
}