Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jdk 8 #351

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ If you want to contribute, see this [guide](docs/CONTRIBUTING.md).

## Requirements

Open JDK 11.0.4 + is required.
Open JDK 8+ is required.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</distributionManagement>

<properties>
<jdk.version>11</jdk.version>
<jdk.version>8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.version>0.8.7</jacoco.version>
<sonar.coverage.jacoco.xmlReportPaths>
Expand Down Expand Up @@ -109,7 +109,7 @@
<configuration>
<rules>
<requireJavaVersion>
<version>11.0.4</version>
<version>8</version>
</requireJavaVersion>
<requireMavenVersion>
<version>3.3.9</version>
Expand Down
9 changes: 5 additions & 4 deletions vtl-csv/src/main/java/fr/insee/vtl/csv/CSVDataset.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.vtl.csv;

import fr.insee.vtl.model.Dataset;
import fr.insee.vtl.model.utils.Java8Helpers;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseBool;
import org.supercsv.cellprocessor.ParseDouble;
Expand All @@ -26,8 +27,8 @@ public class CSVDataset implements Dataset {
public CSVDataset(DataStructure structure, Reader csv) throws IOException {
this.structure = structure;
this.csvReader = new CsvMapReader(csv, CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
var columns = this.csvReader.getHeader(true);
if (!this.structure.keySet().containsAll(List.of(columns))) {
String[] columns = this.csvReader.getHeader(true);
if (!this.structure.keySet().containsAll(Java8Helpers.listOf(columns))) {
throw new RuntimeException("missing columns in CSV");
}
}
Expand Down Expand Up @@ -68,8 +69,8 @@ public List<DataPoint> getDataPoints() {
if (this.data == null) {
this.data = new ArrayList<>();
try {
var header = getNameMapping();
var processors = getProcessors();
String[] header = getNameMapping();
CellProcessor[] processors = getProcessors();
Map<String, Object> datum;
while ((datum = this.csvReader.read(header, processors)) != null) {
this.data.add(new DataPoint(this.structure, datum));
Expand Down
8 changes: 8 additions & 0 deletions vtl-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package fr.insee.vtl.engine;

import fr.insee.vtl.engine.exceptions.VtlRuntimeException;
import fr.insee.vtl.model.exceptions.VtlScriptException;
import org.threeten.extra.Interval;
import org.threeten.extra.PeriodDuration;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.time.*;
import java.time.temporal.*;
import java.time.temporal.ChronoUnit;

/**
* This comment explains the temporal functionality supported by Trevas, as defined in the VTL 2.0 specification.
Expand Down Expand Up @@ -188,19 +184,19 @@ public static PeriodDuration period_indicator(Interval timePeriod) {
public static Interval timeshift(Interval time, Long n) {
OffsetDateTime from = time.getStart().atOffset(ZoneOffset.UTC);
OffsetDateTime to = time.getEnd().atOffset(ZoneOffset.UTC);
var dur = PeriodDuration.between(from, to)
PeriodDuration dur = PeriodDuration.between(from, to)
.multipliedBy(n.intValue());
return Interval.of(from.plus(dur.getPeriod()).toInstant(), to.plus(dur.getPeriod()).toInstant());
}

public static ZonedDateTime at_zone(Instant op, String zone) {
var zid = ZoneId.of(zone);
ZoneId zid = ZoneId.of(zone);
return op.atZone(zid);
}


private static Interval truncate_time(Interval op, ChronoUnit unit, ZoneId zone) {
var start = truncate_time(op.getStart(), unit, zone);
Instant start = truncate_time(op.getStart(), unit, zone);
return Interval.of(start, unit.getDuration());
}

Expand All @@ -213,7 +209,7 @@ public static Interval truncate_time(Interval op, String unit) {
}

private static Instant truncate_time(Instant op, ChronoUnit unit, ZoneId zone) {
var zonedOp = op.atZone(zone);
ZonedDateTime zonedOp = op.atZone(zone);
switch (unit) {
case DAYS:
return zonedOp.truncatedTo(ChronoUnit.DAYS).toInstant();
Expand Down Expand Up @@ -248,7 +244,7 @@ public static ZonedDateTime truncate_time(ZonedDateTime op, String unit) {
}

public static OffsetDateTime truncate_time(OffsetDateTime op, String unit) {
var zoned = op.toZonedDateTime();
ZonedDateTime zoned = op.toZonedDateTime();
return truncate_time(zoned.toInstant(), toChronoUnit(unit), zoned.getZone()).atOffset(op.getOffset());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.vtl.engine;

import com.github.hervian.reflection.Fun;
import fr.insee.vtl.model.utils.Java8Helpers;
import fr.insee.vtl.engine.visitors.expression.*;
import fr.insee.vtl.engine.visitors.expression.functions.ComparisonFunctionsVisitor;
import fr.insee.vtl.engine.visitors.expression.functions.DistanceFunctionsVisitor;
Expand All @@ -16,7 +17,7 @@

public class VtlNativeMethods {

public static final Set<Method> NATIVE_METHODS = Set.of(
public static final Set<Method> NATIVE_METHODS = Java8Helpers.setOf(
// NumericFunctionsVisitor
Fun.toMethod(NumericFunctionsVisitor::ceil),
Fun.toMethod(NumericFunctionsVisitor::floor),
Expand Down
59 changes: 14 additions & 45 deletions vtl-engine/src/main/java/fr/insee/vtl/engine/VtlScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,24 @@

import fr.insee.vtl.engine.exceptions.VtlRuntimeException;
import fr.insee.vtl.engine.exceptions.VtlSyntaxException;
import fr.insee.vtl.model.utils.Java8Helpers;
import fr.insee.vtl.engine.visitors.AssignmentVisitor;
import fr.insee.vtl.model.FunctionProvider;
import fr.insee.vtl.model.Positioned;
import fr.insee.vtl.model.ProcessingEngine;
import fr.insee.vtl.model.ProcessingEngineFactory;
import fr.insee.vtl.model.VtlMethod;
import fr.insee.vtl.model.*;
import fr.insee.vtl.model.exceptions.VtlScriptException;
import fr.insee.vtl.parser.VtlLexer;
import fr.insee.vtl.parser.VtlParser;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;

import javax.script.AbstractScriptEngine;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import javax.script.*;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.StringJoiner;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -113,7 +84,7 @@ public static Positioned fromTokens(Token from, Token to) {
if (to == null) {
to = from;
}
var position = new Positioned.Position(
Positioned.Position position = new Positioned.Position(
from.getLine() - 1,
to.getLine() - 1,
from.getCharPositionInLine(),
Expand Down Expand Up @@ -201,12 +172,10 @@ private String getProcessingEngineName() {
*/
public ProcessingEngine getProcessingEngine() {
String name = getProcessingEngineName();
Optional<ProcessingEngineFactory> factory = ServiceLoader.load(ProcessingEngineFactory.class)
.stream()
.map(ServiceLoader.Provider::get)
Optional<ProcessingEngineFactory> factory = Java8Helpers.streamIterator(ServiceLoader.load(ProcessingEngineFactory.class).iterator())
.filter(f -> f.getName().equals(name))
.findFirst();
return factory.orElseThrow().getProcessingEngine(this);
return factory.orElseThrow(() -> new NoSuchElementException("No value present")).getProcessingEngine(this);
}

/**
Expand All @@ -231,7 +200,7 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int
if (offendingSymbol instanceof Token) {
errors.add(new VtlSyntaxException(msg, fromToken((Token) offendingSymbol)));
} else {
var pos = new Positioned.Position(startLine, startLine, startColumn, startColumn + 1);
Positioned.Position pos = new Positioned.Position(startLine, startLine, startColumn, startColumn + 1);
errors.add(new VtlScriptException(msg, () -> pos));
}
}
Expand All @@ -248,10 +217,10 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int

// Note that we need to call this method to trigger the
// error listener.
var start = parser.start();
VtlParser.StartContext start = parser.start();

if (!errors.isEmpty()) {
var first = errors.removeFirst();
VtlScriptException first = errors.removeFirst();
for (VtlScriptException suppressed : errors) {
first.addSuppressed(suppressed);
}
Expand Down Expand Up @@ -322,14 +291,14 @@ public ScriptEngineFactory getFactory() {
}

public VtlMethod findMethod(String name, Collection<Class> types) throws NoSuchMethodException {
Set<Method> customMethods = methodCache == null ? Set.of()
Set<Method> customMethods = methodCache == null ? Java8Helpers.setOf()
: new HashSet<>(methodCache.values());
Set<Method> methods = Stream.concat(NATIVE_METHODS.stream(), customMethods.stream())
.collect(Collectors.toSet());

List<Method> candidates = methods.stream()
.filter(method -> method.getName().equals(name))
.filter(method -> matchParameters(method, types.toArray(Class[]::new)))
.filter(method -> matchParameters(method, types.toArray(new Class[0])))
.collect(Collectors.toList());
if (candidates.size() == 1) {
return new VtlMethod(candidates.get(0));
Expand All @@ -349,7 +318,7 @@ public VtlMethod findGlobalMethod(String name, Collection<Class> types) throws N

List<Method> candidates = methods.stream()
.filter(method -> method.getName().equals(name))
.filter(method -> matchParameters(method, types.toArray(Class[]::new)))
.filter(method -> matchParameters(method, types.toArray(new Class[0])))
.collect(Collectors.toList());

if (candidates.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.insee.vtl.engine;

import fr.insee.vtl.model.utils.Java8Helpers;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import java.util.List;
Expand Down Expand Up @@ -37,7 +39,7 @@ public String getEngineVersion() {
*/
@Override
public List<String> getExtensions() {
return List.of("vtl");
return Java8Helpers.listOf("vtl");
}

/**
Expand All @@ -47,7 +49,7 @@ public List<String> getExtensions() {
*/
@Override
public List<String> getMimeTypes() {
return List.of();
return Java8Helpers.listOf();
}

/**
Expand All @@ -57,7 +59,7 @@ public List<String> getMimeTypes() {
*/
@Override
public List<String> getNames() {
return List.of(getLanguageName(), getEngineName(), "vtl", "Trevas", "trevas");
return Java8Helpers.listOf(getLanguageName(), getEngineName(), "vtl", "Trevas", "trevas");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CastExpression extends ResolvableExpression {
public CastExpression(Positioned position, ResolvableExpression expr, String mask, Class<?> target) throws VtlScriptException {
super(position);
this.target = target;
var source = expr.getType();
Class<?> source = expr.getType();
if (source.equals(target)) {
this.expr = expr;
} else {
Expand All @@ -45,7 +45,7 @@ public CastExpression(Positioned position, ResolvableExpression expr, String mas
}

public ResolvableExpression castBoolean(ResolvableExpression expr) {
var outputClass = getType();
Class<?> outputClass = getType();
if (outputClass.equals(String.class)) {
return ResolvableExpression.withType(String.class).withPosition(expr).using(context -> {
Boolean exprValue = (Boolean) expr.resolve(context);
Expand All @@ -69,7 +69,7 @@ public ResolvableExpression castBoolean(ResolvableExpression expr) {
}

private ResolvableExpression castDouble(ResolvableExpression expr) {
var outputClass = getType();
Class<?> outputClass = getType();
if (outputClass.equals(String.class))
return ResolvableExpression.withType(String.class).withPosition(expr).using(context -> {
Double exprValue = (Double) expr.resolve(context);
Expand Down Expand Up @@ -98,11 +98,11 @@ private ResolvableExpression castDouble(ResolvableExpression expr) {
}

private ResolvableExpression castInstant(ResolvableExpression expr, String mask) {
var outputClass = getType();
Class<?> outputClass = getType();
if (outputClass.equals(String.class))
return ResolvableExpression.withType(String.class).withPosition(expr).using(context -> {

var value = expr.resolve(context);
Object value = expr.resolve(context);
Instant exprValue;
if (value instanceof LocalDate) {
exprValue = ((LocalDate) value).atStartOfDay().toInstant(ZoneOffset.UTC);
Expand All @@ -117,7 +117,7 @@ private ResolvableExpression castInstant(ResolvableExpression expr, String mask)
}

private ResolvableExpression castLong(ResolvableExpression expr) {
var outputClass = getType();
Class<?> outputClass = getType();
if (outputClass.equals(String.class))
return ResolvableExpression.withType(String.class).withPosition(expr).using(context -> {
Long exprValue = (Long) expr.resolve(context);
Expand All @@ -140,7 +140,7 @@ private ResolvableExpression castLong(ResolvableExpression expr) {
}

private ResolvableExpression castString(ResolvableExpression expr, String mask) {
var outputClass = getType();
Class<?> outputClass = getType();
if (outputClass.equals(Long.class)) {
return ResolvableExpression.withType(Long.class).withPosition(expr).using(context -> {
String exprValue = (String) expr.resolve(context);
Expand Down
Loading
Loading