Commit 6eae225e authored by Javier Costa's avatar Javier Costa
Browse files

Added scope and variable classes

parent 1627ca40
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
package tfm.graphlib.utils;

import tfm.graphlib.nodes.Vertex;

public class Scope {

    private Vertex parent;

    public Scope(Vertex parent) {
        this.parent = parent;
    }

    public Vertex getParent() {
        return parent;
    }

    @Override
    public int hashCode() {
        return parent.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;

        if (!(o instanceof Scope))
            return false;

        Scope other = (Scope) o;

        return this.parent.equals(other.parent);
    }

    @Override
    public String toString() {
        return parent.getName();
    }
}
+58 −0
Original line number Diff line number Diff line
package tfm.graphlib.utils;

public class Variable<T> {

    private Scope scope;
    private String name;
    private T value;

    public Variable(Scope scope, String name) {
        this(scope, name, null);
    }

    public Variable(Scope scope, String name, T value) {
        this.scope = scope;
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public Scope getScope() {
        return scope;
    }

    @Override
    public int hashCode() {
        return scope.hashCode() + name.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;

        if (!(o instanceof Variable)) {
            return false;
        }

        Variable other = (Variable) o;

        return name.equals(other.name) && scope.equals(other.scope);
    }

    @Override
    public String toString() {
        return String.format("Variable %s defined in scope %s", name, scope);
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}