mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-01 12:13:48 +00:00
feat[protocol]: ArrayUtils新增list转array
This commit is contained in:
@@ -20,6 +20,7 @@ import com.zfoo.net.core.tcp.TcpClient;
|
||||
import com.zfoo.net.core.tcp.TcpServer;
|
||||
import com.zfoo.net.session.model.AttributeType;
|
||||
import com.zfoo.net.util.SessionUtils;
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
import com.zfoo.protocol.collection.ConcurrentArrayList;
|
||||
import com.zfoo.protocol.collection.ConcurrentHashSet;
|
||||
import com.zfoo.protocol.exception.ExceptionUtils;
|
||||
@@ -247,14 +248,14 @@ public class ZookeeperRegistry implements IRegistry {
|
||||
if (Objects.isNull(providerStat)) {
|
||||
curator.create()
|
||||
.withMode(CreateMode.PERSISTENT)
|
||||
.forPath(PROVIDER_ROOT_PATH, StringUtils.EMPTY_BYTES);
|
||||
.forPath(PROVIDER_ROOT_PATH, ArrayUtils.EMPTY_BYTE_ARRAY);
|
||||
}
|
||||
|
||||
var consumerStat = curator.checkExists().forPath(CONSUMER_ROOT_PATH);
|
||||
if (Objects.isNull(consumerStat)) {
|
||||
curator.create()
|
||||
.withMode(CreateMode.PERSISTENT)
|
||||
.forPath(CONSUMER_ROOT_PATH, StringUtils.EMPTY_BYTES);
|
||||
.forPath(CONSUMER_ROOT_PATH, ArrayUtils.EMPTY_BYTE_ARRAY);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -536,7 +537,7 @@ public class ZookeeperRegistry implements IRegistry {
|
||||
curator.create()
|
||||
.creatingParentsIfNeeded()
|
||||
.withMode(CreateMode.PERSISTENT)
|
||||
.forPath(listenerPath, StringUtils.EMPTY_BYTES);
|
||||
.forPath(listenerPath, ArrayUtils.EMPTY_BYTE_ARRAY);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
package com.zfoo.protocol.buffer;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
@@ -631,7 +632,7 @@ public abstract class ByteBufUtils {
|
||||
public static byte[] readByteArray(ByteBuf byteBuf) {
|
||||
var length = readInt(byteBuf);
|
||||
if (length == 0) {
|
||||
return StringUtils.EMPTY_BYTES;
|
||||
return ArrayUtils.EMPTY_BYTE_ARRAY;
|
||||
}
|
||||
|
||||
var bytes = new byte[length];
|
||||
|
||||
@@ -25,6 +25,17 @@ import java.util.List;
|
||||
* @version 3.0
|
||||
*/
|
||||
public abstract class ArrayUtils {
|
||||
|
||||
public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
|
||||
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
||||
public static final short[] EMPTY_SHORT_ARRAY = new short[0];
|
||||
public static final int[] EMPTY_INT_ARRAY = new int[0];
|
||||
public static final long[] EMPTY_LONG_ARRAY = new long[0];
|
||||
public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
|
||||
public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
|
||||
public static final char[] EMPTY_CHAR_ARRAY = new char[0];
|
||||
|
||||
|
||||
/**
|
||||
* length
|
||||
*/
|
||||
@@ -242,6 +253,106 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* toArray
|
||||
*/
|
||||
public static boolean[] booleanToArray(List<Boolean> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_BOOLEAN_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new boolean[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static byte[] byteToArray(List<Byte> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_BYTE_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new byte[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static short[] shortToArray(List<Short> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_SHORT_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new short[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[] intToArray(List<Integer> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_INT_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new int[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
public static long[] longToArray(List<Long> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_LONG_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new long[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static float[] floatToArray(List<Float> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_FLOAT_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new float[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static double[] doubleToArray(List<Double> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_DOUBLE_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new double[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static char[] charToArray(List<Character> list) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return EMPTY_CHAR_ARRAY;
|
||||
}
|
||||
var size = list.size();
|
||||
var array = new char[size];
|
||||
for (var i = 0; i < size; i++) {
|
||||
array[i] = list.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static <T> T[] listToArray(List<T> list, Class<T> clazz) {
|
||||
AssertionUtils.notNull(list);
|
||||
AssertionUtils.notNull(clazz);
|
||||
|
||||
@@ -30,7 +30,6 @@ import java.util.regex.Pattern;
|
||||
public abstract class StringUtils {
|
||||
|
||||
public static final String EMPTY = "";
|
||||
public static final byte[] EMPTY_BYTES = new byte[0];
|
||||
public static final String[] EMPTY_ARRAY = new String[0];
|
||||
|
||||
public static final String SPACE = " ";
|
||||
@@ -368,7 +367,7 @@ public abstract class StringUtils {
|
||||
try {
|
||||
return str.getBytes(DEFAULT_CHARSET);
|
||||
} catch (Exception e) {
|
||||
return EMPTY_BYTES;
|
||||
return ArrayUtils.EMPTY_BYTE_ARRAY;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user