001    /* 
002        Licensed to the Apache Software Foundation (ASF) under one
003        or more contributor license agreements.  See the NOTICE file
004        distributed with this work for additional information
005        regarding copyright ownership.  The ASF licenses this file
006        to you under the Apache License, Version 2.0 (the
007        "License"); you may not use this file except in compliance
008        with the License.  You may obtain a copy of the License at
009    
010           http://www.apache.org/licenses/LICENSE-2.0
011    
012        Unless required by applicable law or agreed to in writing,
013        software distributed under the License is distributed on an
014        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015        KIND, either express or implied.  See the License for the
016        specific language governing permissions and limitations
017        under the License.  
018     */
019    package org.apache.wiki.workflow;
020    
021    import java.io.Serializable;
022    
023    /**
024     * Represents a contextual artifact, which can be any serializable object, useful for making
025     * a Decision. Facts are key-value pairs, where the key is a String (message
026     * key) and the value is an arbitrary object. Generally, the supplied object's
027     * {@link #toString()} method should return a human-readable String. Facts are
028     * immutable objects.
029     * 
030     * @since 2.5
031     */
032    public final class Fact implements Serializable
033    {
034        private static final long serialVersionUID = -7459432935402796978L;
035    
036        private final String m_key;
037    
038        private final Serializable m_obj;
039    
040        /**
041         * Constructs a new Fact with a supplied message key and value.
042         * 
043         * @param messageKey
044         *            the "name" of this fact, which should be an i18n message key
045         * @param value
046         *            the object to associate with the name
047         */
048        public Fact(String messageKey, Serializable value)
049        {
050            if ( messageKey == null || value == null )
051            {
052                throw new IllegalArgumentException( "Fact message key or value parameters must not be null." );
053            }
054            m_key = messageKey;
055            m_obj = value;
056        }
057    
058        /**
059         * Returns this Fact's name, as represented an i18n message key.
060         * @return the message key
061         */
062        public String getMessageKey()
063        {
064            return m_key;
065        }
066    
067        /**
068         * Returns this Fact's value.
069         * @return the value object
070         */
071        public Serializable getValue()
072        {
073            return m_obj;
074        }
075        
076        /**
077         * Two Facts are considered equal if their message keys and value objects are equal.
078         * @param obj the object to test
079         * @return <code>true</code> if logically equal, <code>false</code> if not
080         */
081        public boolean equals( Object obj )
082        {
083            if ( !( obj instanceof Fact ) ) 
084            {
085                return false;
086            }
087            
088            Fact f = (Fact)obj;
089            return m_key.equals( f.m_key) && m_obj.equals( f.m_obj );
090        }
091        
092        /**
093         * {@inheritDoc}
094         */
095        public int hashCode()
096        {
097            return m_key.hashCode() + 41 * m_obj.hashCode();
098        }
099    
100        /**
101         * Returns a String representation of this Fact.
102         * @return the representation
103         */
104        public String toString()
105        {
106            return "[Fact:" + m_obj.toString() + "]";
107        }
108    }