UnknownTest.java
/***************************************************************************
Copyright 2015 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.util;
public final class UnknownTest {
private static final class Handler implements EventHandler<Simple> {
@Override public Simple nextState(final Object event, final Simple currentState) {
return currentState;
}
}
private static final class Simple implements State<Simple> {
public final EventCalculator<Simple> ec = new EventCalculator<>();
@Override public Simple nextState(final Object event) { return ec.nextState(event, this); }
@Override public <I> I lookupInterface(final Class<I> theInterface) { return null; }
}
private static final class Machine implements StateMachine<Simple> {
private Simple state = new Simple();
@Override public void nextState(final Object event) { this.state = this.state.nextState(event); }
@Override public Simple state() { return this.state; }
}
private interface I1 {}
private interface I2 {}
private interface I3 extends I2 {}
private static class A {}
private static class B extends A {}
private static class C extends A {}
private static class D extends C {}
private static class E extends D implements I1 {}
private static class F extends E implements I2 {}
private static class G extends D implements I3 {}
public static void main(final String[] args) throws Exception {
final Machine m = new Machine();
final UnknownStateMachine<Simple> x = new UnknownStateMachine<>(m);
final Object o = x.lookupInterface(Object.class);
final Unknown u = x.lookupInterface(Unknown.class);
final Object any = x.lookupInterface(Simple.class);
m.state().ec.addHandler(A.class, new Handler());
m.state().ec.addHandler(B.class, new Handler());
m.state().ec.addHandler(D.class, new Handler());
m.state().ec.addHandler(I1.class, new Handler());
m.state().ec.addHandler(I3.class, new Handler());
m.nextState(new A());
m.nextState(new B());
try {
m.nextState(new C());
throw new AssertionError("There is no handler for this state.");
} catch (final RuntimeException ex) {
// Expected.
}
m.nextState(new D());
m.nextState(new E());
try {
m.nextState(new F());
throw new AssertionError("There is no handler for this state.");
} catch (final RuntimeException ex) {
// Expected.
}
m.nextState(new G());
}
}