All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jppf.nio.NioServerFactory Maven / Gradle / Ivy

There is a newer version: 6.3-alpha
Show newest version
/*
 * JPPF.
 * Copyright (C) 2005-2015 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed 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.jppf.nio;

import java.nio.channels.SelectionKey;
import java.util.Map;

import org.jppf.utils.collections.CollectionMap;

/**
 * Instances of this class provide a mapping of enumerated values for states and
 * transitions to the actual corresponding objects.
 * @param  the type safe enumeration of the states.
 * @param  the type safe enumeration of the state transitions.
 * @author Laurent Cohen
 */
public abstract class NioServerFactory, T extends Enum>
{
  /**
   * A short name for read and write channel operations.
   */
  public static final int RW = SelectionKey.OP_READ|SelectionKey.OP_WRITE;
  /**
   * A short name for read channel operations.
   */
  public static final int R = SelectionKey.OP_READ;
  /**
   * A short name for write channel operations.
   */
  public static final int W = SelectionKey.OP_WRITE;
  /**
   * Map of all states for a class server.
   */
  protected final Map> stateMap;
  /**
   * Map of all states for a class server.
   */
  protected final Map> transitionMap;
  /**
   * The server for which this factory is intended.
   */
  protected final NioServer server;
  /**
   * A map of the allowed states to which each state can transition.
   */
  protected final CollectionMap allowedTransitions;

  /**
   * Initialize this factory with the specified server.
   * @param server the server for which to initialize.
   */
  protected NioServerFactory(final NioServer server)
  {
    this.server = server;
    stateMap = createStateMap();
    transitionMap = createTransitionMap();
    allowedTransitions = createAllowedTransitionsMap();
  }

  /**
   * Create the map of all possible states.
   * @return a mapping of the states enumeration to the corresponding NioStateInstances.
   */
  protected abstract Map> createStateMap();

  /**
   * Create the map of all possible states.
   * @return a mapping of the states enumeration to the corresponding NioStateInstances.
   */
  protected abstract Map> createTransitionMap();

  /**
   * 
   * @return a CollectionMap for the possible states.
   */
  protected CollectionMap createAllowedTransitionsMap()
  {
    return null;
  }

  /**
   * Get a state given its name.
   * @param name the name of the state to lookup.
   * @return an NioState instance.
   */
  public NioState getState(final S name)
  {
    return stateMap.get(name);
  }

  /**
   * Get a transition given its name.
   * @param name the name of the transition to lookup.
   * @return an NioTransition instance.
   */
  public NioTransition getTransition(final T name)
  {
    return transitionMap.get(name);
  }

  /**
   * Get the server for which this factory is intended.
   * @return an NioServer instance.
   */
  public NioServer getServer()
  {
    return server;
  }

  /**
   * Create a transition to the specified state for the specified IO operations.
   * @param state resulting state of the transition.
   * @param ops the operations allowed.
   * @return an NioTransition<ClassState> instance.
   */
  protected NioTransition transition(final S state, final int ops)
  {
    return new NioTransition<>(state, ops);
  }

  /**
   * Determine whether the transion from the current state to a new state is allowed.
   * @param currentState the current state.
   * @param newState the new state.
   * @return true if the transition is allowed, false otherwise.
   */
  public boolean isTransitionAllowed(final S currentState, final S newState)
  {
    return (allowedTransitions == null) || allowedTransitions.containsValue(currentState, newState);
  }
}