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
+ }
0 commit comments