Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Collections.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Beeyond Software Holding BV
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00023 #ifndef _CLASS_BEE_UTIL_COLLECTIONS_H
00024 #define _CLASS_BEE_UTIL_COLLECTIONS_H
00025 
00026 #ifdef __cplusplus
00027 
00028 #include "beecrypt/c++/lang/Object.h"
00029 using beecrypt::lang::Object;
00030 #include "beecrypt/c++/util/Set.h"
00031 using beecrypt::util::Set;
00032 #include "beecrypt/c++/lang/NullPointerException.h"
00033 using beecrypt::lang::NullPointerException;
00034 
00035 namespace beecrypt {
00036     namespace util {
00058         class BEECRYPTCXXAPI Collections
00059         {
00060         private:
00061             template<class E> class SynchronizedCollection : public beecrypt::lang::Object, public virtual beecrypt::util::Collection<E>
00062             {
00063             private:
00064                 Object* _mutex;
00065                 Collection<E>* _c;
00066 
00067             public:
00068                 SynchronizedCollection(Collection<E>* c, Object* mutex) : _c(c), _mutex(mutex)
00069                 {
00070                     if (!_mutex)
00071                         throw NullPointerException();
00072                 }
00073                 virtual ~SynchronizedCollection()
00074                 {
00075                     delete _c;
00076                 }
00077 
00078                 virtual bool add(E* e)
00079                 {
00080                     bool result = false;
00081                     synchronized (_mutex)
00082                     {
00083                         result = _c->add(e);
00084                     }
00085                     return result;
00086                 }
00087                 virtual bool addAll(const Collection<E>& c)
00088                 {
00089                     bool result = false;
00090                     synchronized (_mutex)
00091                     {
00092                         result = _c->addAll(c);
00093                     }
00094                     return result;
00095                 }
00096                 virtual void clear()
00097                 {
00098                     synchronized (_mutex)
00099                     {
00100                         _c->clear();
00101                     }
00102                 }
00103                 virtual bool contains(const E* e) const
00104                 {
00105                     bool result = false;
00106                     synchronized (_mutex)
00107                     {
00108                         result = _c->contains(e);
00109                     }
00110                     return result;
00111                 }
00112                 virtual bool containsAll(const Collection<E>& c) const
00113                 {
00114                     bool result = false;
00115                     synchronized (_mutex)
00116                     {
00117                         result = _c->containsAll(c);
00118                     }
00119                     return result;
00120                 }
00121                 #if 0
00122                 virtual bool equals(const Object* obj) const throw ()
00123                 {
00124                 }
00125                 #endif
00126                 virtual jint size() const throw ()
00127                 {
00128                     jint result = 0;
00129                     synchronized (_mutex)
00130                     {
00131                         result = _c->size();
00132                     }
00133                     return result;
00134                 }
00135             };
00136 
00137             template<class E> class SynchronizedSet : public SynchronizedCollection<E>, public virtual beecrypt::util::Set<E>
00138             {
00139                 virtual ~SynchronizedSet() {}
00140             };
00141 
00142         public:
00143             template<class E> static Collection<E>* synchronizedCollection(Collection<E>& c)
00144             {
00145                 return new SynchronizedCollection<E>(&c, &c);
00146             }
00147             template<class E> static Collection<E>* synchronizedCollection(Collection<E>& c, Object& mutex)
00148             {
00149                 return new SynchronizedCollection<E>(&c, &mutex);
00150             }
00151 
00152             template<class E> static Set<E>* synchronizedSet(Set<E>& s)
00153             {
00154                 return new SynchronizedSet<E>(&s, &s);
00155             }
00156             template<class E> static Set<E>* synchronizedSet(Set<E>& s, Object& mutex)
00157             {
00158                 return new SynchronizedSet<E>(&s, &mutex);
00159             }
00160         };
00161     }
00162 }
00163 
00164 #endif
00165 
00166 #endif

Generated on Sun Apr 24 16:41:43 2005 for BeeCrypt C++ by  doxygen 1.3.9.1