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 12, 2018
1 parent 9d4b6cb commit 9761fbc
Show file tree
Hide file tree
Showing 14 changed files with 296 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,15 +17,20 @@

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;
import java.util.Collections;
import java.util.List;

public class ListMerger implements Merger<List<?>> {

@Override
public List<Object> merge(List<?>... items) {
if (ArrayUtils.isEmpty(items)) {
return Collections.emptyList();
}
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,17 +16,19 @@
*/
package org.apache.dubbo.rpc.cluster.merger;

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

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MapMerger implements Merger<Map<?, ?>> {

@Override
public Map<?, ?> merge(Map<?, ?>... items) {
if (items.length == 0) {
return null;
if (ArrayUtils.isEmpty(items)) {
return Collections.emptyMap();
}
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 @@ -30,7 +30,19 @@ public class MergerFactory {
private static final ConcurrentMap<Class<?>, Merger<?>> mergerCache =
new ConcurrentHashMap<Class<?>, Merger<?>>();

/**
* Find the merger according to the returnType class, the merger will
* merge an array of returnType into one
*
* @param returnType the merger will return this type
* @return the merger which merges an array of returnType into one, return null if not exist
* @throws IllegalArgumentException if returnType is null
*/
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
*/
package org.apache.dubbo.rpc.cluster.merger;

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

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class SetMerger implements Merger<Set<?>> {

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

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

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

0 comments on commit 9761fbc

Please sign in to comment.