diff options
author | Martin <marcin.j.chrzanowski@gmail.com> | 2019-11-18 14:53:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-18 14:53:44 +0100 |
commit | e742bf9b8d1bc5ee7a97586510643db6fd3174f2 (patch) | |
tree | f1bf701c819a842e1f82e328cf3118556411502f /src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java | |
parent | 015e46aa190a36c593eeff8b09cea43d9902de0d (diff) |
Implement basic API (#15)
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java')
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java new file mode 100644 index 0000000..d9cbe4c --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -0,0 +1,70 @@ +package pl.edu.mimuw.cloudatlas.model; + +import java.io.ByteArrayInputStream; + +import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program; +import pl.edu.mimuw.cloudatlas.interpreter.query.parser; +import pl.edu.mimuw.cloudatlas.interpreter.query.Yylex; +import pl.edu.mimuw.cloudatlas.model.Value; + +/** + * A class that holds a CloudAtlas query. + */ +public class ValueQuery extends Value { + // Original source code + private String code; + // Parsed query + private Program query; + /** + * Constructs a new <code>ValueQuery</code> object. + * + * @param name the name of the query + * @param query the code of the query + */ + public ValueQuery(String query) throws Exception { + this.code = query; + Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); + this.query = (new parser(lex)).pProgram(); + } + + private ValueQuery() { + this.code = null; + this.query = null; + } + + @Override + public Type getType() { + return TypePrimitive.QUERY; + } + + @Override + public boolean isNull() { + return query == null || code == null; + } + + public Value isEqual(Value value) { + sameTypesOrThrow(value, Operation.EQUAL); + if(isNull() && value.isNull()) + return new ValueBoolean(true); + else if(isNull() || value.isNull()) + return new ValueBoolean(false); + return new ValueBoolean(code.equals(((ValueQuery)value).code)); + } + + @Override + public Value getDefaultValue() { + return new ValueQuery(); + } + + @Override + public Value convertTo(Type type) { + switch(type.getPrimaryType()) { + case QUERY: + return this; + case STRING: + return isNull() ? ValueString.NULL_STRING : new ValueString(code); + default: + throw new UnsupportedConversionException(getType(), type); + } + } +} |