use of com.github.davidmoten.rx.Functions in project risky by amsa-code.
the class Formats method transform.
public static Observable<Integer> transform(final File input, final File output, Pattern pattern, final Transformer<HasFix, HasFix> transformer, final Action2<List<HasFix>, File> fixesWriter, final Func1<String, String> renamer) {
Preconditions.checkNotNull(input);
Preconditions.checkNotNull(output);
Preconditions.checkNotNull(pattern);
Preconditions.checkNotNull(transformer);
final List<File> files = Files.find(input, pattern);
long n = 0;
for (File file : files) n += file.length();
final long totalSizeBytes = n;
log.info("transforming " + new DecimalFormat("0.000").format(totalSizeBytes / 1000000.0) + "MB");
final Action1<File> logger = new Action1<File>() {
final AtomicInteger count = new AtomicInteger();
final long startTime = System.currentTimeMillis();
final AtomicLong size = new AtomicLong();
@Override
public void call(File f) {
long t = System.currentTimeMillis();
int n = count.incrementAndGet();
long bytes = size.getAndAdd(f.length());
double timeToFinishMins;
if (n > 1) {
timeToFinishMins = (t - startTime) / (double) (bytes) * (totalSizeBytes - bytes) / 1000.0 / 60.0;
} else
timeToFinishMins = -1;
DecimalFormat df = new DecimalFormat("0.000");
log.info("transforming " + n + " of " + files.size() + ":" + f + ", sizeMB=" + df.format(f.length() / 1000000.0) + ", finish in mins=" + df.format(timeToFinishMins));
}
};
log.info("converting " + files.size() + " files" + " in " + input);
return Observable.from(files).flatMap(file -> {
final File outputFile = rebase(file, input, output);
outputFile.getParentFile().mkdirs();
logger.call(file);
return BinaryFixes.from(file, true, BinaryFixesFormat.WITHOUT_MMSI).toList().flatMapIterable(Functions.<List<Fix>>identity()).compose(transformer).toList().doOnNext(list -> {
File f = new File(outputFile.getParentFile(), renamer.call(outputFile.getName()));
fixesWriter.call(list, f);
}).count();
});
}
Aggregations