Skip to content

Commit

Permalink
standardize semantics of all mergers,enhance mergeFactory and testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
luchy0120 committed Dec 11, 2018
1 parent bb3216d commit de12c48
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

import java.lang.reflect.Array;
Expand All @@ -25,34 +26,48 @@ public class ArrayMerger implements Merger<Object[]> {
public static final ArrayMerger INSTANCE = new ArrayMerger();

@Override
public Object[] merge(Object[]... others) {
if (others.length == 0) {
return null;
public Object[] merge(Object[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new Object[0];
}

int i = 0;
while (i < items.length && items[i] == null) {
i++;
}

if (i == items.length) {
return new Object[0];
}

Class<?> type = items[i].getClass().getComponentType();

int totalLen = 0;
for (int i = 0; i < others.length; i++) {
Object item = others[i];
if (item != null && item.getClass().isArray()) {
totalLen += Array.getLength(item);
} else {
throw new IllegalArgumentException((i + 1) + "th argument is not an array");
for (; i < items.length; i++) {
if (items[i] == null) {
continue;
}
Class<?> itemType = items[i].getClass().getComponentType();
if (itemType != type) {
throw new IllegalArgumentException("Arguments' types are different");
}
totalLen += items[i].length;
}

if (totalLen == 0) {
return null;
return new Object[0];
}

Class<?> type = others[0].getClass().getComponentType();

Object result = Array.newInstance(type, totalLen);

int index = 0;
for (Object array : others) {
for (int i = 0; i < Array.getLength(array); i++) {
Array.set(result, index++, Array.get(array, i));
for (Object[] array : items) {
if (array != null) {
for (int j = 0; j < array.length; j++) {
Array.set(result, index++, array[j]);
}
}
}
return (Object[]) result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

public class BooleanArrayMerger implements Merger<boolean[]> {

@Override
public boolean[] merge(boolean[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new boolean[0];
}
int totalLen = 0;
for (boolean[] array : items) {
totalLen += array.length;
if (array != null) {
totalLen += array.length;
}
}
boolean[] result = new boolean[totalLen];
int index = 0;
for (boolean[] array : items) {
for (boolean item : array) {
result[index++] = item;
if (array != null) {
for (boolean item : array) {
result[index++] = item;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

public class ByteArrayMerger implements Merger<byte[]> {

@Override
public byte[] merge(byte[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new byte[0];
}
int total = 0;
for (byte[] array : items) {
total += array.length;
if (array != null) {
total += array.length;
}
}
byte[] result = new byte[total];
int index = 0;
for (byte[] array : items) {
for (byte item : array) {
result[index++] = item;
if (array != null) {
for (byte item : array) {
result[index++] = item;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

public class CharArrayMerger implements Merger<char[]> {

@Override
public char[] merge(char[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new char[0];
}
int total = 0;
for (char[] array : items) {
total += array.length;
if (array != null) {
total += array.length;
}
}
char[] result = new char[total];
int index = 0;
for (char[] array : items) {
for (char item : array) {
result[index++] = item;
if (array != null) {
for (char item : array) {
result[index++] = item;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

public class DoubleArrayMerger implements Merger<double[]> {

@Override
public double[] merge(double[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new double[0];
}
int total = 0;
for (double[] array : items) {
total += array.length;
if (array != null) {
total += array.length;
}
}
double[] result = new double[total];
int index = 0;
for (double[] array : items) {
for (double item : array) {
result[index++] = item;
if (array != null) {
for (double item : array) {
result[index++] = item;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

public class FloatArrayMerger implements Merger<float[]> {

@Override
public float[] merge(float[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new float[0];
}
int total = 0;
for (float[] array : items) {
total += array.length;
if (array != null) {
total += array.length;
}
}
float[] result = new float[total];
int index = 0;
for (float[] array : items) {
for (float item : array) {
result[index++] = item;
if (array != null) {
for (float item : array) {
result[index++] = item;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

public class IntArrayMerger implements Merger<int[]> {

@Override
public int[] merge(int[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new int[0];
}
int totalLen = 0;
for (int[] item : items) {
totalLen += item.length;
for (int[] array : items) {
if (array != null) {
totalLen += array.length;
}
}
int[] result = new int[totalLen];
int index = 0;
for (int[] item : items) {
for (int i : item) {
result[index++] = i;
for (int[] array : items) {
if (array != null) {
for (int item : array) {
result[index++] = item;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

import java.util.ArrayList;
Expand All @@ -26,6 +27,9 @@ public class ListMerger implements Merger<List<?>> {

@Override
public List<Object> merge(List<?>... items) {
if (ArrayUtils.isEmpty(items)) {
return new ArrayList<Object>();
}
List<Object> result = new ArrayList<Object>();
for (List<?> item : items) {
if (item != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

public class LongArrayMerger implements Merger<long[]> {

@Override
public long[] merge(long[]... items) {
if (ArrayUtils.isEmpty(items)) {
return new long[0];
}
int total = 0;
for (long[] array : items) {
total += array.length;
if (array != null) {
total += array.length;
}
}
long[] result = new long[total];
int index = 0;
for (long[] array : items) {
for (long item : array) {
result[index++] = item;
if (array != null) {
for (long item : array) {
result[index++] = item;
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

import java.util.HashMap;
Expand All @@ -25,8 +26,8 @@ public class MapMerger implements Merger<Map<?, ?>> {

@Override
public Map<?, ?> merge(Map<?, ?>... items) {
if (items.length == 0) {
return null;
if (ArrayUtils.isEmpty(items)) {
return new HashMap<Object, Object>();
}
Map<Object, Object> result = new HashMap<Object, Object>();
for (Map<?, ?> item : items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class MergerFactory {
new ConcurrentHashMap<Class<?>, Merger<?>>();

public static <T> Merger<T> getMerger(Class<T> returnType) {
if (returnType == null) {
throw new IllegalArgumentException("returnType is null");
}

Merger result;
if (returnType.isArray()) {
Class type = returnType.getComponentType();
Expand All @@ -49,6 +53,11 @@ public static <T> Merger<T> getMerger(Class<T> returnType) {
result = mergerCache.get(returnType);
}
}

if (result == null) {
throw new IllegalArgumentException(returnType.getName() + " is not supported");
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.rpc.cluster.Merger;

import java.util.HashSet;
Expand All @@ -25,7 +26,9 @@ public class SetMerger implements Merger<Set<?>> {

@Override
public Set<Object> merge(Set<?>... items) {

if (ArrayUtils.isEmpty(items)) {
return new HashSet<Object>();
}
Set<Object> result = new HashSet<Object>();

for (Set<?> item : items) {
Expand Down
Loading

0 comments on commit de12c48

Please sign in to comment.