⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statemachinefactory.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one *  or more contributor license agreements.  See the NOTICE file *  distributed with this work for additional information *  regarding copyright ownership.  The ASF licenses this file *  to you 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 org.apache.mina.statemachine;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.ArrayList;import java.util.Arrays;import java.util.Comparator;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import org.apache.mina.statemachine.annotation.Transition;import org.apache.mina.statemachine.annotation.TransitionAnnotation;import org.apache.mina.statemachine.annotation.Transitions;import org.apache.mina.statemachine.event.Event;import org.apache.mina.statemachine.transition.MethodTransition;/** * Creates {@link StateMachine}s by reading {@link org.apache.mina.statemachine.annotation.State}, * {@link Transition} and {@link Transitions} (or equivalent) annotations from one or more arbitrary  * objects. *  * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 592479 $, $Date: 2007-11-06 17:26:11 +0100 (Tue, 06 Nov 2007) $ */public class StateMachineFactory {    private final Class<? extends Annotation> transitionAnnotation;    private final Class<? extends Annotation> transitionsAnnotation;    protected StateMachineFactory(Class<? extends Annotation> transitionAnnotation,                 Class<? extends Annotation> transitionsAnnotation) {        this.transitionAnnotation = transitionAnnotation;        this.transitionsAnnotation = transitionsAnnotation;    }        /**     * Returns a new {@link StateMachineFactory} instance which creates      * {@link StateMachine}s by reading the specified {@link Transition}     * equivalent annotation.     *      * @param transitionAnnotation the {@link Transition} equivalent annotation.     * @return the {@link StateMachineFactory}.     */    public static StateMachineFactory getInstance(Class<? extends Annotation> transitionAnnotation) {        TransitionAnnotation a = transitionAnnotation.getAnnotation(TransitionAnnotation.class);        if (a == null) {            throw new IllegalArgumentException("The annotation class "                     + transitionAnnotation + " has not been annotated with the "                     + TransitionAnnotation.class.getName() + " annotation");        }        return new StateMachineFactory(transitionAnnotation, a.value());    }        /**     * Creates a new {@link StateMachine} from the specified handler object and     * using a start state with id <code>start</code>.     *      * @param handler the object containing the annotations describing the      *        state machine.     * @return the {@link StateMachine} object.     */    public StateMachine create(Object handler) {        return create(handler, new Object[0]);    }    /**     * Creates a new {@link StateMachine} from the specified handler object and     * using the {@link State} with the specified id as start state.     *      * @param start the id of the start {@link State} to use.     * @param handler the object containing the annotations describing the      *        state machine.     * @return the {@link StateMachine} object.     */    public StateMachine create(String start, Object handler) {        return create(start, handler, new Object[0]);    }    /**     * Creates a new {@link StateMachine} from the specified handler objects and     * using a start state with id <code>start</code>.     *      * @param handler the first object containing the annotations describing the      *        state machine.     * @param handlers zero or more additional objects containing the      *        annotations describing the state machine.     * @return the {@link StateMachine} object.     */    public StateMachine create(Object handler, Object... handlers) {        return create("start", handler, handlers);    }        /**     * Creates a new {@link StateMachine} from the specified handler objects and     * using the {@link State} with the specified id as start state.     *      * @param start the id of the start {@link State} to use.     * @param handler the first object containing the annotations describing the      *        state machine.     * @param handlers zero or more additional objects containing the      *        annotations describing the state machine.     * @return the {@link StateMachine} object.     */    public StateMachine create(String start, Object handler, Object... handlers) {                Map<String, State> states = new HashMap<String, State>();        List<Object> handlersList = new ArrayList<Object>(1 + handlers.length);        handlersList.add(handler);        handlersList.addAll(Arrays.asList(handlers));                LinkedList<Field> fields = new LinkedList<Field>();        for (Object h : handlersList) {            fields.addAll(getFields(h instanceof Class ? (Class<?>) h : h.getClass()));        }        for (State state : createStates(fields)) {            states.put(state.getId(), state);        }        if (!states.containsKey(start)) {            throw new StateMachineCreationException("Start state '" + start + "' not found.");        }        setupTransitions(transitionAnnotation, transitionsAnnotation, states, handlersList);        return new StateMachine(states.values(), start);    }    private static void setupTransitions(Class<? extends Annotation> transitionAnnotation,             Class<? extends Annotation> transitionsAnnotation, Map<String, State> states, List<Object> handlers) {        for (Object handler : handlers) {            setupTransitions(transitionAnnotation, transitionsAnnotation, states, handler);        }    }        private static void setupTransitions(Class<? extends Annotation> transitionAnnotation,             Class<? extends Annotation> transitionsAnnotation, Map<String, State> states, Object handler) {                Method[] methods = handler.getClass().getDeclaredMethods();        Arrays.sort(methods, new Comparator<Method>() {            public int compare(Method m1, Method m2) {                return m1.toString().compareTo(m2.toString());            }        });                for (Method m : methods) {            List<TransitionWrapper> transitionAnnotations = new ArrayList<TransitionWrapper>();            if (m.isAnnotationPresent(transitionAnnotation)) {                transitionAnnotations.add(new TransitionWrapper(transitionAnnotation, m.getAnnotation(transitionAnnotation)));            }            if (m.isAnnotationPresent(transitionsAnnotation)) {                transitionAnnotations.addAll(Arrays.asList(new TransitionsWrapper(transitionAnnotation,                         transitionsAnnotation, m.getAnnotation(transitionsAnnotation)).value()));            }                        if (transitionAnnotations.isEmpty()) {                continue;            }                        for (TransitionWrapper annotation : transitionAnnotations) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -