ref[move]: move RandomUtils

This commit is contained in:
godotg
2023-09-02 19:59:45 +08:00
parent 4d015a1fb8
commit 9f6f154e76
9 changed files with 68 additions and 88 deletions
@@ -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;
@@ -13,7 +13,7 @@
package com.zfoo.event.model.event;
import com.zfoo.util.math.RandomUtils;
import com.zfoo.protocol.util.RandomUtils;
/**
* @author godotg
@@ -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;
/**
* 随机负载均衡器,任选服务提供者的其中之一
@@ -12,8 +12,6 @@
package com.zfoo.net.router.attachment;
import com.zfoo.util.math.RandomUtils;
/**
* @author godotg
* @version 3.0
@@ -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;
@@ -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;
+10
View File
@@ -60,6 +60,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
<exclusion>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
@@ -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<T> {
private int cursor = 0;
private final TreeMap<Integer, T> 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<T> select(int count) {
List<T> resultList = new ArrayList<>();
for (int i = 0; i < count; i++) {
resultList.add(select());
}
return resultList;
}
public Collection<T> getAll() {
return elementMap.values();
}
}
}
@@ -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<T> {
private int cursor = 0;
private final TreeMap<Integer, T> 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<T> select(int count) {
List<T> resultList = new ArrayList<>();
for (int i = 0; i < count; i++) {
resultList.add(select());
}
return resultList;
}
public Collection<T> getAll() {
return elementMap.values();
}
}