001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.jingle;
018
019import org.jivesoftware.smack.SmackException;
020import org.jivesoftware.smack.packet.IQ;
021import org.jivesoftware.smackx.jingle.packet.Jingle;
022
023/**
024 *  Implement the Jingle Session state using the State Behavioral pattern.
025 *  (From the book Design Patterns, AKA GoF.)
026 *  These classes also employ the Flyweight and Singleton patterns as recommended for the State pattern by GoF.
027 *  
028 *  There seems to be three ways to go with the State pattern in Java: interface, abstract class and enums.
029 *  Most of the accepted models use abstract classes.  It wasn't clear to me that any of the three models was
030 *  superior, so I went with the most common example.
031 *  
032 *  @author Jeff Williams
033 */
034public abstract class JingleSessionState {
035
036    /**
037     * Called when entering the state.
038     */
039    public static JingleSessionState getInstance() {
040        // Since we can never instantiate this class there is nothing to return (ever).
041        return null;
042    }
043
044    /**
045     * Called when entering the state.
046     */
047    public abstract void enter();
048
049    /**
050     * Called when exiting the state.
051     */
052    public abstract void exit();
053
054    /**
055     * Process an incoming Jingle Packet.
056     * When you look at the GoF State pattern this method roughly corresponds to example on p310: ProcessOctect().
057     */
058    public abstract IQ processJingle(JingleSession session, Jingle jingle, JingleActionEnum action) throws SmackException;
059
060    /**
061     * For debugging just emit the short name of the class.
062     */
063    public String toString() {
064        return this.getClass().getSimpleName();
065    }
066}