mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
improve V1 parsing
This commit is contained in:
@@ -85,4 +85,11 @@ public class AbstractMeasure<T extends Enum<? extends Unit>> implements Serializ
|
||||
public String toString() {
|
||||
return format.format(value) + unit;
|
||||
}
|
||||
|
||||
protected static double parse(String value) {
|
||||
if (value.startsWith("."))
|
||||
value = "0" + value;
|
||||
value = value.replace(",", ".").replace("*", "");
|
||||
return Double.parseDouble(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,11 @@ public class Capacitance extends AbstractMeasure<CapacitanceUnit> {
|
||||
}
|
||||
|
||||
public static Capacitance parseCapacitance(String value) {
|
||||
value = value.replace("*", "");
|
||||
for (CapacitanceUnit unit : CapacitanceUnit.values()) {
|
||||
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
|
||||
value = value.substring(0, value.length() - unit.toString().length() - 1).trim();
|
||||
return new Capacitance(Double.parseDouble(value), unit);
|
||||
value = value.substring(0, value.length() - unit.toString().length()).trim();
|
||||
return new Capacitance(parse(value), unit);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Could not parse capacitance: " + value);
|
||||
|
||||
@@ -20,10 +20,11 @@ public class Inductance extends AbstractMeasure<InductanceUnit> {
|
||||
}
|
||||
|
||||
public static Inductance parseCapacitance(String value) {
|
||||
value = value.replace("*", "");
|
||||
for (InductanceUnit unit : InductanceUnit.values()) {
|
||||
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
|
||||
value = value.substring(0, value.length() - unit.toString().length() - 1).trim();
|
||||
return new Inductance(Double.parseDouble(value), unit);
|
||||
value = value.substring(0, value.length() - unit.toString().length()).trim();
|
||||
return new Inductance(parse(value), unit);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Could not parse inductance: " + value);
|
||||
|
||||
@@ -20,10 +20,11 @@ public class Power extends AbstractMeasure<PowerUnit> {
|
||||
}
|
||||
|
||||
public static Power parseCapacitance(String value) {
|
||||
value = value.replace("*", "");
|
||||
for (PowerUnit unit : PowerUnit.values()) {
|
||||
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
|
||||
value = value.substring(0, value.length() - unit.toString().length() - 1).trim();
|
||||
return new Power(Double.parseDouble(value), unit);
|
||||
value = value.substring(0, value.length() - unit.toString().length()).trim();
|
||||
return new Power(parse(value), unit);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Could not parse power: " + value);
|
||||
|
||||
@@ -88,10 +88,11 @@ public class Resistance extends AbstractMeasure<ResistanceUnit> {
|
||||
}
|
||||
|
||||
public static Resistance parseResistance(String value) {
|
||||
value = value.replace("*", "").replace("R", "\u2126");
|
||||
for (ResistanceUnit unit : ResistanceUnit.values()) {
|
||||
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
|
||||
value = value.substring(0, value.length() - unit.toString().length()).trim();
|
||||
return new Resistance(Double.parseDouble(value), unit);
|
||||
return new Resistance(parse(value), unit);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Could not parse resistance: " + value);
|
||||
|
||||
@@ -31,10 +31,11 @@ public class Size extends AbstractMeasure<SizeUnit> implements Comparable<Size>
|
||||
}
|
||||
|
||||
public static Size parseSize(String value) {
|
||||
value = value.replace("*", "");
|
||||
for (SizeUnit unit : SizeUnit.values()) {
|
||||
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
|
||||
value = value.substring(0, value.length() - unit.toString().length() - 1).trim();
|
||||
return new Size(Double.parseDouble(value), unit);
|
||||
value = value.substring(0, value.length() - unit.toString().length()).trim();
|
||||
return new Size(parse(value), unit);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Could not parse size: " + value);
|
||||
|
||||
@@ -20,10 +20,11 @@ public class Voltage extends AbstractMeasure<VoltageUnit> {
|
||||
}
|
||||
|
||||
public static Voltage parseCapacitance(String value) {
|
||||
value = value.replace("*", "");
|
||||
for (VoltageUnit unit : VoltageUnit.values()) {
|
||||
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
|
||||
value = value.substring(0, value.length() - unit.toString().length() - 1).trim();
|
||||
return new Voltage(Double.parseDouble(value), unit);
|
||||
value = value.substring(0, value.length() - unit.toString().length()).trim();
|
||||
return new Voltage(parse(value), unit);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Could not parse voltage: " + value);
|
||||
|
||||
Reference in New Issue
Block a user