Skip to content

Commit

Permalink
updated Lightshow
Browse files Browse the repository at this point in the history
  • Loading branch information
cuttestkittensrule committed Feb 16, 2024
1 parent f030b25 commit e347000
Showing 1 changed file with 18 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package com.team2813.lib2813.subsystems.lightshow;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;

import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj2.command.Subsystem;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
Expand All @@ -30,86 +23,41 @@ public boolean apply() {
};
};
protected Set<State> states = new HashSet<>();
protected Optional<State> defaultState;
protected Consumer<Color> colorConsumer;
protected Optional<? extends State> defaultState;
protected Consumer<? super Color> colorConsumer;

/**
* Creates a new Lightshow subsystem. Uses the given {@code enumClass} to get a
* Creates a new Lightshow subsystem using an enum. Uses the given {@code enumClass} to get a
* list of {@link State}s to use.
*
* @param <T> The type of an enum that implements state
* @param enumClass
* @param colorConsumer
*/
protected <T extends Enum<T> & State> Lightshow(Class<T> enumClass, Consumer<Color> colorConsumer) {
protected <T extends Enum<T> & State> Lightshow(Class<T> enumClass, Consumer<? super Color> colorConsumer) {
addStates(enumClass);
this.colorConsumer = colorConsumer;
}

protected Lightshow(Set<State> states, Consumer<Color> colorConsumer) {
/**
* Creates a new Lightshow subsystem with a set of states.
* @param states
* @param colorConsumer
*/
protected Lightshow(Set<? extends State> states, Consumer<? super Color> colorConsumer) {
addStates(states);
this.colorConsumer = colorConsumer;
}

public final <T extends Enum<T> & State> void addStates(Class<T> enumClass) {
public final <T extends Enum<T> & State> void addStates(Class<? extends T> enumClass) {
T[] constants = enumClass.getEnumConstants();
states.addAll(Arrays.asList(constants));
if (defaultState.isEmpty()) {
resolveDefault(enumClass);
}
}

public final void addStates(Set<State> states) {
public final void addStates(Set<? extends State> states) {
this.states.addAll(states);
}

/**
* Resolves the default color from an enum class
*
* @param <T> the enum type to get from
* @param enumClass the class of the enum to get from
* @return an optional describing the default state, or an empty optional if no
* default state was found
*/
protected final <T extends Enum<T> & State> Optional<State> resolveDefault(Class<T> enumClass) {
try {
for (T constant : enumClass.getEnumConstants()) {
if (enumClass.getField(constant.name()).getAnnotation(Default.class) != null) {
return Optional.of(constant);
}
}
} catch (NoSuchFieldException e) {
return resolveObfuscatedDefault(enumClass);
}
return Optional.empty();
}

/**
* Resolves the default state from an obfuscated enum. Returns the first enum
* constant it finds that has the
* {@link Default} annotation on it.
*
* @param <T> the type of Enum that is a State
* @param enumClass the class of the enum
* @return an optional representing the color
*/
protected final <T extends Enum<T> & State> Optional<State> resolveObfuscatedDefault(Class<T> enumClass) {
List<T> constants = Arrays.asList(enumClass.getEnumConstants());
for (Field field : enumClass.getDeclaredFields()) {
if (field.isEnumConstant()) {
try {
Object resolved = field.get(null);
if (enumClass.isInstance(resolved)
&& constants.contains(resolved)
&& field.getAnnotation(Default.class) != null) {
return Optional.of(enumClass.cast(resolved));
}
} catch (IllegalAccessException e) {
DriverStation.reportWarning("IllegalAccessException in Lightshow: " + e.getMessage(), false);
}
}
}
return Optional.empty();
}

/**
* Updates the current States.
* Makes the current {@link #states} updated,
Expand Down Expand Up @@ -146,16 +94,11 @@ public void periodic() {
}
}

public void setDefaultState(State defaultState) {
this.defaultState = Optional.of(defaultState);
}

/**
* Marks an enum constant as being the default state.
* Sets the {@link State} to be used if none is given.
* @param defaultState the {@link State} to be used as a default.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public static @interface Default {

public void setDefaultState(State defaultState) {
this.defaultState = Optional.of(defaultState);
}
}

0 comments on commit e347000

Please sign in to comment.