mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
修复 AbstractFilter 的 init 方法在 maxValue 小于 machineNum 时导致数组越界异常
This commit is contained in:
@@ -48,12 +48,13 @@ public abstract class AbstractFilter implements BloomFilter {
|
|||||||
*/
|
*/
|
||||||
public void init(long maxValue, int machineNum) {
|
public void init(long maxValue, int machineNum) {
|
||||||
this.size = Assert.checkBetween(maxValue, 1, Integer.MAX_VALUE);
|
this.size = Assert.checkBetween(maxValue, 1, Integer.MAX_VALUE);
|
||||||
|
final int capacity = (int) ((this.size + machineNum - 1) / machineNum);
|
||||||
switch (machineNum) {
|
switch (machineNum) {
|
||||||
case BitMap.MACHINE32:
|
case BitMap.MACHINE32:
|
||||||
bm = new IntMap((int) (size / machineNum));
|
bm = new IntMap(capacity);
|
||||||
break;
|
break;
|
||||||
case BitMap.MACHINE64:
|
case BitMap.MACHINE64:
|
||||||
bm = new LongMap((int) (size / machineNum));
|
bm = new LongMap(capacity);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("Error Machine number!");
|
throw new RuntimeException("Error Machine number!");
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package cn.hutool.bloomfilter;
|
||||||
|
|
||||||
|
import cn.hutool.bloomfilter.bitMap.BitMap;
|
||||||
|
import cn.hutool.bloomfilter.filter.DefaultFilter;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
public class AbstractFilterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testInitWhenMaxValueLessThanMachineNum() {
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
DefaultFilter filter = new DefaultFilter(1, BitMap.MACHINE32);
|
||||||
|
filter.add("init");
|
||||||
|
}, "maxValue=1且machineNum=32时add应无异常");
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
DefaultFilter filter = new DefaultFilter(31, BitMap.MACHINE32);
|
||||||
|
filter.add("init");
|
||||||
|
}, "maxValue=31且machineNum=32时add应无异常");
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
DefaultFilter filter = new DefaultFilter(1, BitMap.MACHINE64);
|
||||||
|
filter.add("init");
|
||||||
|
}, "maxValue=1且machineNum=64时add应无异常");
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
DefaultFilter filter = new DefaultFilter(63, BitMap.MACHINE64);
|
||||||
|
filter.add("init");
|
||||||
|
}, "maxValue=63且machineNum=64时add应无异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user