API 0.9.5
Foundation/CPSortDescriptor.j
Go to the documentation of this file.
00001 /*
00002  * CPSortDescriptor.j
00003  * Foundation
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00033 @implementation CPSortDescriptor : CPObject
00034 {
00035     CPString    _key;
00036     SEL         _selector;
00037     BOOL        _ascending;
00038 }
00039 
00040 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending
00041 {
00042     return [[self alloc] initWithKey:aKey ascending:isAscending];
00043 }
00044 
00045 // Initializing a sort descriptor
00052 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending
00053 {
00054     return [self initWithKey:aKey ascending:isAscending selector:@selector(compare:)];
00055 }
00056 
00057 + (id)sortDescriptorWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
00058 {
00059     return [[self alloc] initWithKey:aKey ascending:isAscending selector:aSelector];
00060 }
00061 
00069 - (id)initWithKey:(CPString)aKey ascending:(BOOL)isAscending selector:(SEL)aSelector
00070 {
00071     self = [super init];
00072 
00073     if (self)
00074     {
00075         _key = aKey;
00076         _ascending = isAscending;
00077         _selector = aSelector;
00078     }
00079 
00080     return self;
00081 }
00082 
00083 // Getting information about a sort descriptor
00087 - (BOOL)ascending
00088 {
00089     return _ascending;
00090 }
00091 
00095 - (CPString)key
00096 {
00097     return _key;
00098 }
00099 
00103 - (SEL)selector
00104 {
00105     return _selector;
00106 }
00107 
00108 // Using sort descriptors
00115 - (CPComparisonResult)compareObject:(id)lhsObject withObject:(id)rhsObject
00116 {
00117     return (_ascending ? 1 : -1) * [[lhsObject valueForKeyPath:_key] performSelector:_selector withObject:[rhsObject valueForKeyPath:_key]];
00118 }
00119 
00124 - (id)reversedSortDescriptor
00125 {
00126     return [[[self class] alloc] initWithKey:_key ascending:!_ascending selector:_selector];
00127 }
00128 
00129 - (CPString)description
00130 {
00131     return [CPString stringWithFormat:@"(%@, %@, %@)",
00132         [self key], [self ascending] ? @"ascending": @"descending", CPStringFromSelector([self selector])];
00133 }
00134 
00135 @end
00136 
00137 var CPSortDescriptorKeyKey          = @"CPSortDescriptorKeyKey", // Don't you just love naming schemes ;)
00138     CPSortDescriptorAscendingKey    = @"CPSortDescriptorAscendingKey",
00139     CPSortDescriptorSelectorKey     = @"CPSortDescriptorSelectorKey";
00140 
00141 @implementation CPSortDescriptor (CPCoding)
00142 
00143 - (id)initWithCoder:(CPCoder)aCoder
00144 {
00145     if (self = [super init])
00146     {
00147         _key = [aCoder decodeObjectForKey:CPSortDescriptorKeyKey];
00148         _ascending = [aCoder decodeBoolForKey:CPSortDescriptorAscendingKey];
00149         _selector = CPSelectorFromString([aCoder decodeObjectForKey:CPSortDescriptorSelectorKey]);
00150     }
00151 
00152     return self;
00153 }
00154 
00155 - (void)encodeWithCoder:(CPCoder)aCoder
00156 {
00157     [aCoder encodeObject:_key forKey:CPSortDescriptorKeyKey];
00158     [aCoder encodeBool:_ascending forKey:CPSortDescriptorAscendingKey];
00159     [aCoder encodeObject:CPStringFromSelector(_selector) forKey:CPSortDescriptorSelectorKey];
00160 }
00161 
00162 @end
 All Classes Files Functions Variables Defines