Modernise Java codebase (Java 8 features)

This commit is contained in:
Simon Legner 2021-01-03 23:39:01 +01:00
parent 22092218af
commit b787a1fdaa
4 changed files with 13 additions and 22 deletions

View File

@ -38,7 +38,7 @@ public abstract class BinaryParser implements BlockReaderAdapter {
/** Take a Info protocol buffer containing a date and convert it into a java Date object */ /** Take a Info protocol buffer containing a date and convert it into a java Date object */
protected Date getDate(Osmformat.Info info) { protected Date getDate(Osmformat.Info info) {
if (info.hasTimestamp()) { if (info.hasTimestamp()) {
return new Date(date_granularity * (long) info.getTimestamp()); return new Date(date_granularity * info.getTimestamp());
} else } else
return NODATE; return NODATE;
} }

View File

@ -82,8 +82,8 @@ public class BinarySerializer implements Closeable, Flushable {
/** How many primitives have been seen in this batch */ /** How many primitives have been seen in this batch */
protected int batch_size = 0; protected int batch_size = 0;
protected int total_entities = 0; protected int total_entities = 0;
private StringTable stringtable = new StringTable(); private final StringTable stringtable = new StringTable();
protected List<PrimGroupWriterInterface> groups = new ArrayList<PrimGroupWriterInterface>(); protected List<PrimGroupWriterInterface> groups = new ArrayList<>();
protected BlockOutputStream output; protected BlockOutputStream output;
public BinarySerializer(BlockOutputStream output) { public BinarySerializer(BlockOutputStream output) {

View File

@ -37,11 +37,7 @@ public class StringTable {
private String set[]; private String set[];
public void incr(String s) { public void incr(String s) {
if (counts.containsKey(s)) { counts.merge(s, 1, Integer::sum);
counts.put(s, new Integer(counts.get(s).intValue() + 1));
} else {
counts.put(s, new Integer(1));
}
} }
/** After the stringtable has been built, return the offset of a string in it. /** After the stringtable has been built, return the offset of a string in it.
@ -51,17 +47,11 @@ public class StringTable {
* @return * @return
*/ */
public int getIndex(String s) { public int getIndex(String s) {
return stringmap.get(s).intValue(); return stringmap.get(s);
} }
public void finish() { public void finish() {
Comparator<String> comparator = new Comparator<String>() { Comparator<String> comparator = (s1, s2) -> counts.get(s2) - counts.get(s1);
@Override
public int compare(final String s1, String s2) {
int diff = counts.get(s2) - counts.get(s1);
return diff;
}
};
/* Sort the stringtable */ /* Sort the stringtable */
@ -114,15 +104,15 @@ public class StringTable {
Arrays.sort(set, Math.min(1 << 14, set.length-1), Math.min(1 << 21, Arrays.sort(set, Math.min(1 << 14, set.length-1), Math.min(1 << 21,
set.length-1), comparator); set.length-1), comparator);
} }
stringmap = new HashMap<String, Integer>(2 * set.length); stringmap = new HashMap<>(2 * set.length);
for (int i = 0; i < set.length; i++) { for (int i = 0; i < set.length; i++) {
stringmap.put(set[i], new Integer(i+1)); // Index 0 is reserved for use as a delimiter. stringmap.put(set[i], i + 1); // Index 0 is reserved for use as a delimiter.
} }
counts = null; counts = null;
} }
public void clear() { public void clear() {
counts = new HashMap<String, Integer>(100); counts = new HashMap<>(100);
stringmap = null; stringmap = null;
set = null; set = null;
} }
@ -131,8 +121,9 @@ public class StringTable {
Osmformat.StringTable.Builder builder = Osmformat.StringTable Osmformat.StringTable.Builder builder = Osmformat.StringTable
.newBuilder(); .newBuilder();
builder.addS(ByteString.copyFromUtf8("")); // Add a unused string at offset 0 which is used as a delimiter. builder.addS(ByteString.copyFromUtf8("")); // Add a unused string at offset 0 which is used as a delimiter.
for (int i = 0; i < set.length; i++) for (String s : set) {
builder.addS(ByteString.copyFromUtf8(set[i])); builder.addS(ByteString.copyFromUtf8(s));
}
return builder; return builder;
} }
} }

View File

@ -69,6 +69,6 @@ public class BlockOutputStream {
} }
OutputStream outwrite; OutputStream outwrite;
List<FileBlockPosition> writtenblocks = new ArrayList<FileBlockPosition>(); List<FileBlockPosition> writtenblocks = new ArrayList<>();
CompressFlags compression; CompressFlags compression;
} }