FLEX-34854 The spark Sort now extends the mx version, to prevent the need of changing both classes when a bug is fixed or a feature is implemented. NOTES: -this is a result of the mailing list discussion entitled 'Can we unify spark and mx Sort and SortField?'. -some members of mx.collections.Sort needed to be marked protected so that the sort() function could access them. In the next commit they will be reverted by implementing a way in which the sort() function doesn't need to be overridden. -mx.collections.Sort needed to have a new method, createEmptySortField(), which is now used by noFieldsCompare(), and that ensures that the differences in spark.collections.Sort.noFieldsCompare() are preserved without having to copy-paste the entire method.
Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/3aacd762 Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/3aacd762 Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/3aacd762 Branch: refs/heads/develop Commit: 3aacd76295e213278612c4a6457e54df8f1d34f4 Parents: 2ce4252 Author: Mihai Chira <[email protected]> Authored: Sun Jun 21 21:15:03 2015 +0100 Committer: Mihai Chira <[email protected]> Committed: Sun Jun 21 21:15:03 2015 +0100 ---------------------------------------------------------------------- .../framework/src/mx/collections/Sort.as | 15 +- .../spark/src/spark/collections/Sort.as | 511 +------------------ 2 files changed, 30 insertions(+), 496 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/3aacd762/frameworks/projects/framework/src/mx/collections/Sort.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/framework/src/mx/collections/Sort.as b/frameworks/projects/framework/src/mx/collections/Sort.as index b296ed3..02cb41e 100644 --- a/frameworks/projects/framework/src/mx/collections/Sort.as +++ b/frameworks/projects/framework/src/mx/collections/Sort.as @@ -211,7 +211,7 @@ public class Sort extends EventDispatcher implements ISort * @private * Used for accessing localized Error messages. */ - private var resourceManager:IResourceManager = + protected var resourceManager:IResourceManager = ResourceManager.getInstance(); //-------------------------------------------------------------------------- @@ -233,7 +233,7 @@ public class Sort extends EventDispatcher implements ISort /** * @private */ - private var usingCustomCompareFunction:Boolean; + protected var usingCustomCompareFunction:Boolean; [Inspectable(category="General")] @@ -699,7 +699,7 @@ public class Sort extends EventDispatcher implements ISort * @private * Make sure all SortFields are ready to execute their comparators. */ - private function initSortFields(item:Object, buildArraySortArgs:Boolean = false):Object + protected function initSortFields(item:Object, buildArraySortArgs:Boolean = false):Object { var arraySortArgs:Object = null; var i:int; @@ -736,7 +736,7 @@ public class Sort extends EventDispatcher implements ISort * number of fields to check. We don't look at the actual values * to see if they match the actual sort. */ - private function internalCompare(a:Object, b:Object, fields:Array = null):int + protected function internalCompare(a:Object, b:Object, fields:Array = null):int { var result:int = 0; if (!_fields) @@ -777,7 +777,7 @@ public class Sort extends EventDispatcher implements ISort { if (!defaultEmptyField) { - defaultEmptyField = new SortField(); + defaultEmptyField = createEmptySortField(); try { defaultEmptyField.initializeDefaultCompareFunction(a); @@ -800,5 +800,10 @@ public class Sort extends EventDispatcher implements ISort return result; } + + protected function createEmptySortField():ISortField + { + return new SortField(); + } } } http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/3aacd762/frameworks/projects/spark/src/spark/collections/Sort.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/spark/src/spark/collections/Sort.as b/frameworks/projects/spark/src/spark/collections/Sort.as index 807f121..e0f1447 100644 --- a/frameworks/projects/spark/src/spark/collections/Sort.as +++ b/frameworks/projects/spark/src/spark/collections/Sort.as @@ -21,15 +21,10 @@ package spark.collections { import flash.events.Event; - - import mx.collections.ISort; + import mx.styles.IAdvancedStyleClient; import mx.collections.ISortField; import mx.collections.errors.SortError; import mx.core.FlexGlobals; - import mx.resources.IResourceManager; - import mx.resources.ResourceManager; - import mx.styles.AdvancedStyleClient; - import mx.utils.ObjectUtil; [DefaultProperty("fields")] [ResourceBundle("collections")] @@ -193,9 +188,10 @@ package spark.collections * @playerversion AIR 2.5 * @productversion Flex 4.5 */ -public class Sort extends AdvancedStyleClient implements ISort +public class Sort extends mx.collections.Sort implements IAdvancedStyleClient { include "../core/Version.as"; + include "AdvancedStyleClientImplementation.as"; //-------------------------------------------------------------------------- // @@ -257,11 +253,7 @@ public class Sort extends AdvancedStyleClient implements ISort */ public function Sort(fields:Array = null, customCompareFunction:Function = null, unique:Boolean = false) { - super(); - - this.fields = fields; - this.compareFunction = customCompareFunction; - this.unique = unique; + super(fields, customCompareFunction, unique); } //-------------------------------------------------------------------------- @@ -272,13 +264,6 @@ public class Sort extends AdvancedStyleClient implements ISort /** * @private - * Used for accessing localized Error messages. - */ - private var resourceManager:IResourceManager = - ResourceManager.getInstance(); - - /** - * @private * Cache for "locale" style. * * The code needs be able to find out if the locale style has been changed @@ -292,120 +277,7 @@ public class Sort extends AdvancedStyleClient implements ISort // //-------------------------------------------------------------------------- - //---------------------------------- - // compareFunction - //---------------------------------- - - /** - * @private - * Storage for the compareFunction property. - */ - private var _compareFunction:Function; - - /** - * @private - */ - private var usingCustomCompareFunction:Boolean; - - [Inspectable(category="General")] - - /** - * @inheritDoc - * - * @langversion 3.0 - * @playerversion Flash 10.1 - * @playerversion AIR 1.5 - * @productversion Flex 4.5 - */ - public function get compareFunction():Function - { - return usingCustomCompareFunction ? _compareFunction : internalCompare; - } - - /** - * @private - */ - public function set compareFunction(value:Function):void - { - _compareFunction = value; - usingCustomCompareFunction = _compareFunction != null; - } - - //---------------------------------- - // fields - //---------------------------------- - - /** - * @private - * Storage for the fields property. - */ - private var _fields:Array; - - - [Inspectable(category="General", arrayType="mx.collections.ISortField")] - [Bindable("fieldsChanged")] - - /** - * @inheritDoc - * - * @default null - * - * @see SortField - * - * @langversion 3.0 - * @playerversion Flash 10.1 - * @playerversion AIR 2.5 - * @productversion Flex 4.5 - */ - public function get fields():Array - { - return _fields; - } - - /** - * @private - */ - public function set fields(value:Array):void - { - _fields = value; - - dispatchEvent(new Event("fieldsChanged")); - } - - //---------------------------------- - // unique - //---------------------------------- - - /** - * @private - * Storage for the unique property. - */ - private var _unique:Boolean; - [Inspectable(category="General")] - - /** - * @inheritDoc - * - * @default false - * - * @langversion 3.0 - * @playerversion Flash 10.1 - * @playerversion AIR 1.5 - * @productversion Flex 4.5 - */ - public function get unique():Boolean - { - return _unique; - } - - /** - * @inheritDoc - */ - public function set unique(value:Boolean):void - { - _unique = value; - } //-------------------------------------------------------------------------- // @@ -416,10 +288,10 @@ public class Sort extends AdvancedStyleClient implements ISort /** * @private */ - override public function getStyle(styleProp:String):* + private function _getStyle(styleProp:String):* { if (styleProp != "locale") - return super.getStyle(styleProp); + return _advancedStyleClient.getStyle(styleProp); if ((localeStyle !== undefined) && (localeStyle !== null)) return localeStyle; @@ -444,9 +316,9 @@ public class Sort extends AdvancedStyleClient implements ISort * the class to be updated immediately when the locale style is * set directly on this class instance. */ - override public function setStyle(styleProp:String, newValue:*):void + private function _setStyle(styleProp:String, newValue:*):void { - super.setStyle(styleProp, newValue); + _advancedStyleClient.setStyle(styleProp, newValue); if (styleProp != "locale") return; @@ -474,21 +346,12 @@ public class Sort extends AdvancedStyleClient implements ISort * @playerversion AIR 2.5 * @productversion Flex 4.5 */ - override public function styleChanged(styleProp:String):void + private function _styleChanged(styleProp:String):void { localeChanged(); - super.styleChanged(styleProp); + _advancedStyleClient.styleChanged(styleProp); } - /** - * @private - * A pretty printer for Sort that lists the sort fields and their - * options. - */ - override public function toString():String - { - return ObjectUtil.toString(this); - } //-------------------------------------------------------------------------- // @@ -503,244 +366,8 @@ public class Sort extends AdvancedStyleClient implements ISort * @playerversion Flash 10.1 * @playerversion AIR 2.5 * @productversion Flex 4.5 - */ - public function findItem(items:Array, - values:Object, - mode:String, - returnInsertionIndex:Boolean = false, - compareFunction:Function = null):int - { - var compareForFind:Function; - var fieldsForCompare:Array; - var message:String; - - if (!items) - { - message = resourceManager.getString( - "collections", "noItems"); - throw new SortError(message); - } - else if (items.length == 0) - { - return returnInsertionIndex ? 1 : -1; - } - - if (compareFunction == null) - { - compareForFind = this.compareFunction; - // configure the search criteria - if (values && fields && fields.length > 0) - { - fieldsForCompare = []; - //build up the fields we can compare, if we skip a field in the - //middle throw an error. it is ok to not have all the fields - //though - var field:ISortField; - var hadPreviousFieldName:Boolean = true; - for (var i:int = 0; i < fields.length; i++) - { - field = fields[i] as ISortField; - if (field.name) - { - if (field.objectHasSortField(values)) - { - if (!hadPreviousFieldName) - { - message = resourceManager.getString( - "collections", "findCondition", [ field.name ]); - throw new SortError(message); - } - else - { - fieldsForCompare.push(field.name); - } - } - else - { - hadPreviousFieldName = false; - } - } - else - { - //this is ok because sometimes a sortfield might - //have a custom comparator - fieldsForCompare.push(null); - } - } - if (fieldsForCompare.length == 0) - { - message = resourceManager.getString( - "collections", "findRestriction"); - throw new SortError(message); - } - else - { - try - { - initSortFields(items[0]); - } - catch(initSortError:SortError) - { - //oh well, use the default comparators... - } - } - } - } - else - { - compareForFind = compareFunction; - } - - // let's begin searching - var found:Boolean = false; - var objFound:Boolean = false; - var index:int = 0; - var lowerBound:int = 0; - var upperBound:int = items.length -1; - var obj:Object = null; - var direction:int = 1; - while(!objFound && (lowerBound <= upperBound)) - { - index = Math.round((lowerBound+ upperBound)/2); - obj = items[index]; - //if we were given fields for comparison use that method, but - //if not the comparator may be for SortField in which case - //it'd be an error to pass a 3rd parameter - direction = fieldsForCompare - ? compareForFind(values, obj, fieldsForCompare) - : compareForFind(values, obj); - - switch(direction) - { - case -1: - upperBound = index -1; - break; - - case 0: - objFound = true; - switch(mode) - { - case ANY_INDEX_MODE: - found = true; - break; - - case FIRST_INDEX_MODE: - found = (index == lowerBound); - // start looking towards bof - var objIndex:int = index - 1; - var match:Boolean = true; - while(match && !found && (objIndex >= lowerBound)) - { - obj = items[objIndex]; - var prevCompare:int = fieldsForCompare - ? compareForFind(values, obj, fieldsForCompare) - : compareForFind(values, obj); - match = (prevCompare == 0); - if (!match || (match && (objIndex == lowerBound))) - { - found= true; - index = objIndex + (match ? 0 : 1); - } // if match - objIndex--; - } // while - break; - - case LAST_INDEX_MODE: - // if we where already at the edge case then we already found the last value - found = (index == upperBound); - // start looking towards eof - objIndex = index + 1; - match = true; - while(match && !found && (objIndex <= upperBound)) - { - obj = items[objIndex]; - var nextCompare:int = fieldsForCompare - ? compareForFind(values, obj, fieldsForCompare) - : compareForFind(values, obj); - match = (nextCompare == 0); - if (!match || (match && (objIndex == upperBound))) - { - found= true; - index = objIndex - (match ? 0 : 1); - } // if match - objIndex++; - } // while - break; - default: - { - message = resourceManager.getString( - "collections", "unknownMode"); - throw new SortError(message); - } - } // switch - break; - - case 1: - lowerBound = index +1; - break; - } // switch - } // while - if (!found && !returnInsertionIndex) - { - return -1; - } - else - { - return (direction > 0) ? index + 1 : index; - } - } - - /** - * @inheritDoc - * - * @langversion 3.0 - * @playerversion Flash 10.1 - * @playerversion AIR 2.5 - * @productversion Flex 4.5 */ - public function propertyAffectsSort(property:String):Boolean - { - if (usingCustomCompareFunction || !fields) return true; - for (var i:int = 0; i < fields.length; i++) - { - var field:ISortField = fields[i]; - if (field.name == property || field.usingCustomCompareFunction) - { - return true; - } - } - return false; - } - - /** - * @inheritDoc - * - * @langversion 3.0 - * @playerversion Flash 10.1 - * @playerversion AIR 2.5 - * @productversion Flex 4.5 - */ - public function reverse():void - { - if (fields) - { - for (var i:int = 0; i < fields.length; i++) - { - ISortField(fields[i]).reverse(); - } - } - noFieldsDescending = !noFieldsDescending; - } - - /** - * @inheritDoc - * - * @langversion 3.0 - * @playerversion Flash 10.1 - * @playerversion AIR 2.5 - * @productversion Flex 4.5 - */ - public function sort(items:Array):void + override public function sort(items:Array):void { if (!items || items.length <= 1) { @@ -757,7 +384,7 @@ public class Sort extends AdvancedStyleClient implements ISort function (a:Object, b:Object):int { // append our fields to the call, since items.sort() won't - return compareFunction(a, b, _fields); + return compareFunction(a, b, fields); }; var message:String; @@ -848,116 +475,18 @@ public class Sort extends AdvancedStyleClient implements ISort } } - /** - * @private - * Make sure all SortFields are ready to execute their comparators. - */ - private function initSortFields(item:Object, buildArraySortArgs:Boolean = false):Object - { - var arraySortArgs:Object = null; - var i:int; - for (i = 0; i<fields.length; i++) - { - ISortField(fields[i]).initializeDefaultCompareFunction(item); - } - if (buildArraySortArgs) - { - arraySortArgs = {fields: [], options: []}; - for (i = 0; i<fields.length; i++) - { - var field:ISortField = fields[i]; - var options:int = field.arraySortOnOptions; - if (options == -1) - { - return null; - } - else - { - arraySortArgs.fields.push(field.name); - arraySortArgs.options.push(options); - } - } - } - return arraySortArgs; - } - - /** - * @private - * Compares the values specified based on the sort field options specified - * for this sort. The fields parameter is really just used to get the - * number of fields to check. We don't look at the actual values - * to see if they match the actual sort. - */ - private function internalCompare(a:Object, b:Object, fields:Array = null):int - { - var result:int = 0; - if (!_fields) - { - result = noFieldsCompare(a, b); - } - else - { - var i:int = 0; - var len:int = fields ? fields.length : _fields.length; - while (result == 0 && (i < len)) - { - var sf:ISortField = ISortField(_fields[i]); - result = sf.compareFunction(a, b); - if (sf.descending) - result *= -1; - i++; - } - } - - return result; - } private var defaultEmptyField:SortField; - private var noFieldsDescending:Boolean = false; - /** - * If the sort does not have any sort fields nor a custom comparator - * just use an empty SortField object and have it use its default - * logic. - * - * @langversion 3.0 - * @playerversion Flash 10.1 - * @playerversion AIR 2.5 - * @productversion Flex 4.5 - */ - private function noFieldsCompare(a:Object, b:Object, fields:Array = null):int + override protected function createEmptySortField():ISortField { - if (!defaultEmptyField) - { - ensureStyleSource(); - - defaultEmptyField = new SortField(); - const locale:* = getStyle("locale"); - if (locale !== undefined) - defaultEmptyField.setStyle("locale", locale); - - try - { - defaultEmptyField.initializeDefaultCompareFunction(a); - } - catch(e:SortError) - { - //this error message isn't as useful in this case so replace - var message:String = resourceManager.getString( - "collections", "noComparator", [ a ]); - throw new SortError(message); - } - } - - var result:int = defaultEmptyField.compareFunction(a, b); - - if (noFieldsDescending) - { - result *= -1; - } - - return result; + ensureStyleSource(); + var sortField:SortField = new SortField(); + const locale:* = getStyle("locale"); + if (locale !== undefined) + sortField.setStyle("locale", locale); + return sortField; } /** @@ -973,7 +502,7 @@ public class Sort extends AdvancedStyleClient implements ISort */ private function localeChanged():void { - const newLocaleStyle:* = super.getStyle("locale"); + const newLocaleStyle:* = _advancedStyleClient.getStyle("locale"); if (localeStyle === newLocaleStyle) return;
