/*!
  @header ECCache
  @abstract Module of Encore
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Oliver Langer

  Author: Oliver Langer

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  24.02.05 ola     initial version
  22.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#include <Encore/ECIterator.h>
#include <Encore/ECCache.h>
#include <Encore/ECArrayIterator.h>
#include <Encore/ECCacheItem.h>
#include <Encore/ECMemory.h>


@implementation ECCache

- init {
  self = [super init];
  
  self->keyToCacheItem = [[NSMutableDictionary alloc] init];
  self->lock = [[NSLock alloc] init];
  
  return self;
}


- (void) dealloc {
  [self->keyToCacheItem release];
  [self->lock release];
  
  [super dealloc];
}


- (id) initWithCoder: (NSCoder *) decoder {
  self = [super init];
  
  self->keyToCacheItem = [[decoder decodeObject] retain];
  self->lock = [[NSLock alloc] init];
  
  return self;
}


- (void) encodeWithCoder: (NSCoder *) encoder {
  [encoder encodeObject: self->keyToCacheItem];
}


- addObject: (id) anObject forKey: (id <NSCopying>) aKey {
  ECCacheItem *newItem;
  
  EC_AUTORELEASEPOOL_BEGIN
  
  [self->lock lock];
  
  if( [self objectExistsForKey: aKey] ) {
    [[[ECIllegalArgumentException alloc]
	  initWithArgumentInfo: @"Given key is already in use" ] raise];
  }
  
  newItem = [[ECCacheItem alloc] initWithObject: anObject ];
  [newItem autorelease];
  [self->keyToCacheItem setObject: newItem forKey: aKey];
  
  [self->lock unlock];
  
  EC_AUTORELEASEPOOL_END
  
  return self;
}


- (id <ECIterator>) allKeys {
  return [[[ECArrayIterator alloc] 
    initWithArray: [self->keyToCacheItem allKeys]] autorelease];
}


- (BOOL) objectExistsForKey: (id) aKey {
  return nil != [self->keyToCacheItem objectForKey: aKey];
}


- objectForKey: (id) aKey incrementRefCounter: (BOOL) doIncrement {
  ECCacheItem *item;
  id toReturn = nil;
  
  [self->lock lock];
  
  item = (ECCacheItem *) [self->keyToCacheItem objectForKey: aKey];

  if( nil != item ) {
    toReturn = [item cachedObject];
    
    if( doIncrement ) {
      [item incrementCacheReferenceCount];
    }
  } 
  
  [self->lock unlock];
  
  return toReturn;
}


- (unsigned int) referenceCounterOfObjectForKey: (id) aKey {
  unsigned int toReturn;
  ECCacheItem *item;
  
  item = (ECCacheItem *) [self->keyToCacheItem objectForKey: aKey];
  
  if( nil != item ) {
    toReturn = [item cacheReferenceCount];
  } else {
    toReturn = 0;
  }

  return toReturn;
}


- (void) decrementRefCounterForKey: (id) aKey {
  ECCacheItem *item;
  
  [self->lock lock];
  
  item = (ECCacheItem *) [self->keyToCacheItem objectForKey: aKey];
  
  if( nil != item ) {
    [item decrementCacheReferenceCount];
	if( [item cacheReferenceCount] < 1 ) {
	  [self->keyToCacheItem removeObjectForKey: aKey];
	}
  }

  [self->lock unlock];
}


- (void) removeObjectForKey: (id) aKey {
  ECCacheItem *item;

  [self->lock lock];
  
  item = (ECCacheItem *) [self->keyToCacheItem objectForKey: aKey];
  if( nil != item ) {
    [self->keyToCacheItem removeObjectForKey: aKey];
  }
  
  [self->lock unlock];
}
@end
