Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-belellou committed Nov 17, 2024
1 parent 869c992 commit a4a3dd0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public abstract class ChFile {
protected Double yOffset;
protected String detector;

int dataStart;
int startTimePosition;
int endTimePosition;
int unitsPosition;
int yOffsetPosition;
int yScalingPosition;
int detectorPosition;
protected final int dataStart; // Has no use for now
protected final int startTimePosition;
protected final int endTimePosition;
protected final int unitsPosition;
protected final int yOffsetPosition;
protected final int yScalingPosition;
protected final int detectorPosition;

protected ChFile(RandomAccessFile input, int dataStart, int startTimePosition, int endTimePosition, int unitsPosition, int yOffsetPosition, int yScalingPosition, int detectorPosition) throws IOException {
this.dataStart = dataStart;
Expand All @@ -46,59 +46,46 @@ protected ChFile(RandomAccessFile input, int dataStart, int startTimePosition, i

protected abstract void parseData(RandomAccessFile input) throws IOException;

/**
* Returns the values found in the .ch file, converted to picoampere as the standard imposes.
*/
public List<Double> getValues() {
return values;
}

protected void setValues(List<Double> values) {
this.values = values;
}

public Float getStartTime() {
return startTime;
}

private void setStartTime(Float startTime) {
this.startTime = startTime;
}

public Float getEndTime() {
return endTime;
}

private void setEndTime(Float endTime) {
this.endTime = endTime;
public String getDetector() {
return detector;
}

/**
* Returns the unit found in the .ch file.<br>
* Warning: the values stored in this class are converted to picoampere, as the standard imposes.
*/
protected Unit<ElectricCurrent> getUnit() {
return unit;
}

public String getUnitSymbol() {
return unit.toString();
}

private void setUnit(String unit) {
Unit<? extends Quantity> localUnit = Unit.valueOf(unit);

if (!PICO_AMPERE_UNIT.isCompatible(localUnit)) {
throw new IllegalArgumentException("Unsupported unit: " + localUnit);
throw new IllegalArgumentException("Unsupported unit: " + unit);
}

this.unit = localUnit.asType(ElectricCurrent.class);
}

public String getDetector() {
return detector;
}

private void setDetector(String detector) {
this.detector = detector;
}

protected void readMetadata(RandomAccessFile input) throws IOException {
setStartTime(readMetadataTime(input, startTimePosition));
setEndTime(readMetadataTime(input, endTimePosition));
startTime = readMetadataTime(input, startTimePosition);
endTime = readMetadataTime(input, endTimePosition);
setUnit(readStringAtPosition(input, unitsPosition, true));

input.seek(yOffsetPosition);
Expand All @@ -107,6 +94,6 @@ protected void readMetadata(RandomAccessFile input) throws IOException {
input.seek(yScalingPosition);
yScaling = input.readDouble();

setDetector(readStringAtPosition(input, detectorPosition, true));
detector = readStringAtPosition(input, detectorPosition, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import static fr.ifpen.allotropeconverters.gc.chemstation.chfile.ReadHelpers.readLittleEndianDouble;

Expand All @@ -29,15 +28,13 @@ protected void parseData(RandomAccessFile input) throws IOException {
throw new IllegalArgumentException("Input too large to parse");
}

List<Double> values = new ArrayList<>((int) numberOfPoints);
UnitConverter unitConverter = getUnit().getConverterTo(PICO_AMPERE_UNIT);
values = new ArrayList<>((int) numberOfPoints);
UnitConverter unitConverter = unit.getConverterTo(PICO_AMPERE_UNIT);

input.seek(DATA_START);

for (int i = 0; i < numberOfPoints; i++) {
values.add(unitConverter.convert(readLittleEndianDouble(input) * yScaling));
}

setValues(values);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.ifpen.allotropeconverters.gc.chemstation.chfile;

import javax.measure.converter.UnitConverter;
import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
Expand All @@ -26,6 +27,8 @@ protected void parseData(RandomAccessFile input) throws IOException {
values = new ArrayList<>();
long[] buffer = new long[]{0, 0, 0};

UnitConverter unitConverter = unit.getConverterTo(PICO_AMPERE_UNIT);

boolean endOfFile = false;

while (!endOfFile) {
Expand All @@ -41,8 +44,7 @@ protected void parseData(RandomAccessFile input) throws IOException {
buffer[1] = 0;
}

values.add(buffer[0] * yScaling + yOffset);

values.add(unitConverter.convert(buffer[0] * yScaling + yOffset));
} catch (EOFException e) {
endOfFile = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ void getVersionReturnsExpected() throws IOException {
Assertions.assertEquals(2.165234, values.get(0), 0.001);

Assertions.assertEquals(SI.PICO(SI.AMPERE), chFile.getUnit());
Assertions.assertEquals("pA", chFile.getUnitSymbol());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ void getVersionReturnsExpected() throws IOException {
Assertions.assertEquals(2.1010, values.get(0), 0.001);

Assertions.assertEquals(SI.PICO(SI.AMPERE), chFile.getUnit());
Assertions.assertEquals("pA", chFile.getUnitSymbol());
}
}

0 comments on commit a4a3dd0

Please sign in to comment.