This commit is contained in:
Daniel Mills
2021-08-10 07:26:41 -04:00
parent 2ebb9f2763
commit 70b659acbe
53 changed files with 244 additions and 453 deletions

View File

@@ -18,109 +18,87 @@
package com.volmit.iris.util.collection;
public class StateList
{
private final KList<String> states;
public class StateList {
private final KList<String> states;
public StateList(String... states)
{
this.states = new KList<String>(states);
public StateList(String... states) {
this.states = new KList<String>(states);
if(getBits() > 64)
{
throw new RuntimeException("StateLists cannot exceed 64 bits! You are trying to use " + getBits() + " bits!");
}
}
if (getBits() > 64) {
throw new RuntimeException("StateLists cannot exceed 64 bits! You are trying to use " + getBits() + " bits!");
}
}
public StateList(Enum<?>... states)
{
this.states = new KList<Enum<?>>().convert(Enum::name);
public StateList(Enum<?>... states) {
this.states = new KList<Enum<?>>().convert(Enum::name);
if(getBits() > 64)
{
throw new RuntimeException("StateLists cannot exceed 64 bits! You are trying to use " + getBits() + " bits!");
}
}
if (getBits() > 64) {
throw new RuntimeException("StateLists cannot exceed 64 bits! You are trying to use " + getBits() + " bits!");
}
}
public long max()
{
return (long) (Math.pow(2, getBits()) - 1);
}
public long max() {
return (long) (Math.pow(2, getBits()) - 1);
}
public KList<String> getEnabled(long list)
{
KList<String> f = new KList<>();
public KList<String> getEnabled(long list) {
KList<String> f = new KList<>();
for(String i : states)
{
if(is(list, i))
{
f.add(i);
}
}
for (String i : states) {
if (is(list, i)) {
f.add(i);
}
}
return f;
}
return f;
}
public long of(String... enabledStates)
{
long b = 0;
public long of(String... enabledStates) {
long b = 0;
for(String i : enabledStates)
{
b |= getBit(i);
}
for (String i : enabledStates) {
b |= getBit(i);
}
return b;
}
return b;
}
public long set(long list, String state, boolean enabled)
{
long bit = getBit(state);
boolean is = is(list, state);
public long set(long list, String state, boolean enabled) {
long bit = getBit(state);
boolean is = is(list, state);
if(enabled && !is)
{
return list | bit;
}
if (enabled && !is) {
return list | bit;
} else if (!enabled && is) {
return list ^ bit;
}
else if(!enabled && is)
{
return list ^ bit;
}
return list;
}
return list;
}
public boolean is(long list, String state) {
long bit = getBit(state);
public boolean is(long list, String state)
{
long bit = getBit(state);
return bit > 0 && (list & bit) == bit;
}
return bit > 0 && (list & bit) == bit;
}
public boolean hasBit(String state) {
return getBit(state) > 0;
}
public boolean hasBit(String state)
{
return getBit(state) > 0;
}
public long getBit(String state) {
return getBit(states.indexOf(state));
}
public long getBit(String state)
{
return getBit(states.indexOf(state));
}
public long getBit(int index) {
return (long) (index < 0 ? -1 : Math.pow(2, index));
}
public long getBit(int index)
{
return (long) (index < 0 ? -1 : Math.pow(2, index));
}
public int getBytes() {
return getBits() == 0 ? 0 : ((getBits() >> 2) + 1);
}
public int getBytes()
{
return getBits() == 0 ? 0 : ((getBits() >> 2) + 1);
}
public int getBits()
{
return states.size();
}
public int getBits() {
return states.size();
}
}