1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
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-29 04:12:59 +0100 (lun, 29 oct 2007) $
33 */
34 public abstract class AbstractStateContextLookup implements StateContextLookup {
35 private final StateContextFactory contextFactory;
36
37 /**
38 * Creates a new instance which uses the specified {@link StateContextFactory}
39 * to create {@link StateContext} objects.
40 *
41 * @param contextFactory the factory.
42 */
43 public AbstractStateContextLookup(StateContextFactory contextFactory) {
44 if (contextFactory == null) {
45 throw new NullPointerException("contextFactory");
46 }
47 this.contextFactory = contextFactory;
48 }
49
50 public StateContext lookup(Object[] eventArgs) {
51 for (int i = 0; i < eventArgs.length; i++) {
52 if (supports(eventArgs[i].getClass())) {
53 StateContext sc = lookup(eventArgs[i]);
54 if (sc == null) {
55 sc = contextFactory.create();
56 store(eventArgs[i], sc);
57 }
58 return sc;
59 }
60 }
61 return null;
62 }
63
64 /**
65 * Extracts a {@link StateContext} from the specified event argument which
66 * is an instance of a class {@link #supports(Class)} returns
67 * <code>true</code> for.
68 *
69 * @param eventArg the event argument.
70 * @return the {@link StateContext}.
71 */
72 protected abstract StateContext lookup(Object eventArg);
73
74 /**
75 * Stores a new {@link StateContext} in the specified event argument which
76 * is an instance of a class {@link #supports(Class)} returns
77 * <code>true</code> for.
78 *
79 * @param eventArg the event argument.
80 * @param context the {@link StateContext} to be stored.
81 */
82 protected abstract void store(Object eventArg, StateContext context);
83
84 /**
85 * Must return <code>true</code> for any {@link Class} that this
86 * {@link StateContextLookup} can use to store and lookup
87 * {@link StateContext} objects.
88 *
89 * @param c the class.
90 * @return <code>true</code> or <code>false</code>.
91 */
92 protected abstract boolean supports(Class<?> c);
93 }