![]() |
API 0.9.5
|
00001 /* 00002 * CPExpression_set.j 00003 * 00004 * Created by cacaodev. 00005 * Copyright 2010. 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 00023 @implementation CPExpression_set : CPExpression 00024 { 00025 CPExpression _left; 00026 CPExpression _right; 00027 } 00028 00029 - (id)initWithType:(int)type left:(CPExpression)left right:(CPExpression)right 00030 { 00031 self = [super initWithExpressionType:type]; 00032 00033 if (self) 00034 { 00035 _left = left; 00036 _right = right; 00037 } 00038 00039 return self; 00040 } 00041 00042 - (BOOL)isEqual:(id)object 00043 { 00044 if (self == object) 00045 return YES; 00046 00047 if (object.isa != self.isa || [object expressionType] != [self expressionType] || ![[object leftExpression] isEqual:[self leftExpression]] || ![[object rightExpression] isEqual:[self rightExpression]]) 00048 return NO; 00049 00050 return YES; 00051 } 00052 00053 - (id)expressionValueWithObject:object context:(CPDictionary)context 00054 { 00055 var right = [_right expressionValueWithObject:object context:context]; 00056 if ([right isKindOfClass:[CPArray class]]) 00057 right = [CPSet setWithArray:right]; 00058 else if ([right isKindOfClass:[CPDictionary class]]) 00059 right = [CPSet setWithArray:[right allValues]]; 00060 else if (![right isKindOfClass:[CPSet class]]) 00061 [CPException raise:CPInvalidArgumentException reason:@"The right expression for a CP*SetExpressionType expression must evaluate to a CPArray, CPDictionary or CPSet"]; 00062 00063 var left = [_left expressionValueWithObject:object context:context]; 00064 if (![left isKindOfClass:[CPSet class]]) 00065 [CPException raise:CPInvalidArgumentException reason:@"The left expression for a CP*SetExpressionType expression must evaluate to a CPSet"]; 00066 00067 var result = [left copy]; 00068 switch (_type) 00069 { 00070 case CPIntersectSetExpressionType : [result intersectSet:right]; 00071 break; 00072 case CPUnionSetExpressionType : [result unionSet:right]; 00073 break; 00074 case CPMinusSetExpressionType : [result minusSet:right]; 00075 break; 00076 default: 00077 } 00078 00079 return result; 00080 } 00081 00082 - (CPExpression )_expressionWithSubstitutionVariables:(CPDictionary )variables 00083 { 00084 // UNIMPLEMENTED 00085 return self; 00086 } 00087 00088 - (CPExpression)leftExpression 00089 { 00090 return _left; 00091 } 00092 00093 - (CPExpression)rightExpression 00094 { 00095 return _right; 00096 } 00097 00098 - (CPString)description 00099 { 00100 var desc; 00101 switch (_type) 00102 { 00103 case CPIntersectSetExpressionType : desc = @" INTERSECT "; 00104 break; 00105 case CPUnionSetExpressionType : desc = @" UNION "; 00106 break; 00107 case CPMinusSetExpressionType : desc = @" MINUS "; 00108 break; 00109 default: 00110 } 00111 00112 return [_left description] + desc + [_right description]; 00113 } 00114 00115 @end 00116 00117 var CPLeftExpressionKey = @"CPLeftExpression", 00118 CPRightExpressionKey = @"CPRightExpression", 00119 CPExpressionType = @"CPExpressionType"; 00120 00121 @implementation CPExpression_set (CPCoding) 00122 00123 - (id)initWithCoder:(CPCoder)coder 00124 { 00125 var left = [coder decodeObjectForKey:CPLeftExpressionKey], 00126 right = [coder decodeObjectForKey:CPRightExpressionKey], 00127 type = [coder decodeIntForKey:CPExpressionType]; 00128 00129 return [self initWithType:type left:left right:right]; 00130 } 00131 00132 - (void)encodeWithCoder:(CPCoder)coder 00133 { 00134 [coder encodeObject:_left forKey:CPLeftExpressionKey]; 00135 [coder encodeObject:_right forKey:CPRightExpressionKey]; 00136 [coder encodeInt:_type forKey:CPExpressionType]; 00137 } 00138 00139 @end