-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package fr.insee.vtl.prov; | ||
|
||
import org.antlr.v4.runtime.ParserRuleContext; | ||
import org.antlr.v4.runtime.Token; | ||
import org.antlr.v4.runtime.misc.Interval; | ||
|
||
import java.util.Optional; | ||
|
||
public class Variable { | ||
|
||
/** | ||
* Returns the text of a context with empty space (aka. all channels). | ||
*/ | ||
private static String getText(ParserRuleContext ctx) { | ||
Token start = ctx.getStart(); | ||
Token stop = ctx.getStop(); | ||
return start.getInputStream().getText(new Interval(start.getStartIndex(), stop.getStopIndex())); | ||
} | ||
|
||
// Identifier of the variable. | ||
private final ParserRuleContext nameCtx; | ||
|
||
// Expression of the variable, if any. | ||
private final ParserRuleContext exprCtx; | ||
|
||
// Version of the variable. | ||
private Integer version = 0; | ||
|
||
// Previous version. | ||
private Variable previous; | ||
|
||
Variable(ParserRuleContext name, ParserRuleContext expression) { | ||
this.nameCtx = name; | ||
this.exprCtx = expression; | ||
} | ||
|
||
public Variable(ParserRuleContext name) { | ||
this.nameCtx = name; | ||
this.exprCtx = null; | ||
} | ||
|
||
public String getName() { | ||
return getText(this.nameCtx); | ||
} | ||
|
||
public String getVersion() { | ||
return getName() + "_" + this.version; | ||
} | ||
|
||
public Optional<String> getExpression() { | ||
return Optional.ofNullable(exprCtx) | ||
.map(Variable::getText); | ||
} | ||
|
||
public Optional<Variable> getPrevious() { | ||
return Optional.ofNullable(previous); | ||
} | ||
|
||
public Variable newVersion() { | ||
Variable copy = new Variable(this.nameCtx, this.exprCtx); | ||
copy.version = this.version + 1; | ||
copy.previous = this; | ||
return copy; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getVersion(); | ||
} | ||
|
||
public final boolean isSame(Variable other) { | ||
return nameCtx.equals(other.nameCtx); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Variable)) return false; | ||
|
||
Variable variable = (Variable) o; | ||
return getVersion().equals(variable.getVersion()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return nameCtx.hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters