All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.intelie.pipes.ast.LiteralNode Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes.ast;

import net.intelie.pipes.types.Type;
import net.intelie.pipes.util.LiteralRepresentation;
import net.intelie.pipes.util.Preconditions;

import java.util.Objects;

public class LiteralNode implements AstNode {
    private static final long serialVersionUID = 1L;

    private final SourceLocation location;
    private final Type type;
    private final Object value;
    private final int _hash;

    public LiteralNode(SourceLocation location, Type type, Object value) {
        Preconditions.checkArgument(type != null, "must have type");
        this.location = location;
        this.type = type;
        this.value = value;
        this._hash = Objects.hash(this.type, this.value);
    }

    public static LiteralNode newNumber(SourceLocation location, Object value) {
        return new LiteralNode(location, Type.NUMBER, value);
    }

    public static LiteralNode newString(SourceLocation location, Object value) {
        return new LiteralNode(location, Type.STRING, value);
    }

    public static LiteralNode newObject(SourceLocation location, Object value) {
        return new LiteralNode(location, Type.OBJECT, value);
    }

    public static LiteralNode newBoolean(SourceLocation location, Object value) {
        return new LiteralNode(location, Type.BOOLEAN, value);
    }

    public static LiteralNode newNull(SourceLocation location) {
        return new LiteralNode(location, Type.NULL, null);
    }

    public static LiteralNode newAuto(SourceLocation location, Object value) {
        return new LiteralNode(location, Type.infer(value), value);
    }


    public Type getType() {
        return type;
    }

    public Object getValue() {
        return value;
    }

    @Override
    public SourceLocation getLocation() {
        return location;
    }

    @Override
    public String toString() {
        return LiteralRepresentation.toString(type, value);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LiteralNode that = (LiteralNode) o;

        boolean b = Objects.equals(this.type, that.type) &&
                Objects.equals(this.type.cast(this.value), that.type.cast(that.value));

        return b;
    }

    @Override
    public int hashCode() {
        return _hash;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy