JAVA-2116: Split or move libraries-data-2 module (#9716)
* JAVA-2116: Move Java-R Integration to libraries-6 module * JAVA-2116: Move Guide to JMapper to libraries-data module
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.jmapper;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
|
||||
public class User {
|
||||
|
||||
private long id;
|
||||
private String email;
|
||||
private LocalDate birthDate;
|
||||
|
||||
// constructors
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(long id, String email, LocalDate birthDate) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public LocalDate getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
public void setBirthDate(LocalDate birthDate) {
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [id=" + id + ", email=" + email + ", birthDate=" + birthDate + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.baeldung.jmapper;
|
||||
|
||||
import com.googlecode.jmapper.annotations.JMap;
|
||||
import com.googlecode.jmapper.annotations.JMapConversion;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
|
||||
public class UserDto {
|
||||
|
||||
@JMap
|
||||
private long id;
|
||||
|
||||
@JMap("email")
|
||||
private String username;
|
||||
|
||||
@JMap("birthDate")
|
||||
private int age;
|
||||
|
||||
@JMapConversion(from={"birthDate"}, to={"age"})
|
||||
public int conversion(LocalDate birthDate){
|
||||
return Period.between(birthDate, LocalDate.now()).getYears();
|
||||
}
|
||||
|
||||
// constructors
|
||||
|
||||
public UserDto() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserDto(long id, String username, int age) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserDto [id=" + id + ", username=" + username + ", age=" + age + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.jmapper;
|
||||
|
||||
import com.googlecode.jmapper.annotations.JGlobalMap;
|
||||
|
||||
@JGlobalMap
|
||||
public class UserDto1 {
|
||||
|
||||
private long id;
|
||||
private String email;
|
||||
|
||||
|
||||
// constructors
|
||||
|
||||
public UserDto1() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserDto1(long id, String email) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserDto [id=" + id + ", email=" + email + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.jmapper.relational;
|
||||
|
||||
import com.googlecode.jmapper.annotations.JMap;
|
||||
|
||||
|
||||
public class User {
|
||||
|
||||
@JMap(classes = {UserDto1.class, UserDto2.class})
|
||||
private long id;
|
||||
|
||||
@JMap(attributes = {"username", "email"}, classes = {UserDto1.class, UserDto2.class})
|
||||
private String email;
|
||||
|
||||
// constructors
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(long id, String email) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [id=" + id + ", email=" + email + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.jmapper.relational;
|
||||
|
||||
|
||||
public class UserDto1 {
|
||||
|
||||
private long id;
|
||||
private String username;
|
||||
|
||||
// constructors
|
||||
|
||||
public UserDto1() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserDto1(long id, String username) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserDto [id=" + id + ", username=" + username + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.jmapper.relational;
|
||||
|
||||
|
||||
public class UserDto2 {
|
||||
|
||||
private long id;
|
||||
private String email;
|
||||
|
||||
// constructors
|
||||
|
||||
public UserDto2() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserDto2(long id, String email) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
// getters and setters
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserDto2 [id=" + id + ", email=" + email + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.r;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
/**
|
||||
* FastR showcase.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
public class FastRMean {
|
||||
|
||||
/**
|
||||
* Invokes the customMean R function passing the given values as arguments.
|
||||
*
|
||||
* @param values the input to the mean script
|
||||
* @return the result of the R script
|
||||
*/
|
||||
public double mean(int[] values) {
|
||||
Context polyglot = Context.newBuilder()
|
||||
.allowAllAccess(true)
|
||||
.build();
|
||||
String meanScriptContent = RUtils.getMeanScriptContent();
|
||||
polyglot.eval("R", meanScriptContent);
|
||||
Value rBindings = polyglot.getBindings("R");
|
||||
Value rInput = rBindings.getMember("c")
|
||||
.execute(values);
|
||||
return rBindings.getMember("customMean")
|
||||
.execute(rInput)
|
||||
.asDouble();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.r;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import com.github.rcaller.rstuff.RCaller;
|
||||
import com.github.rcaller.rstuff.RCallerOptions;
|
||||
import com.github.rcaller.rstuff.RCode;
|
||||
|
||||
/**
|
||||
* RCaller showcase.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
public class RCallerMean {
|
||||
|
||||
/**
|
||||
* Invokes the customMean R function passing the given values as arguments.
|
||||
*
|
||||
* @param values the input to the mean script
|
||||
* @return the result of the R script
|
||||
* @throws IOException if any error occurs
|
||||
* @throws URISyntaxException if any error occurs
|
||||
*/
|
||||
public double mean(int[] values) throws IOException, URISyntaxException {
|
||||
String fileContent = RUtils.getMeanScriptContent();
|
||||
RCode code = RCode.create();
|
||||
code.addRCode(fileContent);
|
||||
code.addIntArray("input", values);
|
||||
code.addRCode("result <- customMean(input)");
|
||||
RCaller caller = RCaller.create(code, RCallerOptions.create());
|
||||
caller.runAndReturnResult("result");
|
||||
return caller.getParser()
|
||||
.getAsDoubleArray("result")[0];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.r;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Utility class for loading the script.R content.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
public class RUtils {
|
||||
|
||||
/**
|
||||
* Loads the script.R and returns its content as a string.
|
||||
*
|
||||
* @return the script.R content as a string
|
||||
* @throws IOException if any error occurs
|
||||
* @throws URISyntaxException if any error occurs
|
||||
*/
|
||||
static String getMeanScriptContent() throws IOException, URISyntaxException {
|
||||
URI rScriptUri = RUtils.class.getClassLoader()
|
||||
.getResource("script.R")
|
||||
.toURI();
|
||||
Path inputScript = Paths.get(rScriptUri);
|
||||
return Files.lines(inputScript)
|
||||
.collect(Collectors.joining());
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.r;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.renjin.script.RenjinScriptEngine;
|
||||
import org.renjin.sexp.DoubleArrayVector;
|
||||
|
||||
/**
|
||||
* Renjin showcase.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
public class RenjinMean {
|
||||
|
||||
/**
|
||||
* Invokes the customMean R function passing the given values as arguments.
|
||||
*
|
||||
* @param values the input to the mean script
|
||||
* @return the result of the R script
|
||||
* @throws IOException if any error occurs
|
||||
* @throws URISyntaxException if any error occurs
|
||||
* @throws ScriptException if any error occurs
|
||||
*/
|
||||
public double mean(int[] values) throws IOException, URISyntaxException, ScriptException {
|
||||
RenjinScriptEngine engine = new RenjinScriptEngine();
|
||||
String meanScriptContent = RUtils.getMeanScriptContent();
|
||||
engine.put("input", values);
|
||||
engine.eval(meanScriptContent);
|
||||
DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)");
|
||||
return result.asReal();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.r;
|
||||
|
||||
import org.rosuda.REngine.REXPMismatchException;
|
||||
import org.rosuda.REngine.REngineException;
|
||||
import org.rosuda.REngine.Rserve.RConnection;
|
||||
|
||||
/**
|
||||
* Rserve showcase.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
public class RserveMean {
|
||||
|
||||
/**
|
||||
* Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the
|
||||
* customMean R function passing the given values as arguments.
|
||||
*
|
||||
* @param values the input to the mean script
|
||||
* @return the result of the R script
|
||||
* @throws REngineException if any error occurs
|
||||
* @throws REXPMismatchException if any error occurs
|
||||
*/
|
||||
public double mean(int[] values) throws REngineException, REXPMismatchException {
|
||||
RConnection c = new RConnection();
|
||||
c.assign("input", values);
|
||||
return c.eval("customMean(input)")
|
||||
.asDouble();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user