Skip to content

Instantly share code, notes, and snippets.

@RoboMWM
Last active August 14, 2019 03:38
Show Gist options
  • Save RoboMWM/189fdd97bc66572ff9fea5d1af5fdad2 to your computer and use it in GitHub Desktop.
Save RoboMWM/189fdd97bc66572ff9fea5d1af5fdad2 to your computer and use it in GitHub Desktop.
Playing with generics in java. A simple test of something implemented in https://github.com/AuthMe/ConfigMe. IMO, it's ok, but kinda results in the same effect as defining a method - since you have to specify the node's path as a string anyways. I guess it does save a little in the amount of lines per node. UPDATE: Ok I'm learning generics and I…
public class Property<T>
{
T value;
String key;
Property(String key, T value)
{
this.key = key;
this.value = value;
}
T getValue()
{
return value;
}
}
class ConfigNodesThing
{
public static Property<String> errorMessage = new Property<>("hello", "blah");
public static Property<Integer> errorInt = new Property<>("hello2", 5);
}
class ConfigManagerThing
{
static Map<String, Object> config = new HashMap<>();
static <T> T getValue(Property<T> thing)
{
return (T)config.get(thing.key);
}
}
class WhoCares
{
String whatever()
{
return ConfigManagerThing.getValue(ConfigNodesThing.errorMessage);
}
int nobody()
{
return ConfigManagerThing.getValue(ConfigNodesThing.errorMessage); //error
}
int cares()
{
return ConfigManagerThing.getValue(ConfigNodesThing.errorInt);
}
String ok()
{
return ConfigManagerThing.getValue(ConfigNodesThing.errorInt); //error
}
String boo()
{
return ConfigNodesThing.errorMessage.getValue();
}
String hoo()
{
return ConfigNodesThing.errorInt.getValue(); //error
}
}
@RoboMWM
Copy link
Author

RoboMWM commented Aug 14, 2019

Playing with generics in java. A simple test of something implemented in https://github.com/AuthMe/ConfigMe. IMO, it's ok, but kinda results in the same effect as defining a method - since you have to specify the node's path as a string anyways. I guess it does save a little in the amount of lines per node. UPDATE: Ok I'm learning generics and I have a plan now. Yes I am using this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment