Skip to content

Commit 04c255c

Browse files
authored
Merge pull request eugenp#6 from eugenp/master
merging latest changes
2 parents add8e43 + 3df9e64 commit 04c255c

File tree

614 files changed

+9560
-4621
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

614 files changed

+9560
-4621
lines changed

JGit/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.baeldung</groupId>
6-
<artifactId>JGitSnippets</artifactId>
6+
<artifactId>JGit</artifactId>
77
<version>1.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<url>http://maven.apache.org</url>
10+
<name>JGit</name>
1011

1112
<parent>
1213
<groupId>com.baeldung</groupId>

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
The "REST with Spring" Classes
33
==============================
44

5-
After 5 months of work, here's the Master Class of REST With Spring: <br/>
5+
Here's the Master Class of REST With Spring (price changes permanently next Friday): <br/>
66
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
77

88
And here's the Master Class of Learn Spring Security: <br/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.algorithms.distancebetweenpoints;
2+
3+
import java.awt.geom.Point2D;
4+
5+
public class DistanceBetweenPointsService {
6+
7+
public double calculateDistanceBetweenPoints(
8+
double x1,
9+
double y1,
10+
double x2,
11+
double y2) {
12+
13+
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
14+
}
15+
16+
public double calculateDistanceBetweenPointsWithHypot(
17+
double x1,
18+
double y1,
19+
double x2,
20+
double y2) {
21+
22+
double ac = Math.abs(y2 - y1);
23+
double cb = Math.abs(x2 - x1);
24+
25+
return Math.hypot(ac, cb);
26+
}
27+
28+
public double calculateDistanceBetweenPointsWithPoint2D(
29+
double x1,
30+
double y1,
31+
double x2,
32+
double y2) {
33+
34+
return Point2D.distance(x1, y1, x2, y2);
35+
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.algorithms.linesintersection;
2+
3+
import java.awt.Point;
4+
import java.util.Optional;
5+
6+
public class LinesIntersectionService {
7+
8+
public Optional<Point> calculateIntersectionPoint(double m1, double b1, double m2, double b2) {
9+
10+
if (m1 == m2) {
11+
return Optional.empty();
12+
}
13+
14+
double x = (b2 - b1) / (m1 - m2);
15+
double y = m1 * x + b1;
16+
17+
Point point = new Point();
18+
point.setLocation(x, y);
19+
return Optional.of(point);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.algorithms.distancebetweenpoints;
2+
3+
import org.junit.Test;
4+
5+
import com.baeldung.algorithms.distancebetweenpoints.DistanceBetweenPointsService;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class DistanceBetweenPointsServiceUnitTest {
10+
11+
private DistanceBetweenPointsService service = new DistanceBetweenPointsService();
12+
13+
@Test
14+
public void givenTwoPoints_whenCalculateDistanceByFormula_thenCorrect() {
15+
16+
double x1 = 3;
17+
double y1 = 4;
18+
double x2 = 7;
19+
double y2 = 1;
20+
21+
double distance = service.calculateDistanceBetweenPoints(x1, y1, x2, y2);
22+
23+
assertEquals(distance, 5, 0.001);
24+
25+
}
26+
27+
@Test
28+
public void givenTwoPoints_whenCalculateDistanceWithHypot_thenCorrect() {
29+
30+
double x1 = 3;
31+
double y1 = 4;
32+
double x2 = 7;
33+
double y2 = 1;
34+
35+
double distance = service.calculateDistanceBetweenPointsWithHypot(x1, y1, x2, y2);
36+
37+
assertEquals(distance, 5, 0.001);
38+
39+
}
40+
41+
@Test
42+
public void givenTwoPoints_whenCalculateDistanceWithPoint2D_thenCorrect() {
43+
44+
double x1 = 3;
45+
double y1 = 4;
46+
double x2 = 7;
47+
double y2 = 1;
48+
49+
double distance = service.calculateDistanceBetweenPointsWithPoint2D(x1, y1, x2, y2);
50+
51+
assertEquals(distance, 5, 0.001);
52+
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.algorithms.linesintersection;
2+
3+
import java.awt.Point;
4+
import java.util.Optional;
5+
6+
import org.junit.Test;
7+
import static org.junit.Assert.assertTrue;
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class LinesIntersectionServiceUnitTest {
12+
private LinesIntersectionService service = new LinesIntersectionService();
13+
14+
@Test
15+
public void givenNotParallelLines_whenCalculatePoint_thenPresent() {
16+
17+
double m1 = 0;
18+
double b1 = 0;
19+
double m2 = 1;
20+
double b2 = -1;
21+
22+
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
23+
24+
assertTrue(point.isPresent());
25+
assertEquals(point.get().getX(), 1, 0.001);
26+
assertEquals(point.get().getY(), 0, 0.001);
27+
}
28+
29+
@Test
30+
public void givenParallelLines_whenCalculatePoint_thenEmpty() {
31+
double m1 = 1;
32+
double b1 = 0;
33+
double m2 = 1;
34+
double b2 = -1;
35+
36+
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
37+
38+
assertFalse(point.isPresent());
39+
}
40+
}

apache-avro/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.baeldung</groupId>
7-
<artifactId>apache-avro-tutorial</artifactId>
7+
<artifactId>apache-avro</artifactId>
88
<version>0.0.1-SNAPSHOT</version>
9+
<name>Apache Avro</version>
910

1011
<properties>
1112
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</properties>
2121

2222
<build>
23-
<finalName>${artifactId}</finalName>
23+
<finalName>${project.artifactId}</finalName>
2424
<plugins>
2525
<plugin>
2626
<groupId>net.wasdev.wlp.maven.plugins</groupId>

aws/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target/
2+
.idea/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.baeldung.rds;
2+
3+
import com.amazonaws.auth.AWSCredentialsProvider;
4+
import com.amazonaws.auth.AWSStaticCredentialsProvider;
5+
import com.amazonaws.auth.BasicAWSCredentials;
6+
import com.amazonaws.regions.Regions;
7+
import com.amazonaws.services.rds.AmazonRDS;
8+
import com.amazonaws.services.rds.AmazonRDSClientBuilder;
9+
import com.amazonaws.services.rds.model.*;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.sql.*;
14+
import java.util.List;
15+
import java.util.Properties;
16+
import java.util.UUID;
17+
import java.util.logging.Logger;
18+
19+
public class AWSRDSService {
20+
21+
22+
final static Logger logger = Logger.getLogger(AWSRDSService.class.getName());
23+
private AWSCredentialsProvider credentials;
24+
private AmazonRDS amazonRDS;
25+
private String db_username;
26+
private String db_password;
27+
private String db_database;
28+
private String db_hostname;
29+
30+
/*
31+
* User access key and secret key must be set before execute any operation.
32+
* Follow the link on how to get the user access and secret key
33+
* https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/
34+
* **/
35+
public AWSRDSService() throws IOException {
36+
//Init RDS client with credentials and region.
37+
credentials = new
38+
AWSStaticCredentialsProvider(new
39+
BasicAWSCredentials("<ACCESS_KEY>",
40+
"<SECRET_KEY>"));
41+
amazonRDS = AmazonRDSClientBuilder.standard().withCredentials(credentials)
42+
.withRegion(Regions.AP_SOUTHEAST_2).build();
43+
Properties prop = new Properties();
44+
InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties");
45+
prop.load(input);
46+
db_username = prop.getProperty("db_username");
47+
db_password = prop.getProperty("db_password");
48+
db_database = prop.getProperty("db_database");
49+
}
50+
51+
public AWSRDSService(AmazonRDS amazonRDS){
52+
this.amazonRDS = amazonRDS;
53+
}
54+
55+
/**
56+
* create the RDS instance.
57+
* After instance creation, update the db_hostname with endpoint in db.properties.
58+
* */
59+
60+
public String launchInstance() {
61+
62+
String identifier = "";
63+
CreateDBInstanceRequest request = new CreateDBInstanceRequest();
64+
// RDS instance name
65+
request.setDBInstanceIdentifier("Sydney");
66+
request.setEngine("postgres");
67+
request.setMultiAZ(false);
68+
request.setMasterUsername(db_username);
69+
request.setMasterUserPassword(db_password);
70+
request.setDBName(db_database);
71+
request.setStorageType("gp2");
72+
request.setAllocatedStorage(10);
73+
74+
DBInstance instance = amazonRDS.createDBInstance(request);
75+
76+
// Information about the new RDS instance
77+
identifier = instance.getDBInstanceIdentifier();
78+
String status = instance.getDBInstanceStatus();
79+
Endpoint endpoint = instance.getEndpoint();
80+
String endpoint_url = "Endpoint URL not available yet.";
81+
if (endpoint != null) {
82+
endpoint_url = endpoint.toString();
83+
}
84+
logger.info(identifier + "\t" + status);
85+
logger.info(endpoint_url);
86+
87+
return identifier;
88+
89+
}
90+
91+
// Describe DB instances
92+
public void listInstances() {
93+
DescribeDBInstancesResult result = amazonRDS.describeDBInstances();
94+
List<DBInstance> instances = result.getDBInstances();
95+
for (DBInstance instance : instances) {
96+
// Information about each RDS instance
97+
String identifier = instance.getDBInstanceIdentifier();
98+
String engine = instance.getEngine();
99+
String status = instance.getDBInstanceStatus();
100+
Endpoint endpoint = instance.getEndpoint();
101+
String endpoint_url = "Endpoint URL not available yet.";
102+
if (endpoint != null) {
103+
endpoint_url = endpoint.toString();
104+
}
105+
logger.info(identifier + "\t" + engine + "\t" + status);
106+
logger.info("\t" + endpoint_url);
107+
}
108+
109+
}
110+
111+
//Delete RDS instance
112+
public void terminateInstance(String identifier) {
113+
114+
DeleteDBInstanceRequest request = new DeleteDBInstanceRequest();
115+
request.setDBInstanceIdentifier(identifier);
116+
request.setSkipFinalSnapshot(true);
117+
118+
// Delete the RDS instance
119+
DBInstance instance = amazonRDS.deleteDBInstance(request);
120+
121+
// Information about the RDS instance being deleted
122+
String status = instance.getDBInstanceStatus();
123+
Endpoint endpoint = instance.getEndpoint();
124+
String endpoint_url = "Endpoint URL not available yet.";
125+
if (endpoint != null) {
126+
endpoint_url = endpoint.toString();
127+
}
128+
129+
logger.info(identifier + "\t" + status);
130+
logger.info(endpoint_url);
131+
132+
}
133+
134+
135+
public void runJdbcTests() throws SQLException, IOException {
136+
137+
// Getting database properties from db.properties
138+
Properties prop = new Properties();
139+
InputStream input = AWSRDSService.class.getClassLoader().getResourceAsStream("db.properties");
140+
prop.load(input);
141+
db_hostname = prop.getProperty("db_hostname");
142+
String jdbc_url = "jdbc:postgresql://" + db_hostname + ":5432/" + db_database;
143+
144+
// Create a connection using the JDBC driver
145+
try (Connection conn = DriverManager.getConnection(jdbc_url, db_username, db_password)) {
146+
147+
// Create the test table if not exists
148+
Statement statement = conn.createStatement();
149+
String sql = "CREATE TABLE IF NOT EXISTS jdbc_test (id SERIAL PRIMARY KEY, content VARCHAR(80))";
150+
statement.executeUpdate(sql);
151+
152+
// Do some INSERT
153+
PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO jdbc_test (content) VALUES (?)");
154+
String content = "" + UUID.randomUUID();
155+
preparedStatement.setString(1, content);
156+
preparedStatement.executeUpdate();
157+
logger.info("INSERT: " + content);
158+
159+
// Do some SELECT
160+
sql = "SELECT * FROM jdbc_test";
161+
ResultSet resultSet = statement.executeQuery(sql);
162+
while (resultSet.next()) {
163+
String count = resultSet.getString("content");
164+
logger.info("Total Records: " + count);
165+
}
166+
}
167+
168+
169+
}
170+
171+
public static void main(String[] args) throws IOException, SQLException {
172+
AWSRDSService awsrdsService = new AWSRDSService();
173+
174+
String instanceName = awsrdsService.launchInstance();
175+
176+
//Add some wait for instance creation.
177+
178+
awsrdsService.listInstances();
179+
180+
awsrdsService.runJdbcTests();
181+
182+
awsrdsService.terminateInstance(instanceName);
183+
}
184+
}

aws/src/main/resources/db.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
db_hostname=<RDS EndPoint>
2+
db_username=username
3+
db_password=password
4+
db_database=mydb

0 commit comments

Comments
 (0)