You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/05/19 09:37:27 UTC

[isis] 06/13: ISIS-1948: Internal API: adding 'combine' to _Arrays

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 1ec1a40c6a6104feeac290ec516a620ac55d5078
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri May 18 21:52:46 2018 +0200

    ISIS-1948: Internal API: adding 'combine' to _Arrays
    
    Task-Url: https://issues.apache.org/jira/browse/ISIS-1948
---
 .../isis/applib/internal/collections/_Arrays.java  | 43 ++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java b/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java
index 76f409f..e4ef798 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java
@@ -19,12 +19,15 @@
 
 package org.apache.isis.applib.internal.collections;
 
+import java.lang.reflect.Array;
 import java.util.Collection;
 import java.util.Objects;
 import java.util.stream.Collector;
 
 import javax.annotation.Nullable;
 
+import org.apache.isis.applib.internal._Constants;
+import org.apache.isis.applib.internal.base._Casts;
 import org.apache.isis.applib.internal.base._NullSafe;
 
 /**
@@ -85,6 +88,46 @@ public final class _Arrays {
     	Objects.requireNonNull(componentType);
 		return new _Arrays_CollectorUnknownSize<T>(componentType);
 	}
+    
+    // -- CONCATENATION
+    
+    /**
+     * Returns a new array containing all components {first, *rest} 
+     * @param first (non-null)
+     * @param rest (nullable)
+     * @return (non-null)
+     */
+    @SafeVarargs
+	public static <T> T[] combine(T first, @Nullable  T... rest) {
+    	Objects.requireNonNull(first);
+    	final int restLength = _NullSafe.size(rest);
+    	final T[] all = _Casts.uncheckedCast(Array.newInstance(first.getClass(), restLength+1));
+        all[0] = first;
+        if(restLength>0) {
+        	System.arraycopy(rest, 0, all, 1, restLength);
+        }
+        return all;
+    }
+    
+    /**
+     * Returns a new array containing all components {*first, *rest} 
+     * @param first (nullable)
+     * @param rest (nullable)
+     * @return (non-null)
+     */
+    @SafeVarargs
+	public static <T> T[] combine(T[] first, T... rest) {
+    	final int firstLength = _NullSafe.size(first);
+    	final int restLength = _NullSafe.size(rest);
+    	if(firstLength + restLength == 0) {
+    		return _Casts.uncheckedCast(_Constants.emptyObjects);
+    	}
+    	final Class<?> componentType = firstLength>0 ? first[0].getClass() : rest[0].getClass();
+    	final T[] all = _Casts.uncheckedCast(Array.newInstance(componentType, firstLength + restLength));
+    	System.arraycopy(first, 0, all, 0, firstLength);
+        System.arraycopy(rest, 0, all, firstLength, restLength);
+        return all;
+    }
 
     // -- CONSTRUCTION
     

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.