From 1d8a8d7ccfddbb6848084ca71b4dd21f39cfeb9c Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 30 Aug 2017 20:24:17 +0300 Subject: [PATCH] Add functional value --- .../annimon/hotarufx/lib/FunctionValue.java | 69 +++++++++++++++++++ .../java/com/annimon/hotarufx/lib/Types.java | 3 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/annimon/hotarufx/lib/FunctionValue.java diff --git a/app/src/main/java/com/annimon/hotarufx/lib/FunctionValue.java b/app/src/main/java/com/annimon/hotarufx/lib/FunctionValue.java new file mode 100644 index 0000000..82b6982 --- /dev/null +++ b/app/src/main/java/com/annimon/hotarufx/lib/FunctionValue.java @@ -0,0 +1,69 @@ +package com.annimon.hotarufx.lib; + +import com.annimon.hotarufx.exceptions.TypeException; +import java.util.Objects; +import lombok.Getter; + +public class FunctionValue implements Value { + + public static final FunctionValue EMPTY = new FunctionValue(args -> NumberValue.ZERO); + + @Getter + private final Function value; + + public FunctionValue(Function value) { + this.value = value; + } + + @Override + public int type() { + return Types.FUNCTION; + } + + @Override + public Object raw() { + return value; + } + + @Override + public int asInt() { + throw new TypeException("Cannot cast function to integer"); + } + + @Override + public double asNumber() { + throw new TypeException("Cannot cast function to number"); + } + + @Override + public String asString() { + return value.toString(); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 71 * hash + Objects.hashCode(this.value); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) + return false; + final FunctionValue other = (FunctionValue) obj; + return Objects.equals(this.value, other.value); + } + + @Override + public int compareTo(Value o) { + return asString().compareTo(o.asString()); + } + + @Override + public String toString() { + return asString(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/annimon/hotarufx/lib/Types.java b/app/src/main/java/com/annimon/hotarufx/lib/Types.java index 043a5a3..068e08a 100644 --- a/app/src/main/java/com/annimon/hotarufx/lib/Types.java +++ b/app/src/main/java/com/annimon/hotarufx/lib/Types.java @@ -8,5 +8,6 @@ public class Types { STRING = 2, MAP = 3, NODE = 4, - FUNCTION = 5; + PROPERTY = 5, + FUNCTION = 6; }