#if !defined(_INLINE)
static char cvsid[] = "@(#)$Id: ContainerAdapter.cpp,v 1.4 1999/05/22 13:00:33 philogelos Exp $";
static char debugFileId[] = __FILE__;
#endif
#include "OGuard.hpp"
#include "PGuard.hpp"
#include "Debug.hpp"
#include "String.hpp"
#include "MethodMap.hpp"
#include "LinkManager.hpp"
#include "containers/ContainerAdapter.hpp"
#include "iter/enumerations/Enumeration.hpp"
#include "iter/enumerations/MappedEnumeration.hpp"
#include "iter/iterators/EnumerationIterator.hpp"
#include "iter/iterables/PrettyPrintIterable.hpp"
#include "algorithms/enumerations/EnumerationAlgorithms.hpp"
#include "exceptions/InvalidArgument.hpp"
ContainerAdapter::ContainerAdapter()
{}
ContainerAdapter::~ContainerAdapter()
{}
boolean ContainerAdapter::isEmpty() const
{
return ( getCardinality() == 0 );
}
Iterator *ContainerAdapter::getIterator() const
{
return new EnumerationIterator( getEnumeration() );
}
Enumeration *ContainerAdapter::getValueEnumeration() const
{
return new MappedEnumeration( getEnumeration(), getAsMap() );
}
Iterator *ContainerAdapter::getValueIterator() const
{
return new EnumerationIterator( getValueEnumeration() );
}
Index ContainerAdapter::getCardinality() const
{
return( EAlg::get()
-> getCardinality( getEnumeration() ) );
}
boolean ContainerAdapter::contains( const Top *anElement ) const
{
return( EAlg::get()
-> contains( getValueEnumeration(), anElement ) );
}
Position *ContainerAdapter::find( const Top *anObject ) const
{
OGuard _this( this, this );
OGuard _object( anObject, this );
PositionEnumeration *en;
en = getEnumeration();
OGuard _en( en, this );
while( en -> hasMoreElements() )
{
Position *pos;
pos = en -> getNextPosition();
{
PGuard _pos( pos, this );
if( pos -> getValue() -> equals( anObject ) )
{
return pos;
}
}
LinkManager::recycle( pos );
}
return ( Position * ) NIL;
}
boolean ContainerAdapter::equals( const Top *anOther ) const
{
preC_( anOther != null );
Enumeration *my;
Enumeration *his;
OGuard _( anOther, this );
if( DCAST( anOther, ContainerAdapter ) == null )
{
return false;
}
my = getValueEnumeration();
his = DCAST( anOther, ContainerAdapter ) -> getValueEnumeration();
OGuard _his( his, this );
OGuard _my( my, this );
while( true )
{
Top *myItem;
Top *hisItem;
if( ! my -> hasMoreElements() )
{
if( ! his -> hasMoreElements() )
{
return true;
}
else
{
return false;
}
}
if( ! his -> hasMoreElements() )
{
return false;
}
myItem = my -> getNextElement();
hisItem = his -> getNextElement();
OGuard _hisItem( hisItem, this );
OGuard _myItem( myItem, this );
if( typeid( myItem ) != typeid( hisItem ) )
{
return false;
}
if( ! myItem -> equals( hisItem ) )
{
return false;
}
}
}
String ContainerAdapter::print( String ( Top::* aMethod )() const ) const
{
Iterator *it;
PrettyPrintIterable *runner;
String result;
it = getValueIterator();
runner = new PrettyPrintIterable( "(", ", ", ")", aMethod );
OGuard _runner( runner, this );
OGuard _it( it, this );
it -> iterate( runner );
result = Object::getString() +
String( *( String * )( runner -> getResult() ) );
return result;
}
String ContainerAdapter::toString() const
{
return print( &Top::toString );
}
String ContainerAdapter::getString() const
{
return print( &Top::getString );
}
String ContainerAdapter::getClassName() const
{
return "ContainerAdapter";
}
boolean ContainerAdapter::isValid ( const Position *aPosition ) const
{
return( ( aPosition -> getContainer() ) == ( const Container * ) this );
}
Map *ContainerAdapter::getAsMap() const
{
return new ContainerAdapter::ContainerMap( ( Container * ) this );
}
ContainerAdapter::ContainerMap::ContainerMap( Container *aContainer )
{
preC_( aContainer != null );
container = aContainer;
}
Top *ContainerAdapter::ContainerMap::apply( Top *anArg ) THROWS( InvalidArgument * )
{
Position *position = DCAST( anArg, Position );
if( position != null )
{
OGuard _( position, this );
if( container -> isValid( position ) )
{
return( container -> getValue( position ) );
}
else
{
throw
( new InvalidArgument
( "ContainerAdapter::ContainerMap::apply(): argument is invalid Position",
nil ) );
}
}
else
{
throw
( new InvalidArgument
( "ContainerAdapter::ContainerMap::apply(): argument is not Position",
nil ) );
}
}
#if defined(_INLINE)
#include "../src/Debug.ipp"
#endif
#if defined(_INLINE)
#include "../src/String.ipp"
#endif