package ra;
import core.CellAddressFactory;
import java.util.HashMap;
import java.util.Map;
import ra.operators.*;
/**
*
* @author Jacek Sroka
*/
public class Utils {
private static CellAddressFactory caf = new CellAddressFactory();
private static Map<String, Result> resultMap = new HashMap<>();
static {
reset();
}
public static void addAlias(String aliasName, Result result) {
if (resultMap.get(aliasName) == null) {
resultMap.put(aliasName, result.clone());
} else {
throw new RuntimeException("The alias: " + aliasName + " is already used.");
}
}
public static Result getPreviouslyComputedResult(String aliasName) {
Result result = resultMap.get(aliasName);
if (result == null) {
throw new RuntimeException("Cannot reference an alias before it was computed. The terms are computed in an infix order.");
} else {
return result;
}
}
private static void initCellAddressFactory(int firstFreeColumn) {
caf = new CellAddressFactory(firstFreeColumn);
}
public static CellAddressFactory getCaf() {
return caf;
}
public static void reset() {
initCellAddressFactory(1);
resultMap = new HashMap<>();
}
public static void reset(int firstFreeColumn) {
initCellAddressFactory(firstFreeColumn);
}
public static ReferenceT ref(String alias) {
return new ReferenceT(alias);
}
public static RelationT rel(int size) {
return new RelationT(caf.getSpaceForNewRelation(size));
}
public static RelationT rel(String alias, int size) {
return (RelationT) new RelationT(caf.getSpaceForNewRelation(size)).setAlias(alias);
}
public static SortAscT sortAsc(Term t, int index) {
return new SortAscT(t, index);
}
public static SortAscT sortAsc(String alias, Term t, int index) {
return (SortAscT) new SortAscT(t, index).setAlias(alias);
}
public static SortDescT sortDesc(Term t, int index) {
return new SortDescT(t, index);
}
public static SortDescT sortDesc(String alias, Term t, int index) {
return (SortDescT) new SortDescT(t, index).setAlias(alias);
}
public static ProjectT project(Term t, int... indexes) {
return new ProjectT(t, indexes);
}
public static ProjectT project(String alias, Term t, int... indexes) {
return (ProjectT) new ProjectT(t, indexes).setAlias(alias);
}
public static SelectT select(Term t, String... expressions) {
return new SelectT(t, expressions);
}
public static SelectT select(String alias, Term t, String... expressions) {
return (SelectT) new SelectT(t, expressions).setAlias(alias);
}
public static FilterT filter(Term t, String pred) {
return new FilterT(t, pred);
}
public static FilterT filter(String alias, Term t, String pred) {
return (FilterT) new FilterT(t, pred).setAlias(alias);
}
public static DupRemT dupRem(Term t) {
return new DupRemT(t);
}
public static DupRemT dupRem(String alias, Term t) {
return (DupRemT) new DupRemT(t).setAlias(alias);
}
public static StandT stand(Term t) {
return new StandT(t);
}
public static StandT stand(String alias, Term t) {
return (StandT) new StandT(t).setAlias(alias);
}
public static UnionT union(Term t1, Term t2) {
return new UnionT(t1, t2);
}
public static UnionT union(String alias, Term t1, Term t2) {
return (UnionT) new UnionT(t1, t2).setAlias(alias);
}
public static DiffBag minusBag(Term t1, Term t2) {
return new DiffBag(t1, t2);
}
public static DiffBag minusBag(String alias, Term t1, Term t2) {
return (DiffBag) new DiffBag(t1, t2).setAlias(alias);
}
public static DiffSet minusSet(Term t1, Term t2) {
return new DiffSet(t1, t2);
}
public static DiffSet minusSet(String alias, Term t1, Term t2) {
return (DiffSet) new DiffSet(t1, t2).setAlias(alias);
}
public static DiffSet diffSet(Term t1, Term t2) {
return new DiffSet(t1, t2);
}
public static DiffSet diffSet(String alias, Term t1, Term t2) {
return (DiffSet) new DiffSet(t1, t2).setAlias(alias);
}
public static DiffBag diffBag(Term t1, Term t2) {
return new DiffBag(t1, t2);
}
public static DiffBag diffBag(String alias, Term t1, Term t2) {
return (DiffBag) new DiffBag(t1, t2).setAlias(alias);
}
public static CartProdT cartProd(Term t1, Term t2) {
return new CartProdT(t1, t2);
}
public static CartProdT cartProd(String alias, Term t1, Term t2) {
return (CartProdT) new CartProdT(t1, t2).setAlias(alias);
}
public static GenJoinT genJoin(Term t1, Term t2, String pred) {
return new GenJoinT(t1, t2, pred);
}
public static GenJoinT genJoin(String alias, Term t1, Term t2, String pred) {
return (GenJoinT) new GenJoinT(t1, t2, pred).setAlias(alias);
}
public static EqJoinT eqJoin(Term t1, Term t2, int index1, int index2) {
return new EqJoinT(t1, t2, index1, index2);
}
public static EqJoinT eqJoin(String alias, Term t1, Term t2, int index1, int index2) {
return (EqJoinT) new EqJoinT(t1, t2, index1, index2).setAlias(alias);
}
public static SemiJoinT semiJoin(Term t1, Term t2, int index1, int index2) {
return new SemiJoinT(t1, t2, index1, index2);
}
public static SemiJoinT semiJoin(String alias, Term t1, Term t2, int index1, int index2) {
return (SemiJoinT) new SemiJoinT(t1, t2, index1, index2).setAlias(alias);
}
public static AggregT groupBy(Term t, int... indexes) {
return new AggregT(t, indexes);
}
public static AggregT groupBy(String alias, Term t, int... indexes) {
return (AggregT) new AggregT(t, indexes).setAlias(alias);
}
public static AggregT groupBy(Term t) {
return new AggregT(t);
}
public static AggregT groupBy(String alias, Term t) {
return (AggregT) new AggregT(t).setAlias(alias);
}
}