SteamWar/BauSystem2.0
Archiviert
12
0

Add Value for implementing custom type variables

Dieser Commit ist enthalten in:
yoyosource 2021-04-21 09:51:30 +02:00
Ursprung ddf8182041
Commit d2cc401c61

Datei anzeigen

@ -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;
}
}
}