Created
August 19, 2020 02:17
-
-
Save IanSSenne/248373ad5e78fdafd6fe9aaf9c10bb01 to your computer and use it in GitHub Desktop.
this was not very much fun to write :(
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
package com.iansenne.fw; | |
import java.util.ArrayList; | |
import java.util. | |
function.Consumer; | |
class FWStateHandler<T>{ | |
FWState<T>handle(String name, T value) { | |
boolean exists = FWStateManager.current().has(name); | |
if (exists) { | |
return (FWState<T>) FWStateManager.current().get(name); | |
} else { | |
FWStateManager.current().set(name, new FWState(name, value)); | |
return (FWState<T>) FWStateManager.current().get(name); | |
} | |
} | |
} | |
public class FWState<T>{ | |
private ArrayList<Consumer<Boolean>>handlers = new ArrayList<>(); | |
private static FWStateHandler<Byte>byte_handler = new FWStateHandler<Byte>(); | |
private static FWStateHandler<Short>short_handler = new FWStateHandler<Short>(); | |
private static FWStateHandler<Integer>int_handler = new FWStateHandler<Integer>(); | |
private static FWStateHandler<Long>long_handler = new FWStateHandler<Long>(); | |
private static FWStateHandler<Float>float_handler = new FWStateHandler<Float>(); | |
private static FWStateHandler<Double>double_handler = new FWStateHandler<Double>(); | |
private static FWStateHandler<Character>char_handler = new FWStateHandler<Character>(); | |
private static FWStateHandler<String>string_handler = new FWStateHandler<String>(); | |
private static FWStateHandler<Boolean>bool_handler = new FWStateHandler<Boolean>(); | |
public static FWState<Byte>Byte(Byte value) { | |
return FWState.byte_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<Short>Short(Short value) { | |
return FWState.short_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<Integer>Integer(Integer value) { | |
return FWState.int_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<Long>Long(Long value) { | |
return FWState.long_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<Float>Float(Float value) { | |
return FWState.float_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<Double>Double(Double value) { | |
return FWState.double_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<Character>Character(Character value) { | |
return FWState.char_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<String>String(String value) { | |
return FWState.string_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static FWState<Boolean>Boolean(Boolean value) { | |
return FWState.bool_handler.handle(FWStateManager.getNextName(), value); | |
} | |
public static class named { | |
public static FWState<Byte>Byte(String name, Byte value) { | |
return FWState.byte_handler.handle(name, value); | |
} | |
public static FWState<Short>Short(String name, Short value) { | |
return FWState.short_handler.handle(name, value); | |
} | |
public static FWState<Integer>Integer(String name, Integer value) { | |
return FWState.int_handler.handle(name, value); | |
} | |
public static FWState<Long>Long(String name, Long value) { | |
return FWState.long_handler.handle(name, value); | |
} | |
public static FWState<Float>Float(String name, Float value) { | |
return FWState.float_handler.handle(name, value); | |
} | |
public static FWState<Double>Double(String name, Double value) { | |
return FWState.double_handler.handle(name, value); | |
} | |
public static FWState<Character>Character(String name, Character value) { | |
return FWState.char_handler.handle(name, value); | |
} | |
public static FWState<String>String(String name, String value) { | |
return FWState.string_handler.handle(name, value); | |
} | |
public static FWState<Boolean>Boolean(String name, Boolean value) { | |
return FWState.bool_handler.handle(name, value); | |
} | |
public static FWState<Byte>Byte(String name) { | |
return FWState.byte_handler.handle(name, null); | |
} | |
public static FWState<Short>Short(String name) { | |
return FWState.short_handler.handle(name, null); | |
} | |
public static FWState<Integer>Integer(String name) { | |
return FWState.int_handler.handle(name, null); | |
} | |
public static FWState<Long>Long(String name) { | |
return FWState.long_handler.handle(name, null); | |
} | |
public static FWState<Float>Float(String name) { | |
return FWState.float_handler.handle(name, null); | |
} | |
public static FWState<Double>Double(String name) { | |
return FWState.double_handler.handle(name, null); | |
} | |
public static FWState<Character>Character(String name) { | |
return FWState.char_handler.handle(name, null); | |
} | |
public static FWState<String>String(String name) { | |
return FWState.string_handler.handle(name, null); | |
} | |
public static FWState<Boolean>Boolean(String name) { | |
return FWState.bool_handler.handle(name, null); | |
} | |
} | |
String name; | |
T value; | |
public FWState(String name, T value) { | |
this.name = name; | |
this.value = value; | |
} | |
public T get() { | |
return this.value; | |
} | |
public void set(T value) throws Exception { | |
if (!this.value.equals(value)) { | |
this.value = value; | |
ArrayList<Consumer<Boolean>>handlers = this.handlers; | |
this.handlers = new ArrayList<Consumer<Boolean>>(); | |
for (Consumer<Boolean>handler: handlers) { | |
handler.accept(true); | |
} | |
} | |
} | |
public void watch(Consumer<Boolean>handler) { | |
this.handlers.add(handler); | |
} | |
public void clearListeners() { | |
this.handlers = new ArrayList<Consumer<Boolean>>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment