Added AppStateManager.getState(class, failOnMiss)

that can optionally throw an exception if the state does not exist.
This commit is contained in:
Ali-RS 2018-12-03 12:47:01 +03:30 committed by GitHub
parent c0823bb903
commit a61813ebd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -210,6 +210,18 @@ public class AppStateManager {
* @return First attached state that is an instance of stateClass
*/
public <T extends AppState> T getState(Class<T> stateClass){
return getState(stateClass, false);
}
/**
* Returns the first state that is an instance of subclass of the specified class.
* @param <T>
* @param stateClass
* @param failOnMiss
* @return First attached state that is an instance of stateClass. If failOnMiss is true
* then an IllegalArgumentException is thrown if the state is not attached.
*/
public <T extends AppState> T getState(Class<T> stateClass, boolean failOnMiss){
synchronized (states){
AppState[] array = getStates();
for (AppState state : array) {
@ -229,6 +241,10 @@ public class AppStateManager {
}
}
}
if(failOnMiss) {
throw new IllegalArgumentException("State not found for:" + stateClass);
}
return null;
}