Skip to content

Commit 9e657b1

Browse files
committed
feat: add test code
1 parent 8236400 commit 9e657b1

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.collagid;
2+
3+
import java.util.*;
4+
5+
public class RowChunk {
6+
private List<String> chunks = new ArrayList<>();
7+
private Map<String, List<String>> chunkMap = new HashMap<>();
8+
private int totalCount = 0;
9+
10+
public void print() {
11+
for (String chunk : chunks) {
12+
System.out.println(chunk + ":");
13+
List<String> strings = chunkMap.get(chunk);
14+
for (String string : strings) {
15+
System.out.println(" " + string);
16+
}
17+
}
18+
}
19+
20+
public void addRow(String rowId, int index) {
21+
if (index > totalCount) {
22+
index = totalCount;
23+
}
24+
Map.Entry<String, Integer> chunkKeyByOrder = getChunkKeyByOrder(index);
25+
String chunkKey = chunkKeyByOrder.getKey();
26+
int o = chunkKeyByOrder.getValue();
27+
List<String> strings = chunkMap.get(chunkKey);
28+
strings.add(o, rowId);
29+
if (strings.size() >= 120) {
30+
List<String> right = new ArrayList<>(strings.subList(60, strings.size()));
31+
strings.subList(60, strings.size()).clear(); // Clear the sublist in-place
32+
String random = random();
33+
chunkMap.put(random, right);
34+
chunks.add(chunks.indexOf(chunkKey) + 1, random);
35+
}
36+
totalCount += 1;
37+
}
38+
39+
public Map.Entry<String, Integer> getChunkKeyByOrder(int index) {
40+
if (chunks.isEmpty()) {
41+
String random = random();
42+
chunks.add(random);
43+
chunkMap.put(random, new ArrayList<>());
44+
return new AbstractMap.SimpleEntry<>(random, 0);
45+
}
46+
int size = 0;
47+
for (String chunk : chunks) {
48+
List<String> strings = chunkMap.get(chunk);
49+
size += strings.size();
50+
if (index <= size) {
51+
return new AbstractMap.SimpleEntry<>(chunk, index - (size - strings.size()));
52+
}
53+
}
54+
return new AbstractMap.SimpleEntry<>(chunks.get(chunks.size() - 1), index - size);
55+
}
56+
57+
private String random() {
58+
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
59+
Random random = new Random();
60+
StringBuilder sb = new StringBuilder();
61+
for (int i = 0; i < 8; i++) {
62+
int index = random.nextInt(characters.length());
63+
char randomChar = characters.charAt(index);
64+
sb.append(randomChar);
65+
}
66+
return sb.toString();
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.collagid;
2+
3+
import java.util.Comparator;
4+
import java.util.TreeSet;
5+
6+
public class RowForSort implements Comparator<RowForSort> {
7+
int val;
8+
9+
public RowForSort(int i) {
10+
this.val = i;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return "" + val;
16+
}
17+
18+
@Override
19+
public int compareTo(RowForSort o) {
20+
if (val == o.val) {
21+
return 0;
22+
}else if (val > o.val) {
23+
return -1;
24+
} else {
25+
return 1;
26+
}
27+
}
28+
29+
public static void main(String[] args) {
30+
TreeSet<RowForSort> set = new TreeSet<>();
31+
set.add(new RowForSort(10));
32+
set.add(new RowForSort(3));
33+
set.add(new RowForSort(5));
34+
set.add(new RowForSort(6));
35+
set.add(new RowForSort(49));
36+
set.forEach(System.out::println);
37+
}
38+
}

0 commit comments

Comments
 (0)