001    /*
002     * Copyright 2005 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.state;
020    
021    import net.dpml.lang.Enum;
022    
023    /**
024     * Interface describing a condition within which an action may be invoked.
025     *
026     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
027     * @version 1.2.0
028     */
029    public interface Trigger
030    {
031       /**
032        * Initialization event emumeration.
033        */
034        public static final TriggerEvent INITIALIZATION = new TriggerEvent( "initialization" );
035    
036       /**
037        * Termination event emumeration.
038        */
039        public static final TriggerEvent TERMINATION = new TriggerEvent( "termination" );
040    
041       /**
042        * Return the event enumneration that this trigger is associated with.
043        * @return the triggering event class
044        */
045        TriggerEvent getEvent();
046        
047       /**
048        * Return the actions  that this trigger initiates.
049        * @return the triggered action
050        */
051        Action getAction();
052        
053       /**
054        * Trigger policy enumeration.
055        */
056        public static final class TriggerEvent extends Enum
057        {
058            static final long serialVersionUID = 1L;
059    
060           /**
061            * Array of static trigger event enumeration values.
062            */
063            private static final TriggerEvent[] ENUM_VALUES = new TriggerEvent[]{INITIALIZATION, TERMINATION};
064    
065           /**
066            * Returns an array of trigger event enum values.
067            * @return the trigger event array
068            */
069            public static TriggerEvent[] values()
070            {
071                return ENUM_VALUES;
072            }
073            
074           /**
075            * Parse the supplied value and return the corresponding 
076            * trigger event class.
077            * @param value the event name
078            * @return the trgger constant
079            */ 
080            public static TriggerEvent parse( String value )
081            {
082                if( value.equalsIgnoreCase( "initialization" ) )
083                {
084                    return INITIALIZATION;
085                }
086                else if( value.equalsIgnoreCase( "termination" ) )
087                {
088                    return TERMINATION;
089                }
090                else
091                {
092                    final String error =
093                      "Unrecognized trigger event argument [" + value + "]";
094                    throw new IllegalArgumentException( error );
095                }
096            }
097            
098           /**
099            * Internal constructor.
100            * @param label the enumeration label.
101            */
102            private TriggerEvent( String label )
103            {
104                super( label );
105            }
106        }
107    }