coherence/util/SubSet.hpp

00001 /*
00002 * SubSet.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_SUB_SET_HPP
00017 #define COH_SUB_SET_HPP
00018 
00019 #include "coherence/lang.ns"
00020 
00021 #include "coherence/util/AbstractSet.hpp"
00022 #include "coherence/util/Set.hpp"
00023 
00024 COH_OPEN_NAMESPACE2(coherence,util)
00025 
00026 
00027 /**
00028 * Implements a set which is based on another set, which is assumed to be
00029 * immutable. Unlike DeltaSet, the SubSet is assumed to be a subset of the
00030 * underlying set, and optimizes for both remove and retain operations.
00031 *
00032 * @author tb 2009.02.10
00033 */
00034 class COH_EXPORT SubSet
00035     : public class_spec<SubSet,
00036         extends<AbstractSet> >
00037     {
00038     friend class factory<SubSet>;
00039 
00040     // ----- constructor ----------------------------------------------------
00041 
00042     public:
00043         /**
00044         * @internal
00045         */
00046         SubSet(Set::Holder hSet);
00047 
00048 
00049     // ----- SubSet accessors -----------------------------------------------
00050     public:
00051 
00052         /**
00053         * Determine what items were in the original set.
00054         *
00055         * @return the set used to construct this SubSet
00056         */
00057         virtual Set::View getOriginal() const;
00058 
00059         /**
00060         * Determine if the set has been modified.
00061         *
00062         * @return  true if any items have been removed
00063         */
00064         virtual bool isModified() const;
00065 
00066         /**
00067         * Determine if the SubSet is tracking retained items versus removed
00068         * items.
00069         *
00070         * @return true if the SubSet is tracking just the retained items,
00071         *         false if the SubSet is tracking just the removed items
00072         */
00073         virtual bool isTrackingRetained() const;
00074 
00075         /**
00076         * Determine what items were added to the subset. Do not modify the
00077         * returned set.
00078         *
00079         * @return a set of retained items
00080         */
00081         virtual Set::View getRetained() const;
00082 
00083         /**
00084         * Determine if the SubSet is tracking removed items versus retained
00085         * items.
00086         *
00087         * @return true if the SubSet is tracking just the removed items,
00088         *         false if the SubSet is tracking just the retained items
00089         */
00090         virtual bool isTrackingRemoved() const;
00091 
00092         /**
00093         * Determine what items were removed from the subset.
00094         *
00095         * @return an immutable set of removed items
00096         */
00097         virtual Set::View getRemoved() const;
00098 
00099         /**
00100         * Get a mutable set of items that are retained in the subset.
00101         *
00102         * @return a mutable set of retained items
00103         */
00104         virtual Set::Handle ensureRetained();
00105 
00106         /**
00107         * Get a mutable set of items that are removed in the subset.
00108         *
00109         * @return a mutable set of removed items
00110         */
00111         virtual Set::Handle ensureRemoved();
00112 
00113         /**
00114         * Apply the changes to the underlying set ("commit").
00115         */
00116         virtual void resolve();
00117 
00118         /**
00119         * Discard the changes to the set ("rollback").
00120         */
00121         virtual void reset();
00122 
00123 
00124     // ----- Collection interface -------------------------------------------
00125 
00126     public:
00127         /**
00128         * {@inheritDoc}
00129         */
00130         virtual size32_t size() const;
00131 
00132         /**
00133         * {@inheritDoc}
00134         */
00135         virtual bool isEmpty() const;
00136 
00137         /**
00138         * {@inheritDoc}
00139         */
00140         virtual bool contains(Object::View v) const;
00141 
00142         /**
00143         * {@inheritDoc}
00144         */
00145         virtual bool containsAll(Collection::View vCol) const;
00146 
00147         /**
00148         * {@inheritDoc}
00149         */
00150         virtual Iterator::Handle iterator() const;
00151 
00152         /**
00153         * {@inheritDoc}
00154         */
00155         virtual Muterator::Handle iterator();
00156 
00157         /**
00158         * {@inheritDoc}
00159         */
00160         virtual ObjectArray::Handle toArray(
00161                 ObjectArray::Handle hao = NULL) const;
00162 
00163         /**
00164         * {@inheritDoc}
00165         */
00166         virtual bool add(Object::Holder oh);
00167 
00168         /**
00169         * {@inheritDoc}
00170         */
00171         virtual bool addAll(Collection::View vCol);
00172 
00173         /**
00174         * {@inheritDoc}
00175         */
00176         virtual bool remove(Object::View v);
00177 
00178         /**
00179         * {@inheritDoc}
00180         */
00181         virtual bool removeAll(Collection::View vCol);
00182 
00183         /**
00184         * {@inheritDoc}
00185         */
00186         virtual bool retainAll(Collection::View vCol);
00187 
00188         /**
00189         * {@inheritDoc}
00190         */
00191         virtual void clear();
00192 
00193 
00194     // ----- data members ---------------------------------------------------
00195 
00196     protected:
00197         /**
00198         * The underlying set (assumed immutable).
00199         */
00200         FinalHolder<Set> m_ohSetOrig;
00201 
00202         /**
00203         * The removed or retained items.
00204         */
00205         MemberHandle<Set> m_hSetMod;
00206 
00207         /**
00208         * Toggles between whether the modifications are removed or retained.
00209         */
00210         bool m_fRetained;
00211     };
00212 
00213 COH_CLOSE_NAMESPACE2
00214 
00215 #endif // COH_SUB_SET_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.