From d2cc401c61c1b2ac4759419bb81f17a30d9ac9c4 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 21 Apr 2021 09:51:30 +0200 Subject: [PATCH] Add Value for implementing custom type variables --- .../features/script/variables/Value.java | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java 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; + } + } + +}