Ajout dans StringUtil et RandomUtil

This commit is contained in:
Marc Baloup 2018-07-21 17:57:44 +02:00
parent 241671c0aa
commit 4f1446e3a5
No known key found for this signature in database
GPG Key ID: 3A4F019B6C600FDE
3 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,121 @@
package fr.pandacube.java.external_tools;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class Main {
static class Row {
int rowid;
String date, user, uuid;
int wid, x, y, z, type, amount, action;
boolean rolled_back;
}
static class Chest {
final int wid, x, y, z, hashCode;
List<Row> sources = new ArrayList<>();
int actualCount = 0;
boolean alreadyRolledBack = false;
public Chest(int w, int wx, int wy, int wz) {
wid = w;
x = wx;
y = wy;
z = wz;
hashCode = Objects.hash(wid, x, y, z);
}
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof Chest))
return false;
Chest c = (Chest) o;
return wid == c.wid && x == c.x && y == c.y && z == c.z;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public String toString() {
return "world=" + wid + " - /tppos " + x + " " + y + " " + z + " - rolledBack=" + alreadyRolledBack + " - amount=" + actualCount;
}
public String toCSVString() {
return wid + "," + x + "," + y + "," + z + "," + alreadyRolledBack + "," + actualCount;
}
}
public static void main(String args[]) throws Exception {
/*CSVParser parser = new CSVParser(new FileReader("co_survie_container.csv"), CSVFormat.DEFAULT);
List<Row> rows = StreamSupport.stream(parser.spliterator(), false).map(r -> {
Row ret = new Row();
ret.rowid = Integer.parseInt(r.get(0));
ret.date = r.get(1);
ret.user = r.get(2);
ret.uuid = r.get(3);
ret.wid = Integer.parseInt(r.get(4));
ret.x = Integer.parseInt(r.get(5));
ret.y = Integer.parseInt(r.get(6));
ret.z = Integer.parseInt(r.get(7));
ret.type = Integer.parseInt(r.get(8));
ret.amount = Integer.parseInt(r.get(9));
ret.action = Integer.parseInt(r.get(10));
ret.rolled_back = r.get(11).equals("1");
return ret;
}).collect(Collectors.toList());
rows.sort(Comparator.comparingInt(r -> r.rowid));
List<Chest> chests = new ArrayList<>();
for (Row row : rows) {
Chest c = new Chest(row.wid, row.x, row.y, row.z);
int idx = chests.indexOf(c);
c = (idx >= 0) ? chests.get(idx) : c;
if (idx < 0) {
chests.add(c);
idx = chests.size() - 1;
}
c.sources.add(row);
if (row.rolled_back)
c.alreadyRolledBack = true;
if (!c.alreadyRolledBack) {
c.actualCount += (row.action == 1) ? row.amount : -row.amount;
if (c.actualCount == 0) {
chests.remove(idx);
}
}
}
chests.sort(Comparator.comparingInt(c -> c.actualCount));
chests.forEach(c -> System.out.println(c.toCSVString()));
*/
}
}

View File

@ -1,5 +1,6 @@
package fr.pandacube.java.util; package fr.pandacube.java.util;
import java.util.List;
import java.util.Random; import java.util.Random;
public class RandomUtil { public class RandomUtil {
@ -14,4 +15,12 @@ public class RandomUtil {
return rand.nextDouble() * (maxExclu - minInclu) + minInclu; return rand.nextDouble() * (maxExclu - minInclu) + minInclu;
} }
public static <T> T arrayElement(T[] arr) {
return arr[rand.nextInt(arr.length)];
}
public static <T> T listElement(List<T> arr) {
return arr.get(rand.nextInt(arr.size()));
}
} }

View File

@ -1,5 +1,7 @@
package fr.pandacube.java.util; package fr.pandacube.java.util;
import java.util.List;
public class StringUtil { public class StringUtil {
public static String formatDouble(double d) { public static String formatDouble(double d) {
if (d == (long) d) if (d == (long) d)
@ -19,4 +21,12 @@ public class StringUtil {
if (c == c_match) count++; if (c == c_match) count++;
return count; return count;
} }
public static String joinGrammatically(CharSequence sep1, CharSequence sepFinal, List<String> strings) {
int size = strings == null ? 0 : strings.size();
return size == 0 ? "" : size == 1 ? strings.get(0) : String.join(sep1, strings.subList(0, --size)) + sepFinal + strings.get(size);
}
} }