activemq-cpp-3.4.0

src/main/decaf/util/AbstractQueue.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef ABSTRACTQUEUE_H_
00019 #define ABSTRACTQUEUE_H_
00020 
00021 #include <decaf/util/Config.h>
00022 #include <decaf/lang/exceptions/UnsupportedOperationException.h>
00023 #include <decaf/lang/exceptions/NullPointerException.h>
00024 #include <decaf/lang/exceptions/IllegalArgumentException.h>
00025 #include <decaf/lang/exceptions/IllegalStateException.h>
00026 #include <decaf/lang/Iterable.h>
00027 #include <decaf/util/Iterator.h>
00028 #include <decaf/util/Queue.h>
00029 #include <memory>
00030 
00031 namespace decaf {
00032 namespace util {
00033 
00046     template< typename E >
00047     class AbstractQueue : public decaf::util::Queue<E>,
00048                           public decaf::util::AbstractCollection<E> {
00049     public:
00050 
00051         AbstractQueue() : Queue<E>() {}
00052 
00053         virtual ~AbstractQueue() {}
00054 
00061         virtual bool add( const E& value ) {
00062 
00063             if( offer( value ) ) {
00064                 return true;
00065             }
00066 
00067             throw decaf::lang::exceptions::IllegalStateException(
00068                 __FILE__, __LINE__, "Unable to add specified element to the Queue." );
00069         }
00070 
00078         virtual bool addAll( const Collection<E>& collection ) {
00079 
00080             if( this == &collection ) {
00081                 throw decaf::lang::exceptions::IllegalArgumentException(
00082                     __FILE__, __LINE__, "A Queue cannot be added to itself." );
00083             }
00084 
00085             return AbstractCollection<E>::addAll( collection );
00086         }
00087 
00088         using AbstractCollection<E>::remove;
00089 
00095         virtual E remove() {
00096 
00097             E result;
00098             if( this->poll( result ) == true ) {
00099                 return result;
00100             }
00101 
00102             throw decaf::util::NoSuchElementException(
00103                 __FILE__, __LINE__, "Unable to remove specified element from the Queue." );
00104         }
00105 
00112         virtual E element() const {
00113 
00114             E result;
00115             if( this->peek( result ) == true ) {
00116                 return result;
00117             }
00118 
00119             throw decaf::util::NoSuchElementException(
00120                 __FILE__, __LINE__, "Unable to remove specified element from the Queue." );
00121         }
00122 
00128         virtual void clear() {
00129 
00130             if( this->isEmpty() ) {
00131                 return;
00132             }
00133 
00134             E result;
00135             bool successful = true;
00136 
00137             do {
00138                 successful = this->poll( result );
00139             } while( successful );
00140         }
00141 
00142     };
00143 
00144 }}
00145 
00146 #endif /* ABSTRACTQUEUE_H_ */