//
// $Id: Pair.m,v 1.10 2005/05/18 06:46:54 will_mason Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#import "Pair.h"
#import "Constants.h"
#import <Foundation/NSCoder.h>

@implementation OLPair

- (id) initWithCoder: (NSCoder*)decoder
{
    [super init];
    if ([decoder respondsToSelector: @selector(allowsKeyedCoding)] &&
        [decoder allowsKeyedCoding])
    {
        first = OBJ_RETAIN([decoder decodeObjectForKey: FIRST_KEY]);
        second = OBJ_RETAIN([decoder decodeObjectForKey: SECOND_KEY]);
    }
    else
    {
        first = OBJ_RETAIN([decoder decodeObject]);
        second = OBJ_RETAIN([decoder decodeObject]);
    }
    return self;
}

- (id) initWithFirst: (id)f second: (id)s
{
    [super init];
    first = OBJ_RETAIN(f);
    second = OBJ_RETAIN(s);
    return self;
}

- (void) dealloc
{
    OBJ_RELEASE(first);
    OBJ_RELEASE(second);
    [super dealloc];
}

- (id) copyWithAllocator: (OLAllocator*)alloc
{
    return [[alloc allocateObject: [OLPair class]] initWithFirst: first second: second];
}

- (id) copyWithZone: (NSZone*)zone
{
    return [[OLPair allocWithZone: zone] initWithFirst: first second: second];
}

- (void) encodeWithCoder: (NSCoder*)encoder
{
    if ([encoder respondsToSelector: @selector(allowsKeyedCoding)] &&
        [encoder allowsKeyedCoding])
    {
        [encoder encodeObject: first forKey: FIRST_KEY];
        [encoder encodeObject: second forKey: SECOND_KEY];
    }
    else
    {
        [encoder encodeObject: first];
        [encoder encodeObject: second];
    }
}

- (id) first
{
    return first;
}

- (BOOL) isEqual: (id)object
{
    return [object isKindOfClass: [OLPair class]] &&
           [first isEqual: ((OLPair*)object)->first] &&
           [second isEqual: ((OLPair*)object)->second];
}

- (id) second
{
    return second;
}

- (void) setFirst: (id)f
{
    if (f != first)
    {
        OBJ_RELEASE(first);
        first = OBJ_RETAIN(f);
    }
}

- (void) setSecond: (id)s
{
    if (s != second)
    {
        OBJ_RELEASE(second);
        second = OBJ_RETAIN(s);
    }
}

@end
