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 java.io.Serializable;
022    
023    /**
024     * Default implementation of an operation.
025     *
026     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
027     * @version 1.0.4
028     */
029    public class DefaultOperation implements Operation, Serializable
030    {
031        private final String m_name;
032        private final String m_method;
033        
034       /**
035        * Creation of a new operation.
036        * @param name the operation name
037        * @exception NullPointerException if the operation name is null
038        */
039        public DefaultOperation( final String name ) throws NullPointerException
040        {
041            this( name, null );
042        }
043        
044       /**
045        * Creation of a new operation.
046        * @param name the operation name
047        * @param method the overriding method name
048        * @exception NullPointerException if the operation name is null
049        */
050        public DefaultOperation( final String name, String method ) throws NullPointerException
051        {
052            if( null == name )
053            {
054                throw new NullPointerException( "name" );
055            }
056            m_name = name;
057            m_method = method;
058        }
059        
060       /**
061        * Return the action name.
062        * @return the name
063        */
064        public String getName()
065        {
066            return m_name;
067        }
068        
069       /**
070        * Return the optional overriding method name.  If the 
071        * value returned is null the method shall be assumed to be the 
072        * equivalent of "get[Name]().
073        * @return the operation method name
074        */
075        public String getMethodName()
076        {
077            return m_method;
078        }
079        
080       /**
081        * Return a string representation of the instance.
082        * @return the string value
083        */
084        public String toString()
085        {
086            return "operation:" + m_name;
087        }
088        
089       /**
090        * Compare this object to another for equality.
091        * @param other the other object
092        * @return true if the object is equal to this object
093        */
094        public boolean equals( Object other )
095        {
096            if( null == other )
097            {
098                return false;
099            }
100            else if( other instanceof DefaultOperation )
101            {
102                DefaultOperation operation = (DefaultOperation) other;
103                
104                if( !m_name.equals( operation.getName() ) )
105                {
106                    return false;
107                }
108                else if( null == m_method )
109                {
110                    return null == operation.m_method;
111                }
112                else
113                {
114                    return m_method.equals( operation.m_method );
115                }
116            }
117            else
118            {
119                return false;
120            }
121        }
122        
123       /**
124        * Compute the hashcode for this instance.
125        * @return the hashcode value
126        */
127        public int hashCode()
128        {
129            int hash = getClass().hashCode();
130            hash ^= m_name.hashCode();
131            if( null != m_method )
132            {
133                hash ^= m_method.hashCode();
134            }
135            return hash;
136        }
137    }