01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *
19: */
20: package org.apache.mina.statemachine.context;
21:
22: /**
23: * Abstract {@link StateContextLookup} implementation. The {@link #lookup(Object[])}
24: * method will loop through the event arguments and call the {@link #supports(Class)}
25: * method for each of them. The first argument that this method returns
26: * <code>true</code> for will be passed to the abstract {@link #lookup(Object)}
27: * method which should try to extract a {@link StateContext} from the argument.
28: * If none is found a new {@link StateContext} will be created and stored in the
29: * event argument using the {@link #store(Object, StateContext)} method.
30: *
31: * @author The Apache MINA Project (dev@mina.apache.org)
32: * @version $Rev: 589475 $, $Date: 2007-10-28 21:12:59 -0600 (Sun, 28 Oct 2007) $
33: */
34: public abstract class AbstractStateContextLookup implements
35: StateContextLookup {
36: private final StateContextFactory contextFactory;
37:
38: /**
39: * Creates a new instance which uses the specified {@link StateContextFactory}
40: * to create {@link StateContext} objects.
41: *
42: * @param contextFactory the factory.
43: */
44: public AbstractStateContextLookup(StateContextFactory contextFactory) {
45: if (contextFactory == null) {
46: throw new NullPointerException("contextFactory");
47: }
48: this .contextFactory = contextFactory;
49: }
50:
51: public StateContext lookup(Object[] eventArgs) {
52: for (int i = 0; i < eventArgs.length; i++) {
53: if (supports(eventArgs[i].getClass())) {
54: StateContext sc = lookup(eventArgs[i]);
55: if (sc == null) {
56: sc = contextFactory.create();
57: store(eventArgs[i], sc);
58: }
59: return sc;
60: }
61: }
62: return null;
63: }
64:
65: /**
66: * Extracts a {@link StateContext} from the specified event argument which
67: * is an instance of a class {@link #supports(Class)} returns
68: * <code>true</code> for.
69: *
70: * @param eventArg the event argument.
71: * @return the {@link StateContext}.
72: */
73: protected abstract StateContext lookup(Object eventArg);
74:
75: /**
76: * Stores a new {@link StateContext} in the specified event argument which
77: * is an instance of a class {@link #supports(Class)} returns
78: * <code>true</code> for.
79: *
80: * @param eventArg the event argument.
81: * @param context the {@link StateContext} to be stored.
82: */
83: protected abstract void store(Object eventArg, StateContext context);
84:
85: /**
86: * Must return <code>true</code> for any {@link Class} that this
87: * {@link StateContextLookup} can use to store and lookup
88: * {@link StateContext} objects.
89: *
90: * @param c the class.
91: * @return <code>true</code> or <code>false</code>.
92: */
93: protected abstract boolean supports(Class<?> c);
94: }
|