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 */
33 public abstract class AbstractStateContextLookup implements StateContextLookup {
34 private final StateContextFactory contextFactory;
35
36 /**
37 * Creates a new instance which uses the specified {@link StateContextFactory}
38 * to create {@link StateContext} objects.
39 *
40 * @param contextFactory the factory.
41 */
42 public AbstractStateContextLookup(StateContextFactory contextFactory) {
43 if (contextFactory == null) {
44 throw new NullPointerException("contextFactory");
45 }
46 this.contextFactory = contextFactory;
47 }
48
49 public StateContext lookup(Object[] eventArgs) {
50 for (int i = 0; i < eventArgs.length; i++) {
51 if (supports(eventArgs[i].getClass())) {
52 StateContext sc = lookup(eventArgs[i]);
53 if (sc == null) {
54 sc = contextFactory.create();
55 store(eventArgs[i], sc);
56 }
57 return sc;
58 }
59 }
60 return null;
61 }
62
63 /**
64 * Extracts a {@link StateContext} from the specified event argument which
65 * is an instance of a class {@link #supports(Class)} returns
66 * <code>true</code> for.
67 *
68 * @param eventArg the event argument.
69 * @return the {@link StateContext}.
70 */
71 protected abstract StateContext lookup(Object eventArg);
72
73 /**
74 * Stores a new {@link StateContext} in the specified event argument which
75 * is an instance of a class {@link #supports(Class)} returns
76 * <code>true</code> for.
77 *
78 * @param eventArg the event argument.
79 * @param context the {@link StateContext} to be stored.
80 */
81 protected abstract void store(Object eventArg, StateContext context);
82
83 /**
84 * Must return <code>true</code> for any {@link Class} that this
85 * {@link StateContextLookup} can use to store and lookup
86 * {@link StateContext} objects.
87 *
88 * @param c the class.
89 * @return <code>true</code> or <code>false</code>.
90 */
91 protected abstract boolean supports(Class<?> c);
92 }