From 9f6f154e768bc2217da37461e8ab592706196347 Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 2 Sep 2023 19:59:45 +0800 Subject: [PATCH] ref[move]: move RandomUtils --- .../java/com/zfoo/event/manager/EventBus.java | 3 +- .../com/zfoo/event/model/event/IEvent.java | 2 +- .../balancer/RandomConsumerLoadBalancer.java | 2 +- .../net/router/attachment/UdpAttachment.java | 2 - .../main/java/com/zfoo/net/task/TaskBus.java | 3 +- .../java/com/zfoo/orm/manager/OrmManager.java | 2 +- protocol/pom.xml | 10 +++ .../com/zfoo/protocol/util}/RandomUtils.java | 58 +++++++++++++-- .../com/zfoo/util/math/RandomSelector.java | 74 ------------------- 9 files changed, 68 insertions(+), 88 deletions(-) rename {util/src/main/java/com/zfoo/util/math => protocol/src/main/java/com/zfoo/protocol/util}/RandomUtils.java (88%) delete mode 100644 util/src/main/java/com/zfoo/util/math/RandomSelector.java diff --git a/event/src/main/java/com/zfoo/event/manager/EventBus.java b/event/src/main/java/com/zfoo/event/manager/EventBus.java index f000ea86..cd88635e 100644 --- a/event/src/main/java/com/zfoo/event/manager/EventBus.java +++ b/event/src/main/java/com/zfoo/event/manager/EventBus.java @@ -19,8 +19,7 @@ import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject; import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.StringUtils; import com.zfoo.util.SafeRunnable; -import com.zfoo.util.ThreadUtils; -import com.zfoo.util.math.RandomUtils; +import com.zfoo.protocol.util.RandomUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/event/src/main/java/com/zfoo/event/model/event/IEvent.java b/event/src/main/java/com/zfoo/event/model/event/IEvent.java index 9fc252bb..eb9b33ba 100644 --- a/event/src/main/java/com/zfoo/event/model/event/IEvent.java +++ b/event/src/main/java/com/zfoo/event/model/event/IEvent.java @@ -13,7 +13,7 @@ package com.zfoo.event.model.event; -import com.zfoo.util.math.RandomUtils; +import com.zfoo.protocol.util.RandomUtils; /** * @author godotg diff --git a/net/src/main/java/com/zfoo/net/consumer/balancer/RandomConsumerLoadBalancer.java b/net/src/main/java/com/zfoo/net/consumer/balancer/RandomConsumerLoadBalancer.java index 13c5cab7..b01dfd65 100644 --- a/net/src/main/java/com/zfoo/net/consumer/balancer/RandomConsumerLoadBalancer.java +++ b/net/src/main/java/com/zfoo/net/consumer/balancer/RandomConsumerLoadBalancer.java @@ -17,7 +17,7 @@ import com.zfoo.net.session.Session; import com.zfoo.protocol.IPacket; import com.zfoo.protocol.ProtocolManager; import com.zfoo.protocol.exception.RunException; -import com.zfoo.util.math.RandomUtils; +import com.zfoo.protocol.util.RandomUtils; /** * 随机负载均衡器,任选服务提供者的其中之一 diff --git a/net/src/main/java/com/zfoo/net/router/attachment/UdpAttachment.java b/net/src/main/java/com/zfoo/net/router/attachment/UdpAttachment.java index 0606b182..ed23725f 100644 --- a/net/src/main/java/com/zfoo/net/router/attachment/UdpAttachment.java +++ b/net/src/main/java/com/zfoo/net/router/attachment/UdpAttachment.java @@ -12,8 +12,6 @@ package com.zfoo.net.router.attachment; -import com.zfoo.util.math.RandomUtils; - /** * @author godotg * @version 3.0 diff --git a/net/src/main/java/com/zfoo/net/task/TaskBus.java b/net/src/main/java/com/zfoo/net/task/TaskBus.java index 129df285..d5772b4f 100644 --- a/net/src/main/java/com/zfoo/net/task/TaskBus.java +++ b/net/src/main/java/com/zfoo/net/task/TaskBus.java @@ -25,8 +25,7 @@ import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.StringUtils; import com.zfoo.scheduler.manager.SchedulerBus; import com.zfoo.util.SafeRunnable; -import com.zfoo.util.ThreadUtils; -import com.zfoo.util.math.RandomUtils; +import com.zfoo.protocol.util.RandomUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java index 225a30fd..d328d250 100644 --- a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java +++ b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java @@ -35,7 +35,7 @@ import com.zfoo.protocol.collection.ArrayUtils; import com.zfoo.protocol.collection.CollectionUtils; import com.zfoo.protocol.exception.RunException; import com.zfoo.protocol.util.*; -import com.zfoo.util.math.RandomUtils; +import com.zfoo.protocol.util.RandomUtils; import com.zfoo.util.net.HostAndPort; import org.bson.Document; import org.bson.codecs.configuration.CodecRegistries; diff --git a/protocol/pom.xml b/protocol/pom.xml index d73228eb..26771f22 100644 --- a/protocol/pom.xml +++ b/protocol/pom.xml @@ -60,6 +60,16 @@ org.springframework.boot spring-boot-starter-json ${spring.boot.version} + + + spring-boot-starter + org.springframework.boot + + + spring-web + org.springframework + + diff --git a/util/src/main/java/com/zfoo/util/math/RandomUtils.java b/protocol/src/main/java/com/zfoo/protocol/util/RandomUtils.java similarity index 88% rename from util/src/main/java/com/zfoo/util/math/RandomUtils.java rename to protocol/src/main/java/com/zfoo/protocol/util/RandomUtils.java index ceb0d8b4..2994e7de 100644 --- a/util/src/main/java/com/zfoo/util/math/RandomUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/util/RandomUtils.java @@ -1,6 +1,5 @@ /* * Copyright (C) 2020 The zfoo Authors - * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * @@ -11,10 +10,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.util.math; - -import com.zfoo.protocol.util.NumberUtils; -import com.zfoo.protocol.util.StringUtils; +package com.zfoo.protocol.util; import java.math.BigDecimal; import java.math.RoundingMode; @@ -408,4 +404,56 @@ public abstract class RandomUtils { return baseString.charAt(getRandom().nextInt(baseString.length())); } + /** + * @author godotg + * @version 3.0 + */ + public static class RandomSelector { + + private int cursor = 0; + + private final TreeMap elementMap = new TreeMap<>(); + + public void addElement(T value, int weight) { + if (value == null || weight <= 0) { + return; + } + + cursor += weight; + elementMap.put(cursor, value); + } + + public void clear() { + elementMap.clear(); + cursor = 0; + } + + public int size() { + return elementMap.size(); + } + + public T select() { + if (cursor <= 0) { + throw new IllegalStateException("all weights are 0"); + } + if (elementMap.isEmpty()) { + throw new IllegalStateException("selected element is empty, please insert some elements"); + } + + var randomInt = randomInt(cursor) + 1; + return elementMap.ceilingEntry(randomInt).getValue(); + } + + public List select(int count) { + List resultList = new ArrayList<>(); + for (int i = 0; i < count; i++) { + resultList.add(select()); + } + return resultList; + } + + public Collection getAll() { + return elementMap.values(); + } + } } diff --git a/util/src/main/java/com/zfoo/util/math/RandomSelector.java b/util/src/main/java/com/zfoo/util/math/RandomSelector.java deleted file mode 100644 index ffe7a506..00000000 --- a/util/src/main/java/com/zfoo/util/math/RandomSelector.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and limitations under the License. - */ - -package com.zfoo.util.math; - -import org.springframework.lang.Nullable; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.TreeMap; - -/** - * @author godotg - * @version 3.0 - */ -public class RandomSelector { - - private int cursor = 0; - - private final TreeMap elementMap = new TreeMap<>(); - - public void addElement(@Nullable T value, int weight) { - if (value == null || weight <= 0) { - return; - } - - cursor += weight; - elementMap.put(cursor, value); - } - - public void clear() { - elementMap.clear(); - cursor = 0; - } - - public int size() { - return elementMap.size(); - } - - public T select() { - if (cursor <= 0) { - throw new IllegalStateException("全部的权重是0"); - } - if (elementMap.isEmpty()) { - throw new IllegalStateException("选择的元素为空"); - } - - var randomInt = RandomUtils.randomInt(cursor) + 1; - return elementMap.ceilingEntry(randomInt).getValue(); - } - - public List select(int count) { - List resultList = new ArrayList<>(); - for (int i = 0; i < count; i++) { - resultList.add(select()); - } - return resultList; - } - - public Collection getAll() { - return elementMap.values(); - } -}