coherence/util/filter/MapEventFilter.hpp

00001 /*
00002 * MapEventFilter.hpp
00003 *
00004 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_MAP_EVENT_FILTER_HPP
00017 #define COH_MAP_EVENT_FILTER_HPP
00018 
00019 #include "coherence/lang.ns"
00020 
00021 #include "coherence/io/pof/PofReader.hpp"
00022 #include "coherence/io/pof/PofWriter.hpp"
00023 #include "coherence/io/pof/PortableObject.hpp"
00024 #include "coherence/util/Filter.hpp"
00025 
00026 COH_OPEN_NAMESPACE3(coherence,util,filter)
00027 
00028 using coherence::io::pof::PofReader;
00029 using coherence::io::pof::PofWriter;
00030 using coherence::io::pof::PortableObject;
00031 
00032 
00033 /**
00034 * Filter which evaluates the content of a MapEvent object according to the
00035 * specified criteria.  This filter is intended to be used by various
00036 * ObservableMap listeners that are interested in particular subsets
00037 * of MapEvent notifications emitted by the map.
00038 *
00039 * Usage examples:
00040 * <ul>
00041 * <li>a filter that evaluates to true if an Employee object is inserted into
00042 *     a cache with a value of Married property set to true.
00043 * <pre>
00044 *   MapEventFilter::create(MapEventFilter.E_INSERT,
00045 *       EqualsFilter::create(ReflectionExtractor::create("isMarried"),
00046 *               Boolean::create(true));
00047 * </pre>
00048 * <li>a filter that evaluates to true if any object is removed from a cache.
00049 * <pre>
00050 *   MapEventFilter::create(MapEventFilter::E_DELETE);
00051 * </pre>
00052 * <li>a filter that evaluates to true if there is an update to an Employee
00053 *     object where either an old or new value of LastName property equals to
00054 *     "Smith"
00055 * <pre>
00056 *   MapEventFilter::create(MapEventFilter::E_UPDATE,
00057 *       EqualsFilter::create(ReflectionExtractor::create("LastName"),
00058 *               String::create("Smith")));
00059 * </pre>
00060 * <li>a filter that is used to keep a cached keySet result based on some map
00061 *     filter up-to-date.
00062 * <pre>
00063 *   Set::View    setKeys   = HashSet::create();
00064 *   Filter::View filterEvt = MapEventFilter::create(filterMap);
00065 *   MapListener::Handle listener = MapListener::create();
00066 *   map.addMapListener(listener, filterEvt, true);
00067 *   setKeys.addAll(map.keySet(filterMap));
00068 * </pre>
00069 * </ul>
00070 *
00071 * see ValueChangeEventFilter
00072 *
00073 * @author djl  2008.05.19
00074 */
00075 class COH_EXPORT MapEventFilter
00076     : public class_spec<MapEventFilter,
00077         extends<Object>,
00078         implements<PortableObject, Filter> >
00079     {
00080     friend class factory<MapEventFilter>;
00081 
00082     // ----- constructors ---------------------------------------------------
00083 
00084     protected:
00085         /**
00086         * Default constructor (necessary for the PortableObject interface).
00087         */
00088         MapEventFilter();
00089 
00090         /**
00091         * Construct a MapEventFilter that evaluates MapEvent objects that
00092         * would affect the results of a keySet filter issued by a previous
00093         * call to
00094         * coherence::util::QueryMap::keySet(coherence::util::Filter::View).
00095         *  It is possible to easily implement <i>continuous query</i>
00096         * functionality.
00097         *
00098         * Using this constructor is equivalent to:
00099         * <tt>
00100         * MapEventFilter::create(E_KEYSET, filter);
00101         * </tt>
00102         *
00103         * @param vFilter  the filter passed previously to a keySet() query
00104         * method
00105         */
00106         MapEventFilter(Filter::View vFilter);
00107 
00108         /**
00109         * Construct a MapEventFilter that evaluates MapEvent objects
00110         * based on the specified combination of event types.
00111         *
00112         * @param nMask    combination of any of the E_* values
00113         * @param vFilter  (optional) the filter used for evaluating event
00114         *                 values
00115         */
00116         MapEventFilter(int32_t nMask, Filter::View vFilter = NULL);
00117 
00118 
00119     // ----- Filter interface -----------------------------------------------
00120 
00121     public:
00122         /**
00123         * {@inheritDoc}
00124         */
00125         virtual bool evaluate(Object::View v) const;
00126 
00127 
00128    // ----- PortableObject interface ---------------------------------------
00129 
00130      public:
00131         /**
00132         * {@inheritDoc}
00133         */
00134         virtual void readExternal(PofReader::Handle hIn);
00135 
00136         /**
00137         * {@inheritDoc}
00138         */
00139         virtual void writeExternal(PofWriter::Handle hOut) const;
00140 
00141 
00142     // ----- Object interface -----------------------------------------------
00143 
00144     public:
00145         /**
00146         * {@inheritDoc}
00147         */
00148         virtual bool equals(Object::View v) const;
00149 
00150         /**
00151         * {@inheritDoc}
00152         */
00153         virtual size32_t hashCode() const;
00154 
00155         /**
00156         * {@inheritDoc}
00157         */
00158         virtual void toStream(std::ostream& out) const;
00159 
00160 
00161     // ----- data member accessors ------------------------------------------
00162 
00163     public:
00164         /**
00165         * Obtain the event mask. The mask value is concatenation of any of
00166         * the E_* values.
00167         *
00168         * @return the event mask
00169         */
00170         virtual int32_t getEventMask() const;
00171 
00172 
00173         /**
00174         * Obtain the Filter object used to evaluate the event value(s).
00175         *
00176         * @return the filter used to evaluate the event value(s)
00177         */
00178         virtual Filter::View getFilter() const;
00179 
00180     // ----- constants ------------------------------------------------------
00181 
00182     public:
00183         /**
00184         * This value indicates that MapEvent#ENTRY_INSERTED
00185         * ENTRY_INSERTED} events should be evaluated. The event will be fired
00186         *  if there is no filter specified or the filter evaluates to true
00187         * for a new value.
00188         */
00189         static const int32_t E_INSERTED       = 0x0001;
00190 
00191         /**
00192         * This value indicates that MapEvent#ENTRY_UPDATED ENTRY_UPDATED
00193         * events should be evaluated. The event will be fired if there is no
00194         * filter specified or the filter evaluates to true when applied to
00195         * either old or new value.
00196         */
00197         static const int32_t E_UPDATED        = 0x0002;
00198 
00199         /**
00200         * This value indicates that MapEvent#ENTRY_DELETED ENTRY_DELETED
00201         * events should be evaluated. The event will be fired if there is no
00202         * filter specified or the filter evaluates to true for an old value.
00203         */
00204         static const int32_t E_DELETED        = 0x0004;
00205 
00206         /**
00207         * This value indicates that MapEvent#ENTRY_UPDATED ENTRY_UPDATED
00208         * events should be evaluated, but only if filter evaluation is false
00209         * for the old value and true for the new value. This corresponds to
00210         * an item that was not in a keySet filter result changing such that
00211         * it would now be in that keySet filter result.
00212         */
00213         static const int32_t E_UPDATED_ENTERED = 0x0008;
00214 
00215         /**
00216         * This value indicates that MapEvent#ENTRY_UPDATED ENTRY_UPDATED
00217         * events should be evaluated, but only if filter evaluation is true
00218         * for the old value and false for the new value. This corresponds to
00219         * an item that was in a keySet filter result changing such that it
00220         * would no longer be in that keySet filter result.
00221         */
00222         static const int32_t E_UPDATED_LEFT   = 0x0010;
00223 
00224         /**
00225         * This value indicates that MapEvent#ENTRY_UPDATED ENTRY_UPDATED
00226         * events should be evaluated, but only if filter evaluation is true
00227         * for both the old and the new value. This corresponds to an item
00228         * that was in a keySet filter result changing but not leaving the
00229         * keySet filter result.
00230         */
00231         static const int32_t E_UPDATED_WITHIN = 0x0020;
00232 
00233         /**
00234         * This value indicates that all events should be evaluated.
00235         */
00236         static const int32_t E_ALL            = E_INSERTED | E_UPDATED |
00237                                                 E_DELETED;
00238 
00239         /**
00240         * This value indicates that all events that would affect the result
00241         * of a coherence::util::QueryMap::keySet(coherence::util::Filter)
00242         * query should be evaluated.
00243         */
00244         static const int32_t E_KEYSET         = E_INSERTED | E_DELETED |
00245                                                 E_UPDATED_ENTERED  |
00246                                                 E_UPDATED_LEFT;
00247         /**
00248         * Event id to event mask translation.
00249         *
00250         * @param iEvent  the event index
00251         *
00252         * @return the translated event mask
00253         */
00254         static int32_t getMask(size32_t iEvent);
00255 
00256 
00257     // ----- data members ---------------------------------------------------
00258 
00259     protected:
00260         /**
00261         * The event mask.
00262         */
00263         int32_t m_nMask;
00264 
00265         /**
00266         * The event value(s) filter.
00267         */
00268         MemberView<Filter> m_vFilter;
00269     };
00270 
00271 COH_CLOSE_NAMESPACE3
00272 
00273 #endif // COH_MAP_EVENT_FILTER_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.