Last active
August 30, 2018 14:11
-
-
Save Rafael09ED/3bfc6ea3068e908035b8fa81752ff635 to your computer and use it in GitHub Desktop.
Method for casting from a generic type to a more specific type and making it easier to handle. Avoid doing this.
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
//Making Interfaces Like this: | |
public interface Data_TypeStringGenericActable<T> { | |
T act(Data_TypeString data); | |
} | |
public interface Data_TypeUriGenericActable<T> { | |
T act(Data_TypeURI data); | |
} | |
//So I can make methods like this: | |
public static <T> T genericSwitch(Data data, | |
Data_TypeStringGenericActable<T> stringActable, | |
Data_TypeUriGenericActable<T> uriActable) { | |
switch (data.getType()) { | |
case STRING: | |
return stringActable.act((Data_TypeString) data); | |
case URI: | |
return uriActable.act((Data_TypeURI) data); | |
default: | |
System.err.println("In DataType.switch, there is at least one missing data case"); | |
return null; | |
} | |
} | |
// So I can write code like this; | |
public static boolean basicSearch(Data data, String searchValue) { | |
//noinspection ConstantConditions | |
return DataType.genericSwitch(data, | |
string -> string.getString().toLowerCase().contains(searchValue.toLowerCase()), | |
uri -> uri.getString().toLowerCase().contains(searchValue.toLowerCase()) | |
); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment