Skip to content

Commit 7d6e096

Browse files
committed
Fixed whitespace formatting.
1 parent 16ca523 commit 7d6e096

10 files changed

Lines changed: 184 additions & 177 deletions

File tree

libraries-data-2/pom.xml

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,21 @@
128128
<version>${awaitility.version}</version>
129129
<scope>test</scope>
130130
</dependency>
131-
132-
<dependency>
133-
<groupId>org.rosuda.REngine</groupId>
134-
<artifactId>Rserve</artifactId>
135-
<version>${rserve.version}</version>
136-
</dependency>
137-
138-
<dependency>
139-
<groupId>com.github.jbytecode</groupId>
140-
<artifactId>RCaller</artifactId>
141-
<version>${rcaller.version}</version>
142-
</dependency>
143-
144-
<dependency>
145-
<groupId>org.renjin</groupId>
146-
<artifactId>renjin-script-engine</artifactId>
147-
<version>${renjin.version}</version>
148-
</dependency>
131+
<dependency>
132+
<groupId>org.rosuda.REngine</groupId>
133+
<artifactId>Rserve</artifactId>
134+
<version>${rserve.version}</version>
135+
</dependency>
136+
<dependency>
137+
<groupId>com.github.jbytecode</groupId>
138+
<artifactId>RCaller</artifactId>
139+
<version>${rcaller.version}</version>
140+
</dependency>
141+
<dependency>
142+
<groupId>org.renjin</groupId>
143+
<artifactId>renjin-script-engine</artifactId>
144+
<version>${renjin.version}</version>
145+
</dependency>
149146
</dependencies>
150147

151148
<repositories>
@@ -157,11 +154,11 @@
157154
</repository>
158155

159156
<!-- Needed for Renjin -->
160-
<repository>
161-
<id>bedatadriven</id>
162-
<name>bedatadriven public repo</name>
163-
<url>https://nexus.bedatadriven.com/content/groups/public/</url>
164-
</repository>
157+
<repository>
158+
<id>bedatadriven</id>
159+
<name>bedatadriven public repo</name>
160+
<url>https://nexus.bedatadriven.com/content/groups/public/</url>
161+
</repository>
165162
</repositories>
166163

167164
<properties>
@@ -183,22 +180,22 @@
183180
<rserve.version>1.8.1</rserve.version>
184181
</properties>
185182

186-
<build>
187-
<plugins>
188-
<plugin>
189-
<groupId>org.apache.maven.plugins</groupId>
190-
<artifactId>maven-compiler-plugin</artifactId>
191-
<configuration>
192-
<!-- Excludes FastR classes from compilations since they require GraalVM -->
193-
<excludes>
194-
<exclude>com/baeldung/r/FastRMean.java</exclude>
195-
</excludes>
196-
<testExcludes>
197-
<exclude>com/baeldung/r/FastRMeanUnitTest.java</exclude>
198-
</testExcludes>
199-
</configuration>
200-
</plugin>
201-
</plugins>
202-
</build>
183+
<build>
184+
<plugins>
185+
<plugin>
186+
<groupId>org.apache.maven.plugins</groupId>
187+
<artifactId>maven-compiler-plugin</artifactId>
188+
<configuration>
189+
<!-- Excludes FastR classes from compilations since they require GraalVM -->
190+
<excludes>
191+
<exclude>com/baeldung/r/FastRMean.java</exclude>
192+
</excludes>
193+
<testExcludes>
194+
<exclude>com/baeldung/r/FastRMeanUnitTest.java</exclude>
195+
</testExcludes>
196+
</configuration>
197+
</plugin>
198+
</plugins>
199+
</build>
203200

204201
</project>

libraries-data-2/src/main/java/com/baeldung/r/FastRMean.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@
1010
*/
1111
public class FastRMean {
1212

13-
/**
14-
* Invokes the customMean R function passing the given values as arguments.
15-
*
16-
* @param values the input to the mean script
17-
* @return the result of the R script
18-
*/
19-
public double mean(int[] values) {
20-
Context polyglot = Context.newBuilder().allowAllAccess(true).build();
21-
String meanScriptContent = RUtils.getMeanScriptContent();
22-
polyglot.eval("R", meanScriptContent);
23-
Value rBindings = polyglot.getBindings("R");
24-
Value rInput = rBindings.getMember("c").execute(values);
25-
return rBindings.getMember("customMean").execute(rInput).asDouble();
26-
}
13+
/**
14+
* Invokes the customMean R function passing the given values as arguments.
15+
*
16+
* @param values the input to the mean script
17+
* @return the result of the R script
18+
*/
19+
public double mean(int[] values) {
20+
Context polyglot = Context.newBuilder()
21+
.allowAllAccess(true)
22+
.build();
23+
String meanScriptContent = RUtils.getMeanScriptContent();
24+
polyglot.eval("R", meanScriptContent);
25+
Value rBindings = polyglot.getBindings("R");
26+
Value rInput = rBindings.getMember("c")
27+
.execute(values);
28+
return rBindings.getMember("customMean")
29+
.execute(rInput)
30+
.asDouble();
31+
}
2732

2833
}

libraries-data-2/src/main/java/com/baeldung/r/RCallerMean.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@
1414
*/
1515
public class RCallerMean {
1616

17-
/**
18-
* Invokes the customMean R function passing the given values as arguments.
19-
*
20-
* @param values the input to the mean script
21-
* @return the result of the R script
22-
* @throws IOException if any error occurs
23-
* @throws URISyntaxException if any error occurs
24-
*/
25-
public double mean(int[] values) throws IOException, URISyntaxException {
26-
String fileContent = RUtils.getMeanScriptContent();
27-
RCode code = RCode.create();
28-
code.addRCode(fileContent);
29-
code.addIntArray("input", values);
30-
code.addRCode("result <- customMean(input)");
31-
RCaller caller = RCaller.create(code, RCallerOptions.create());
32-
caller.runAndReturnResult("result");
33-
return caller.getParser().getAsDoubleArray("result")[0];
34-
}
17+
/**
18+
* Invokes the customMean R function passing the given values as arguments.
19+
*
20+
* @param values the input to the mean script
21+
* @return the result of the R script
22+
* @throws IOException if any error occurs
23+
* @throws URISyntaxException if any error occurs
24+
*/
25+
public double mean(int[] values) throws IOException, URISyntaxException {
26+
String fileContent = RUtils.getMeanScriptContent();
27+
RCode code = RCode.create();
28+
code.addRCode(fileContent);
29+
code.addIntArray("input", values);
30+
code.addRCode("result <- customMean(input)");
31+
RCaller caller = RCaller.create(code, RCallerOptions.create());
32+
caller.runAndReturnResult("result");
33+
return caller.getParser()
34+
.getAsDoubleArray("result")[0];
35+
}
3536

3637
}

libraries-data-2/src/main/java/com/baeldung/r/RUtils.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
*/
1616
public class RUtils {
1717

18-
/**
19-
* Loads the script.R and returns its content as a string.
20-
*
21-
* @return the script.R content as a string
22-
* @throws IOException if any error occurs
23-
* @throws URISyntaxException if any error occurs
24-
*/
25-
static String getMeanScriptContent() throws IOException, URISyntaxException {
26-
URI rScriptUri = RUtils.class.getClassLoader().getResource("script.R").toURI();
27-
Path inputScript = Paths.get(rScriptUri);
28-
return Files.lines(inputScript).collect(Collectors.joining());
29-
}
18+
/**
19+
* Loads the script.R and returns its content as a string.
20+
*
21+
* @return the script.R content as a string
22+
* @throws IOException if any error occurs
23+
* @throws URISyntaxException if any error occurs
24+
*/
25+
static String getMeanScriptContent() throws IOException, URISyntaxException {
26+
URI rScriptUri = RUtils.class.getClassLoader()
27+
.getResource("script.R")
28+
.toURI();
29+
Path inputScript = Paths.get(rScriptUri);
30+
return Files.lines(inputScript)
31+
.collect(Collectors.joining());
32+
}
3033
}

libraries-data-2/src/main/java/com/baeldung/r/RenjinMean.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
*/
1616
public class RenjinMean {
1717

18-
/**
19-
* Invokes the customMean R function passing the given values as arguments.
20-
*
21-
* @param values the input to the mean script
22-
* @return the result of the R script
23-
* @throws IOException if any error occurs
24-
* @throws URISyntaxException if any error occurs
25-
* @throws ScriptException if any error occurs
26-
*/
27-
public double mean(int[] values) throws IOException, URISyntaxException, ScriptException {
28-
RenjinScriptEngine engine = new RenjinScriptEngine();
29-
String meanScriptContent = RUtils.getMeanScriptContent();
30-
engine.put("input", values);
31-
engine.eval(meanScriptContent);
32-
DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)");
33-
return result.asReal();
34-
}
18+
/**
19+
* Invokes the customMean R function passing the given values as arguments.
20+
*
21+
* @param values the input to the mean script
22+
* @return the result of the R script
23+
* @throws IOException if any error occurs
24+
* @throws URISyntaxException if any error occurs
25+
* @throws ScriptException if any error occurs
26+
*/
27+
public double mean(int[] values) throws IOException, URISyntaxException, ScriptException {
28+
RenjinScriptEngine engine = new RenjinScriptEngine();
29+
String meanScriptContent = RUtils.getMeanScriptContent();
30+
engine.put("input", values);
31+
engine.eval(meanScriptContent);
32+
DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)");
33+
return result.asReal();
34+
}
3535

3636
}

libraries-data-2/src/main/java/com/baeldung/r/RserveMean.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
*/
1212
public class RserveMean {
1313

14-
/**
15-
* Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the
16-
* customMean R function passing the given values as arguments.
17-
*
18-
* @param values the input to the mean script
19-
* @return the result of the R script
20-
* @throws REngineException if any error occurs
21-
* @throws REXPMismatchException if any error occurs
22-
*/
23-
public double mean(int[] values) throws REngineException, REXPMismatchException {
24-
RConnection c = new RConnection();
25-
c.assign("input", values);
26-
return c.eval("customMean(input)").asDouble();
27-
}
14+
/**
15+
* Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the
16+
* customMean R function passing the given values as arguments.
17+
*
18+
* @param values the input to the mean script
19+
* @return the result of the R script
20+
* @throws REngineException if any error occurs
21+
* @throws REXPMismatchException if any error occurs
22+
*/
23+
public double mean(int[] values) throws REngineException, REXPMismatchException {
24+
RConnection c = new RConnection();
25+
c.assign("input", values);
26+
return c.eval("customMean(input)")
27+
.asDouble();
28+
}
2829

2930
}

libraries-data-2/src/test/java/com/baeldung/r/FastRMeanUnitTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
@Ignore
1313
public class FastRMeanUnitTest {
1414

15-
/**
16-
* Object to test.
17-
*/
18-
private FastRMean fastrMean = new FastRMean();
15+
/**
16+
* Object to test.
17+
*/
18+
private FastRMean fastrMean = new FastRMean();
1919

20-
/**
21-
* Test for {@link FastRMeanUnitTest#mean(int[])}.
22-
*/
23-
@Test
24-
public void givenValues_whenMean_thenCorrect() {
25-
int[] input = { 1, 2, 3, 4, 5 };
26-
double result = fastrMean.mean(input);
27-
Assert.assertEquals(3.0, result, 0.000001);
28-
}
20+
/**
21+
* Test for {@link FastRMeanUnitTest#mean(int[])}.
22+
*/
23+
@Test
24+
public void givenValues_whenMean_thenCorrect() {
25+
int[] input = { 1, 2, 3, 4, 5 };
26+
double result = fastrMean.mean(input);
27+
Assert.assertEquals(3.0, result, 0.000001);
28+
}
2929
}

libraries-data-2/src/test/java/com/baeldung/r/RCallerMeanIntegrationTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
@Ignore
1818
public class RCallerMeanIntegrationTest {
1919

20-
/**
21-
* Object to test.
22-
*/
23-
private RCallerMean rcallerMean = new RCallerMean();
20+
/**
21+
* Object to test.
22+
*/
23+
private RCallerMean rcallerMean = new RCallerMean();
2424

25-
/**
26-
* Test for {@link RCallerMeanIntegrationTest#mean(int[])}.
27-
*
28-
* @throws ScriptException if an error occurs
29-
* @throws URISyntaxException if an error occurs
30-
*/
31-
@Test
32-
public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException {
33-
int[] input = { 1, 2, 3, 4, 5 };
34-
double result = rcallerMean.mean(input);
35-
Assert.assertEquals(3.0, result, 0.000001);
36-
}
25+
/**
26+
* Test for {@link RCallerMeanIntegrationTest#mean(int[])}.
27+
*
28+
* @throws ScriptException if an error occurs
29+
* @throws URISyntaxException if an error occurs
30+
*/
31+
@Test
32+
public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException {
33+
int[] input = { 1, 2, 3, 4, 5 };
34+
double result = rcallerMean.mean(input);
35+
Assert.assertEquals(3.0, result, 0.000001);
36+
}
3737
}

0 commit comments

Comments
 (0)