001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.statemachine.context; 021 022/** 023 * Abstract {@link StateContextLookup} implementation. The {@link #lookup(Object[])} 024 * method will loop through the event arguments and call the {@link #supports(Class)} 025 * method for each of them. The first argument that this method returns 026 * <tt>true</tt> for will be passed to the abstract {@link #lookup(Object)} 027 * method which should try to extract a {@link StateContext} from the argument. 028 * If none is found a new {@link StateContext} will be created and stored in the 029 * event argument using the {@link #store(Object, StateContext)} method. 030 * 031 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 032 */ 033public abstract class AbstractStateContextLookup implements StateContextLookup { 034 private final StateContextFactory contextFactory; 035 036 /** 037 * Creates a new instance which uses the specified {@link StateContextFactory} 038 * to create {@link StateContext} objects. 039 * 040 * @param contextFactory the factory. 041 */ 042 public AbstractStateContextLookup(StateContextFactory contextFactory) { 043 if (contextFactory == null) { 044 throw new IllegalArgumentException("contextFactory"); 045 } 046 this.contextFactory = contextFactory; 047 } 048 049 public StateContext lookup(Object[] eventArgs) { 050 for (int i = 0; i < eventArgs.length; i++) { 051 if (supports(eventArgs[i].getClass())) { 052 StateContext sc = lookup(eventArgs[i]); 053 if (sc == null) { 054 sc = contextFactory.create(); 055 store(eventArgs[i], sc); 056 } 057 return sc; 058 } 059 } 060 return null; 061 } 062 063 /** 064 * Extracts a {@link StateContext} from the specified event argument which 065 * is an instance of a class {@link #supports(Class)} returns 066 * <tt>true</tt> for. 067 * 068 * @param eventArg the event argument. 069 * @return the {@link StateContext}. 070 */ 071 protected abstract StateContext lookup(Object eventArg); 072 073 /** 074 * Stores a new {@link StateContext} in the specified event argument which 075 * is an instance of a class {@link #supports(Class)} returns 076 * <tt>true</tt> for. 077 * 078 * @param eventArg the event argument. 079 * @param context the {@link StateContext} to be stored. 080 */ 081 protected abstract void store(Object eventArg, StateContext context); 082 083 /** 084 * Must return <tt>true</tt> for any {@link Class} that this 085 * {@link StateContextLookup} can use to store and lookup 086 * {@link StateContext} objects. 087 * 088 * @param c the class. 089 * @return <tt>true</tt> or <tt>false</tt>. 090 */ 091 protected abstract boolean supports(Class<?> c); 092}