00001 /* 00002 * Association.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * Relation between two objects. 00006 * 00007 * Copyright (c) 2003 Milan Cermak 00008 */ 00009 /* 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 #include <stlib/Association.h> 00025 #include <stlib/String.h> 00026 #include <stlib/Stream.h> 00027 #include <stlib/Visitor.h> 00028 00029 Association::Association(Object *key, Object *value) 00030 { 00031 _key = key; 00032 _value = value; 00033 } 00034 00035 /* Copying protocol */ 00036 Association::Association(const Association &origin) 00037 : Object(origin) 00038 { 00039 _key = origin._key; 00040 _value = origin._value; 00041 } 00042 00043 /* Class-accessing protocol */ 00044 String *Association::className(void) const 00045 { 00046 return new String("Association"); 00047 } 00048 00049 /* Accessing protocol */ 00050 void Association::key(Object *object) 00051 { 00052 _key = object; 00053 } 00054 00055 Object *Association::key(void) 00056 { 00057 return _key; 00058 } 00059 00060 void Association::value(Object *object) 00061 { 00062 _value = object; 00063 } 00064 00065 Object *Association::value(void) 00066 { 00067 return _value; 00068 } 00069 00070 /* Comparing protocol */ 00071 long Association::hash(void) const 00072 { 00073 if (_key == nil) return Object::hash(); 00074 else return _key->hash(); 00075 } 00076 00077 bool Association::isEqual(const Object *object) const 00078 { 00079 if (object->isAssociation() && Object::isEqual(object)) { 00080 return _key == ((Association *) object)->key() && 00081 _value == ((Association *) object)->value(); 00082 } 00083 return false; 00084 } 00085 00086 /* Copying protocol */ 00087 Object *Association::copy(void) 00088 { 00089 return new Association(*this); 00090 } 00091 00092 /* Printing protocol */ 00093 void Association::printOn(Stream *stream) const 00094 { 00095 stream->print(_key); 00096 stream->nextPutAll("->"); 00097 if (_value == nil) stream->nextPutAll("nil"); 00098 else stream->print(_value); 00099 } 00100 00101 /* Testing protocol */ 00102 bool Association::isAssociation(void) const 00103 { 00104 return true; 00105 } 00106 00107 /* Visiting protocol */ 00108 void Association::visitBy(Visitor *visitor) 00109 { 00110 visitor->visitAssociation(this); 00111 }