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     * @return the jingle session state.
041     */
042    public static JingleSessionState getInstance() {
043        // Since we can never instantiate this class there is nothing to return (ever).
044        return null;
045    }
046
047    /**
048     * Called when entering the state.
049     */
050    public abstract void enter();
051
052    /**
053     * Called when exiting the state.
054     */
055    public abstract void exit();
056
057    /**
058     * Process an incoming Jingle Packet.
059     * When you look at the GoF State pattern this method roughly corresponds to example on p310: ProcessOctect()
060     *
061     * @param session the jingle session.
062     * @param jingle the jingle stanza.
063     * @param action the jingle action.
064     * @return the resulting IQ.
065     * @throws SmackException if Smack detected an exceptional situation.
066     * @throws InterruptedException if the calling thread was interrupted.
067     */
068    public abstract IQ processJingle(JingleSession session, Jingle jingle, JingleActionEnum action) throws SmackException, InterruptedException;
069
070    /**
071     * For debugging just emit the short name of the class.
072     */
073    @Override
074    public String toString() {
075        return this.getClass().getSimpleName();
076    }
077}