Added ArrayUtil#prepend()
authorkh5325.kim <kh5325.kim@samsung.com>
Thu, 22 Aug 2013 13:51:40 +0000 (22:51 +0900)
committerkh5325.kim <kh5325.kim@samsung.com>
Thu, 22 Aug 2013 13:51:40 +0000 (22:51 +0900)
Change-Id: I6993d00e8d80f7e2b08c2f52147047996ffd14ab

org.tizen.common/src/org/tizen/common/util/ArrayUtil.java

index c259b33..0d87833 100755 (executable)
@@ -812,7 +812,46 @@ public class ArrayUtil
                newArr[newArrLength - 1] = obj;
                return newArr;
        }
-       
+
+       /**
+        * prepend <code>obj</code> to <code>array</code>
+        * 
+        * Return array having only <code>obj</code> if <code>array</code> is <code>null</code>
+        * 
+        * @param array array to add element
+        * @param obj new element to add
+        * 
+        * @return added array
+        */
+       @SuppressWarnings("unchecked")
+       public static <T> T[]
+       prepend(
+               final T[] array,
+               final T obj
+       )
+       {
+               Class<?> compType = Object.class;
+               if ( null != array )
+               {
+                       compType = array.getClass().getComponentType();
+               }
+               else if ( null != obj )
+               {
+                       compType = obj.getClass();
+               }
+
+               final int newArrLength = size( array ) + 1;
+               final T[] newArr = 
+                       (T[]) Array.newInstance( compType, newArrLength );
+               
+               if ( null != array )
+               {
+                       System.arraycopy( array, 0, newArr, 1, array.length );
+               }
+               newArr[0] = obj;
+               return newArr;
+       }
+
        /**
         * remove elements whose index is between <code>startIndex</code> and <code>endIndex</code>
         *