coherence/util/LinkedList.hpp

00001 /*
00002 * LinkedList.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_LINKED_LIST_HPP
00017 #define COH_LINKED_LIST_HPP
00018 
00019 #include "coherence/lang.ns"
00020 
00021 #include "coherence/util/Collection.hpp"
00022 #include "coherence/util/ListIterator.hpp"
00023 #include "coherence/util/AbstractList.hpp"
00024 #include "coherence/util/SubList.hpp"
00025 
00026 COH_OPEN_NAMESPACE2(coherence,util)
00027 
00028 
00029 /**
00030 * A linked list implementation of List.
00031 *
00032 * @author nsa 2008.02.04
00033 */
00034 class COH_EXPORT LinkedList
00035     : public cloneable_spec<LinkedList,
00036         extends<AbstractList> >
00037     {
00038     friend class factory<LinkedList>;
00039 
00040     // ----- constructors ---------------------------------------------------
00041 
00042     protected:
00043         /**
00044         * Create a new LinkedList.
00045         *
00046         * @return a new LinkedList
00047         */
00048         LinkedList();
00049 
00050         /**
00051         * Create a new LinkedList that has a reference to every element in
00052         * the supplied collection.
00053         *
00054         * @param vc  The collection to base the LinkedList on
00055         *
00056         * @return a new LinkedList
00057         */
00058         LinkedList(Collection::View vc);
00059 
00060         /**
00061         * @internal
00062         */
00063         LinkedList(const LinkedList& that);
00064 
00065         /**
00066         * Destructor.
00067         */
00068         virtual ~LinkedList();
00069 
00070 
00071     // ----- List interface -------------------------------------------------
00072 
00073     public:
00074         /**
00075         * {@inheritDoc}
00076         */
00077         virtual bool add(size32_t i, Object::Holder oh);
00078 
00079         /**
00080         * {@inheritDoc}
00081         */
00082         virtual bool addAll(size32_t i, Collection::View vc);
00083 
00084         /**
00085         * {@inheritDoc}
00086         */
00087         virtual Object::Holder get(size32_t i) const;
00088 
00089         /**
00090         * {@inheritDoc}
00091         */
00092         using List::get;
00093 
00094         /**
00095         * {@inheritDoc}
00096         */
00097         virtual size32_t indexOf(Object::View v) const;
00098 
00099         /**
00100         * {@inheritDoc}
00101         */
00102         virtual size32_t lastIndexOf(Object::View v) const;
00103 
00104         /**
00105         * {@inheritDoc}
00106         */
00107         virtual ListIterator::Handle listIterator(size32_t index = 0) const;
00108 
00109         /**
00110         * {@inheritDoc}
00111         */
00112         virtual ListMuterator::Handle listIterator(size32_t index = 0);
00113 
00114         /**
00115         * {@inheritDoc}
00116         */
00117         virtual Object::Holder remove(size32_t index);
00118 
00119         /**
00120         * {@inheritDoc}
00121         */
00122         virtual Object::Holder set(size32_t index, Object::Holder oh);
00123 
00124         /**
00125         * {@inheritDoc}
00126         */
00127         virtual List::View subList(size32_t fromIndex, size32_t toIndex) const;
00128 
00129         /**
00130         * {@inheritDoc}
00131         */
00132         virtual List::Handle subList(size32_t fromIndex, size32_t toIndex);
00133 
00134 
00135     // ----- Collection interface -------------------------------------------
00136 
00137     public:
00138         /**
00139         * {@inheritDoc}
00140         */
00141         virtual size32_t size() const;
00142 
00143         /**
00144         * {@inheritDoc}
00145         */
00146         virtual Iterator::Handle iterator() const;
00147 
00148         /**
00149         * {@inheritDoc}
00150         */
00151         virtual Muterator::Handle iterator();
00152 
00153         /**
00154         * {@inheritDoc}
00155         */
00156         virtual bool add(Object::Holder oh);
00157 
00158         /**
00159         * {@inheritDoc}
00160         */
00161         virtual bool addAll(Collection::View vc);
00162 
00163         /**
00164         * {@inheritDoc}
00165         */
00166         virtual bool remove(Object::View v);
00167 
00168         /**
00169         * {@inheritDoc}
00170         */
00171         virtual bool removeAll(Collection::View vColl);
00172 
00173         /**
00174         * {@inheritDoc}
00175         */
00176         virtual bool retainAll(Collection::View vCol);
00177 
00178         /**
00179         * {@inheritDoc}
00180         */
00181         virtual void clear();
00182 
00183 
00184     // ----- Object interface -----------------------------------------------
00185 
00186     protected:
00187         /**
00188         * {@inheritDoc}
00189         */
00190         virtual void onEscape(bool fEscaped) const;
00191 
00192 
00193     // ----- nested class: ListElement --------------------------------------
00194 
00195     public:
00196         /**
00197         * ListElement is the foundation element for the linked list
00198         */
00199         class ListElement
00200             : public class_spec<ListElement>
00201             {
00202             friend class factory<ListElement>;
00203 
00204             // ----- constructor ----------------------------------------
00205 
00206             protected:
00207                 /**
00208                 * Create a new ListElement object.
00209                 *
00210                 * @param hNext  the element that will be next in the list
00211                 * @param hPrev  the element that will be previous in the
00212                 *               list
00213                 * @param hObj   the object this list element contains
00214                 *
00215                 * @return a newly created ListElement
00216                 */
00217                 ListElement(ListElement::Handle hNext,
00218                         ListElement::Handle hPrev, Object::Holder hObj);
00219 
00220 
00221             // ----- ListElement interface ------------------------------
00222 
00223             public:
00224                 /**
00225                 * Helper method to find a ListElement at the specified
00226                 * position
00227                 *
00228                 * @param thElement  the starting position to find the element
00229                 *                   from
00230                 * @param c          the index of the element to find
00231                 *
00232                 * @return a ListElement at the specified position
00233                 *
00234                 * @throws coherence::lang::IndexOutOfBoundsException if the
00235                 *         index is out of range
00236                 */
00237                 template<class T> static TypedHandle<T>
00238                         nextElement(const TypedHandle<T>& thElement, size32_t c);
00239 
00240             // ----- data members ---------------------------------------
00241 
00242             public:
00243                 /**
00244                 * Reference to the next element in the list
00245                 */
00246                 mutable MemberHandle<ListElement> next;
00247 
00248                 /**
00249                 * Reference to the previous element in the list
00250                 */
00251                 MemberHandle<ListElement> previous;
00252 
00253                 /**
00254                 * Reference to the actual object contained by this list
00255                 */
00256                 MemberHolder<Object> value;
00257             };
00258 
00259 
00260     // ----- nested class: SubLinkedList ----------------------------------
00261 
00262     public:
00263         /**
00264         * Utility class to implement a SubList of a LinkedList.
00265         * SubLinkedList delegates through the the LinkedList for all
00266         * modification operations
00267         */
00268         class SubLinkedList
00269             : public class_spec<SubLinkedList,
00270                 extends<SubList> >
00271             {
00272             friend class factory<SubLinkedList>;
00273 
00274             // ----- constructors ---------------------------------------
00275 
00276             protected:
00277                 /**
00278                 * create a new SubLinkedList.
00279                 *
00280                 * @param fromIndex  the starting point of the sublist in the
00281                 *                   list provided (inclusive).
00282                 * @param toIndex    the end point of the sublist in the list
00283                 *                   provided (exclusive)
00284                 * @param ohList     the list to create a sublist of
00285                 *
00286                 * @return a new SubLinkedList
00287                 */
00288                 SubLinkedList(size32_t fromIndex, size32_t toIndex,
00289                         List::Holder ohList);
00290 
00291 
00292             // ----- List interface --------------------------------------
00293 
00294             public:
00295                 /**
00296                 * {@inheritDoc}
00297                 */
00298                 virtual bool retainAll(Collection::View vColl);
00299 
00300                 /**
00301                 * {@inheritDoc}
00302                 */
00303                 virtual void clear();
00304 
00305                 /**
00306                 * {@inheritDoc}
00307                 */
00308                 virtual List::View
00309                         subList(size32_t fromIndex, size32_t toIndex) const;
00310 
00311                 /**
00312                 * {@inheritDoc}
00313                 */
00314                 virtual List::Handle
00315                         subList(size32_t fromIndex, size32_t toIndex);
00316             };
00317 
00318 
00319     // ----- helper methods -------------------------------------------------
00320 
00321     protected:
00322         /**
00323         * Helper method that will throw a
00324         * coherence::lang::IndexOutOfBoundsException if the supplied index
00325         * is out of range
00326         *
00327         * @param i  the index to test
00328         */
00329         virtual void assertIndex(size32_t i) const;
00330 
00331     private:
00332         /**
00333         * Remove a ListElement from the linked list.
00334         *
00335         * @param hElement  the element to remove
00336         */
00337         Object::Holder removeElement(ListElement::Handle hElement,
00338                 size32_t index);
00339 
00340         /**
00341         * Add a ListElement to the linked list.
00342         *
00343         * @param hNext  the element which will follow the new element
00344         * @param oh     the value portion of the new element
00345         */
00346         void addElement(ListElement::Handle hNext, Object::Holder oh);
00347 
00348     // ----- data members ---------------------------------------------------
00349 
00350     protected:
00351         /**
00352         * The number of elements in the list
00353         */
00354         size32_t m_cElements;
00355 
00356         /**
00357         * The head of the list.
00358         */
00359         mutable MemberHandle<ListElement> m_hHead;
00360 
00361         /**
00362         * The tail of the list.
00363         */
00364         mutable MemberHandle<ListElement> m_hTail;
00365 
00366 
00367     // ----- friends --------------------------------------------------------
00368 
00369     friend class SubLinkedList;
00370     friend class LinkedListIterator;
00371     };
00372 
00373 COH_CLOSE_NAMESPACE2
00374 
00375 #endif // COH_LINKED_LIST_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.