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.jingleold;
018
019import org.jivesoftware.smack.SmackException;
020import org.jivesoftware.smack.packet.IQ;
021
022import org.jivesoftware.smackx.jingleold.packet.Jingle;
023
024/**
025 *  Implement the Jingle Session state using the State Behavioral pattern.
026 *  (From the book Design Patterns, AKA GoF.)
027 *  These classes also employ the Flyweight and Singleton patterns as recommended for the State pattern by GoF.
028 *
029 *  There seems to be three ways to go with the State pattern in Java: interface, abstract class and enums.
030 *  Most of the accepted models use abstract classes.  It wasn't clear to me that any of the three models was
031 *  superior, so I went with the most common example.
032 *
033 *  @author Jeff Williams
034 */
035public abstract class JingleSessionState {
036
037    /**
038     * Called when entering the state.
039     */
040    public static JingleSessionState getInstance() {
041        // Since we can never instantiate this class there is nothing to return (ever).
042        return null;
043    }
044
045    /**
046     * Called when entering the state.
047     */
048    public abstract void enter();
049
050    /**
051     * Called when exiting the state.
052     */
053    public abstract void exit();
054
055    /**
056     * Process an incoming Jingle Packet.
057     * When you look at the GoF State pattern this method roughly corresponds to example on p310: ProcessOctect().
058     * @throws InterruptedException
059     */
060    public abstract IQ processJingle(JingleSession session, Jingle jingle, JingleActionEnum action) throws SmackException, InterruptedException;
061
062    /**
063     * For debugging just emit the short name of the class.
064     */
065    @Override
066    public String toString() {
067        return this.getClass().getSimpleName();
068    }
069}