Repository: flex-sdk
Updated Branches:
  refs/heads/develop eec643c45 -> 5be0fedb6


FLEX-26808
Moving Vector-related function List.getFirstItemValue() to new VectorUtil class 
(unit tested in VectorUtilTests.as)


Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/5d677c20
Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/5d677c20
Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/5d677c20

Branch: refs/heads/develop
Commit: 5d677c2079eedcece9beb25469ea370347f10081
Parents: eec643c
Author: Mihai Chira <[email protected]>
Authored: Tue Mar 22 12:28:26 2016 +0100
Committer: Mihai Chira <[email protected]>
Committed: Tue Mar 22 12:28:26 2016 +0100

----------------------------------------------------------------------
 .../framework/src/mx/utils/VectorUtil.as        | 33 +++++++
 .../framework/tests/mx/utils/VectorUtilTests.as | 97 ++++++++++++++++++++
 .../projects/spark/src/spark/components/List.as | 23 +----
 3 files changed, 135 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/5d677c20/frameworks/projects/framework/src/mx/utils/VectorUtil.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/utils/VectorUtil.as 
b/frameworks/projects/framework/src/mx/utils/VectorUtil.as
new file mode 100644
index 0000000..79a0056
--- /dev/null
+++ b/frameworks/projects/framework/src/mx/utils/VectorUtil.as
@@ -0,0 +1,33 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package mx.utils {
+    public class VectorUtil {
+        /**
+         *  Given a Vector, returns the value of the first item,
+         *  or -1 if there are no items in the Vector;
+         */
+        public static function getFirstItemValue(v:Vector.<int>):int
+        {
+            if (v && v.length > 0)
+                return v[0];
+            else
+                return -1;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/5d677c20/frameworks/projects/framework/tests/mx/utils/VectorUtilTests.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/tests/mx/utils/VectorUtilTests.as 
b/frameworks/projects/framework/tests/mx/utils/VectorUtilTests.as
new file mode 100644
index 0000000..e42cea5
--- /dev/null
+++ b/frameworks/projects/framework/tests/mx/utils/VectorUtilTests.as
@@ -0,0 +1,97 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package mx.utils {
+    import org.flexunit.asserts.assertEquals;
+    import org.flexunit.asserts.assertStrictlyEquals;
+    import org.flexunit.asserts.assertTrue;
+
+    public class VectorUtilTests {
+        [Test]
+        public function test_empty_vector():void
+        {
+            assertEquals(-1, VectorUtil.getFirstItemValue(new Vector.<int>()));
+        }
+
+        [Test]
+        public function test_null_parameter():void
+        {
+            assertEquals(-1, VectorUtil.getFirstItemValue(null));
+        }
+
+        [Test]
+        public function test_vector_with_three_elements():void
+        {
+            //given
+            var vector:Vector.<int> = new Vector.<int>();
+
+            //when
+            vector.push(3, 2, 1);
+
+            //then
+            assertEquals(3, VectorUtil.getFirstItemValue(vector));
+        }
+
+        [Test]
+        public function 
test_vector_with_three_elements_using_global_constructor():void
+        {
+            //given
+            var vector:Vector.<int> = new <int>[35, 25, 15];
+
+            //then
+            assertEquals(35, VectorUtil.getFirstItemValue(vector));
+        }
+
+        [Test]
+        public function test_vector_with_first_element_minus_1():void
+        {
+            //given
+            var vector:Vector.<int> = new <int>[-1, 2, 1];
+
+            //then
+            assertEquals(-1, VectorUtil.getFirstItemValue(vector));
+        }
+
+        [Test]
+        public function test_null_and_undefined_transformed_into_0():void
+        {
+            //given
+            var vector:Vector.<int> = new <int>[undefined, null, 11];
+
+            //then
+            assertStrictlyEquals(0, vector[0]);
+            assertStrictlyEquals(0, vector[1]);
+            assertEquals(0, VectorUtil.getFirstItemValue(vector));
+        }
+
+        [Test]
+        public function test_non_integers_floored():void
+        {
+            //given
+            var vector:Vector.<int> = new Vector.<int>();
+
+            //when
+            vector.push(3.74, 2.5);
+
+            //then
+            assertStrictlyEquals(3, vector[0]);
+            assertStrictlyEquals(2, vector[1]);
+            assertEquals(3, VectorUtil.getFirstItemValue(vector));
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/5d677c20/frameworks/projects/spark/src/spark/components/List.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/spark/src/spark/components/List.as 
b/frameworks/projects/spark/src/spark/components/List.as
index 63329dc..1e2bcec 100644
--- a/frameworks/projects/spark/src/spark/components/List.as
+++ b/frameworks/projects/spark/src/spark/components/List.as
@@ -54,7 +54,7 @@ import mx.managers.DragManager;
 import mx.managers.IFocusManagerComponent;
 import mx.utils.ObjectUtil;
 import mx.utils.UIDUtil;
-
+import mx.utils.VectorUtil;
 import spark.components.supportClasses.ListBase;
 import spark.core.NavigationUnit;
 import spark.events.IndexChangeEvent;
@@ -1242,7 +1242,7 @@ public class List extends ListBase implements 
IFocusManagerComponent
         }
         // Keep _proposedSelectedIndex in-sync with multiple selection 
properties. 
         if (!isEmpty(_proposedSelectedIndices))
-            _proposedSelectedIndex = 
getFirstItemValue(_proposedSelectedIndices); 
+            _proposedSelectedIndex = 
VectorUtil.getFirstItemValue(_proposedSelectedIndices);
         
         // need to store changeCaretOnSelection since super.commitSelection() 
may change it
         var currentChangeCaretOnSelection:Boolean = changeCaretOnSelection;
@@ -1572,19 +1572,6 @@ public class List extends ListBase implements 
IFocusManagerComponent
     
     /**
      *  @private
-     *  Given a Vector, returns the value of the first item, 
-     *  or -1 if there are no items in the Vector; 
-     */
-    private function getFirstItemValue(v:Vector.<int>):int
-    {
-        if (v && v.length > 0)
-            return v[0]; 
-        else 
-            return -1; 
-    }
-    
-    /**
-     *  @private
      *  Returns true if v is null or an empty Vector.
      */
     private function isEmpty(v:Vector.<int>):Boolean
@@ -1877,7 +1864,7 @@ public class List extends ListBase implements 
IFocusManagerComponent
             return;
         
         // Handle the fixup of selection
-        var newIndex:int
+        var newIndex:int;
         if (event.currentTarget is IItemRenderer)
             newIndex = IItemRenderer(event.currentTarget).itemIndex;
         else
@@ -2684,7 +2671,7 @@ public class List extends ListBase implements 
IFocusManagerComponent
             // an "caretChange" event to update any bindings and update the 
             // caretIndex backing variable. 
             var oldIndex:Number = caretIndex; 
-            _caretIndex = getFirstItemValue(newInterval);
+            _caretIndex = VectorUtil.getFirstItemValue(newInterval);
             e = new IndexChangeEvent(IndexChangeEvent.CARET_CHANGE); 
             e.oldIndex = oldIndex; 
             e.newIndex = caretIndex; 
@@ -2706,7 +2693,7 @@ public class List extends ListBase implements 
IFocusManagerComponent
         
         var oldIndices:Vector.<int> = selectedIndices;  
         _selectedIndices = newInterval;
-        _selectedIndex = getFirstItemValue(newInterval);
+        _selectedIndex = VectorUtil.getFirstItemValue(newInterval);
         // If the selection has actually changed, trigger a pass to 
         // commitProperties where a change event will be 
         // fired to update any bindings to selection properties. 

Reply via email to