clean old exportToCSV method

use try-with-resources statement
This commit is contained in:
Vebryn 2017-09-17 22:54:37 +02:00
parent 5525938669
commit a867c06507
4 changed files with 6 additions and 56 deletions

View File

@ -22,10 +22,6 @@ import android.support.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -48,7 +44,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
* @param <T> the sample type * @param <T> the sample type
*/ */
public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> { public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSampleProvider.class);
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0]; private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
private final DaoSession mSession; private final DaoSession mSession;
private final GBDevice mDevice; private final GBDevice mDevice;
@ -99,29 +94,6 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
getSampleDao().insertOrReplaceInTx(activitySamples); getSampleDao().insertOrReplaceInTx(activitySamples);
} }
@Override
public void exportToCSV(T[] activitySamples, File outFile) {
String separator = ",";
LOG.info("Exporting samples into csv file: " + outFile.getName());
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
for (T sample : activitySamples){
String line = sample.getTimestamp() + separator + sample.getDeviceId() + separator + sample.getUserId() + separator + sample.getRawIntensity() + separator + sample.getSteps() + separator + sample.getRawKind() + separator + sample.getHeartRate();
//LOG.debug("Adding line into buffer: " + line);
bw.write(line);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e) {
LOG.error(e.getMessage());
}
}
@Nullable @Nullable
@Override @Override
public T getLatestActivitySample() { public T getLatestActivitySample() {

View File

@ -20,9 +20,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import java.io.File;
import java.util.List; import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
/** /**
@ -89,12 +87,6 @@ public interface SampleProvider<T extends AbstractActivitySample> {
*/ */
void addGBActivitySamples(T[] activitySamples); void addGBActivitySamples(T[] activitySamples);
/**
* Exports samples into a CSV file
* @param activitySamples the samples to export
*/
void exportToCSV(T[] activitySamples, File outFile);
/** /**
* Factory method to creates an empty sample of the correct type for this sample provider * Factory method to creates an empty sample of the correct type for this sample provider
* @return the newly created "empty" sample * @return the newly created "empty" sample

View File

@ -23,7 +23,6 @@ import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import java.io.File;
import java.util.List; import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.GBException;
@ -70,10 +69,6 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
return null; return null;
} }
@Override
public void exportToCSV(AbstractActivitySample[] activitySamples, File outFile) {
}
@Override @Override
public void addGBActivitySample(AbstractActivitySample activitySample) { public void addGBActivitySample(AbstractActivitySample activitySample) {
} }

View File

@ -8,7 +8,7 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
/** /**
@ -16,15 +16,13 @@ import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
*/ */
public class CSVExport { public class CSVExport {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSampleProvider.class); private static final Logger LOG = LoggerFactory.getLogger(SampleProvider.class);
public static void exportToCSV(AbstractActivitySample[] activitySamples, File outFile) { public static void exportToCSV(AbstractActivitySample[] activitySamples, File outFile) {
String separator = ","; String separator = ",";
BufferedWriter bw = null;
LOG.info("Exporting samples into csv file: " + outFile.getName()); LOG.info("Exporting samples into csv file: " + outFile.getName());
try { try (BufferedWriter bw = new BufferedWriter(new FileWriter(outFile))) {
bw = new BufferedWriter(new FileWriter(outFile));
bw.write("TIMESTAMP" + separator + "DEVICE_ID" + separator + "USER_ID" + separator + "RAW_INTENSITY" + separator + "STEPS" + separator + "RAW_KIND" + separator + "HEART_RATE"); bw.write("TIMESTAMP" + separator + "DEVICE_ID" + separator + "USER_ID" + separator + "RAW_INTENSITY" + separator + "STEPS" + separator + "RAW_KIND" + separator + "HEART_RATE");
bw.newLine(); bw.newLine();
@ -35,17 +33,10 @@ public class CSVExport {
bw.write(line); bw.write(line);
bw.newLine(); bw.newLine();
} }
bw.flush();
} catch (IOException e) { } catch (IOException e) {
LOG.error(e.getMessage()); LOG.error("Error related to " + outFile.getName() + " file: " + e.getMessage(), e);
} finally {
if (bw != null){
try {
bw.flush();
bw.close();
} catch (IOException e) {
LOG.error(e.getMessage());
}
}
} }
} }
} }