00001 /* 00002 * Copyright (c) 2005 X-Way Rights 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 _ABSTRACT_CLASS_BEE_UTIL_ABSTRACTCOLLECTION_H 00024 #define _ABSTRACT_CLASS_BEE_UTIL_ABSTRACTCOLLECTION_H 00025 00026 #ifdef __cplusplus 00027 00028 #include "beecrypt/c++/lang/Comparable.h" 00029 using beecrypt::lang::Comparable; 00030 #include "beecrypt/c++/lang/StringBuilder.h" 00031 using beecrypt::lang::StringBuilder; 00032 #include "beecrypt/c++/lang/ClassCastException.h" 00033 using beecrypt::lang::ClassCastException; 00034 #include "beecrypt/c++/lang/UnsupportedOperationException.h" 00035 using beecrypt::lang::UnsupportedOperationException; 00036 #include "beecrypt/c++/util/Collection.h" 00037 using beecrypt::util::Collection; 00038 00039 namespace beecrypt { 00040 namespace util { 00045 template<class E> class AbstractCollection : public Object, public virtual Collection<E> 00046 { 00047 protected: 00048 AbstractCollection() {} 00049 00050 static inline bool equals(const E* e1, const E* e2) 00051 { 00052 if (e1 && e2) 00053 return e1->equals(e2); 00054 else 00055 return e1 == e2; 00056 } 00057 00058 public: 00059 virtual ~AbstractCollection() {} 00060 00061 virtual bool add(E* e) 00062 { 00063 throw UnsupportedOperationException(); 00064 } 00065 virtual bool addAll(const Collection<E>& c) 00066 { 00067 bool result = false; 00068 jint pos = c.size(); 00069 Iterator<E>* it = iterator(); 00070 assert(it != 0); 00071 while (--pos >= 0) 00072 result |= add(it->next()); 00073 delete it; 00074 return result; 00075 } 00076 virtual void clear() 00077 { 00078 jint pos = size(); 00079 Iterator<E>* it = iterator(); 00080 assert(it != 0); 00081 while (--pos >= 0) 00082 { 00083 it->next(); 00084 it->remove(); 00085 } 00086 delete it; 00087 } 00088 virtual bool contains(const E* e) const 00089 { 00090 bool result = false; 00091 jint pos = size(); 00092 Iterator<E>* it = iterator(); 00093 assert(it != 0); 00094 while (--pos >= 0) 00095 { 00096 if (equals(e, it->next())) 00097 { 00098 result = true; 00099 break; 00100 } 00101 } 00102 delete it; 00103 return result; 00104 } 00105 virtual bool containsAll(const Collection<E>& c) const 00106 { 00107 bool result = true; 00108 jint pos = c.size(); 00109 Iterator<E>* cit = c.iterator(); 00110 assert(cit != 0); 00111 while (--pos >= 0) 00112 if (!contains(cit->next())) 00113 { 00114 result = false; 00115 break; 00116 } 00117 delete cit; 00118 return result; 00119 } 00120 virtual bool equals(const Object* obj) const throw () 00121 { 00122 return Object::equals(obj); 00123 } 00124 virtual jint hashCode() const throw () 00125 { 00126 return Object::hashCode(); 00127 } 00128 virtual bool isEmpty() const throw () 00129 { 00130 return size() == 0; 00131 } 00132 virtual Iterator<E>* iterator() = 0; 00133 virtual Iterator<E>* iterator() const = 0; 00134 virtual bool remove(const E* e) 00135 { 00136 bool result = false; 00137 jint pos = size(); 00138 Iterator<E>* it = iterator(); 00139 assert(it != 0); 00140 while (--pos >= 0) 00141 { 00142 if (equals(e, it->next())) 00143 { 00144 it->remove(); 00145 result = true; 00146 break; 00147 } 00148 } 00149 delete it; 00150 return result; 00151 } 00152 virtual bool removeAll(const Collection<E>& c) 00153 { 00154 bool result = false; 00155 jint pos = size(); 00156 Iterator<E>* it = iterator(); 00157 assert(it != 0); 00158 while (--pos >= 0) 00159 if (c.contains(it->next())) 00160 { 00161 it->remove(); 00162 result = true; 00163 break; 00164 } 00165 delete it; 00166 return result; 00167 } 00168 virtual bool retainAll(const Collection<E>& c) 00169 { 00170 bool result = false; 00171 jint pos = c.size(); 00172 Iterator<E>* it = c.iterator(); 00173 assert(it != 0); 00174 while (--pos >= 0) 00175 if (!c.contains(it->next())) 00176 { 00177 it->remove(); 00178 result = true; 00179 break; 00180 } 00181 delete it; 00182 return result; 00183 } 00184 virtual jint size() const throw () = 0; 00185 virtual array<E*> toArray() const 00186 { 00187 jint pos = size(); 00188 array<E*> result(pos); 00189 Iterator<E>* it = iterator(); 00190 assert(it != 0); 00191 for (jint i = 0; --pos >= 0; i++) 00192 result[i] = it->next(); 00193 delete it; 00194 return result; 00195 } 00196 virtual String toString() const throw () 00197 { 00198 Iterator<E>* it = iterator(); 00199 assert(it != 0); 00200 00201 StringBuilder buf("["); 00202 00203 for (jint pos = size(); pos > 0; pos--) 00204 { 00205 E* e = it->next(); 00206 if (reinterpret_cast<const void*>(e) == reinterpret_cast<const void*>(this)) 00207 buf.append("(this Collection)"); 00208 else 00209 buf.append(e); 00210 00211 if (pos > 1) 00212 buf.append(", "); 00213 } 00214 delete it; 00215 00216 buf.append("]"); 00217 00218 return buf.toString(); 00219 } 00220 }; 00221 } 00222 } 00223 00224 #endif 00225 00226 #endif