diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java new file mode 100644 index 00000000..9333b0e8 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java @@ -0,0 +1,130 @@ +package de.steamwar.bausystem.features.script.variables; + +import lombok.AllArgsConstructor; + +public interface Value { + long asLong(); + boolean asBoolean(); + String asString(); + + void fromLong(long value); + void fromBoolean(boolean value); + void fromString(String value); + + @AllArgsConstructor + class LongValue implements Value { + + private long value; + + @Override + public long asLong() { + return value; + } + + @Override + public boolean asBoolean() { + return value != 0; + } + + @Override + public String asString() { + return value + ""; + } + + @Override + public void fromLong(long value) { + this.value = value; + } + + @Override + public void fromBoolean(boolean value) { + this.value = value ? 1 : 0; + } + + @Override + public void fromString(String value) { + try { + this.value = Long.parseLong(value); + } catch (NumberFormatException e) { + this.value = 0; + } + } + } + + @AllArgsConstructor + class BooleanValue implements Value { + + private boolean value; + + @Override + public long asLong() { + return value ? 1 : 0; + } + + @Override + public boolean asBoolean() { + return value; + } + + @Override + public String asString() { + return value + ""; + } + + @Override + public void fromLong(long value) { + this.value = value != 0; + } + + @Override + public void fromBoolean(boolean value) { + this.value = value; + } + + @Override + public void fromString(String value) { + this.value = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); + } + } + + @AllArgsConstructor + class StringValue implements Value { + + private String value; + + @Override + public long asLong() { + try { + return Long.parseLong(value); + } catch (NumberFormatException e) { + return 0; + } + } + + @Override + public boolean asBoolean() { + return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); + } + + @Override + public String asString() { + return value; + } + + @Override + public void fromLong(long value) { + this.value = value + ""; + } + + @Override + public void fromBoolean(boolean value) { + this.value = value + ""; + } + + @Override + public void fromString(String value) { + this.value = value; + } + } + +}