Skip to content

Commit 80f92e0

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 7230682 + 7b256aa commit 80f92e0

521 files changed

Lines changed: 6798 additions & 4456 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.algorithms.rectanglesoverlap;
2+
3+
public class Point {
4+
5+
private int x;
6+
private int y;
7+
8+
public Point(int x, int y) {
9+
this.x = x;
10+
this.y = y;
11+
}
12+
13+
public int getX() {
14+
return x;
15+
}
16+
17+
public void setX(int x) {
18+
this.x = x;
19+
}
20+
21+
public int getY() {
22+
return y;
23+
}
24+
25+
public void setY(int y) {
26+
this.y = y;
27+
}
28+
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.algorithms.rectanglesoverlap;
2+
3+
public class Rectangle {
4+
5+
private Point bottomLeft;
6+
private Point topRight;
7+
8+
public Rectangle(Point bottomLeft, Point topRight) {
9+
this.bottomLeft = bottomLeft;
10+
this.topRight = topRight;
11+
}
12+
13+
public Point getBottomLeft() {
14+
return bottomLeft;
15+
}
16+
17+
public void setBottomLeft(Point bottomLeft) {
18+
this.bottomLeft = bottomLeft;
19+
}
20+
21+
public Point getTopRight() {
22+
return topRight;
23+
}
24+
25+
public void setTopRight(Point topRight) {
26+
this.topRight = topRight;
27+
}
28+
29+
public boolean isOverlapping(Rectangle other) {
30+
// one rectangle is to the top of the other
31+
if (this.topRight.getY() < other.bottomLeft.getY() || this.bottomLeft.getY() > other.topRight.getY()) {
32+
return false;
33+
}
34+
// one rectangle is to the left of the other
35+
if (this.topRight.getX() < other.bottomLeft.getX() || this.bottomLeft.getX() > other.topRight.getX()) {
36+
return false;
37+
}
38+
return true;
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.algorithms.rectanglesoverlap;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertFalse;
5+
import org.junit.Test;
6+
7+
import com.baeldung.algorithms.rectanglesoverlap.Point;
8+
import com.baeldung.algorithms.rectanglesoverlap.Rectangle;
9+
10+
public class RectangleUnitTest {
11+
12+
@Test
13+
public void givenTwoOverlappingRectangles_whenisOverlappingCalled_shouldReturnTrue() {
14+
Rectangle rectangle1 = new Rectangle(new Point(2, 1), new Point(4, 3));
15+
Rectangle rectangle2 = new Rectangle(new Point(1, 1), new Point(6, 4));
16+
assertTrue(rectangle1.isOverlapping(rectangle2));
17+
18+
rectangle1 = new Rectangle(new Point(-5, -2), new Point(2, 3));
19+
rectangle2 = new Rectangle(new Point(-2, -1), new Point(5, 2));
20+
assertTrue(rectangle1.isOverlapping(rectangle2));
21+
22+
rectangle1 = new Rectangle(new Point(-5, 1), new Point(2, 4));
23+
rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5));
24+
assertTrue(rectangle1.isOverlapping(rectangle2));
25+
}
26+
27+
@Test
28+
public void givenTwoNonOverlappingRectangles_whenisOverlappingCalled_shouldReturnFalse() {
29+
Rectangle rectangle1 = new Rectangle(new Point(-5, 1), new Point(-3, 4));
30+
Rectangle rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5));
31+
assertFalse(rectangle1.isOverlapping(rectangle2));
32+
33+
rectangle1 = new Rectangle(new Point(-5, 1), new Point(3, 4));
34+
rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, -1));
35+
assertFalse(rectangle1.isOverlapping(rectangle2));
36+
37+
rectangle1 = new Rectangle(new Point(-2, 1), new Point(0, 3));
38+
rectangle2 = new Rectangle(new Point(3, 1), new Point(5, 4));
39+
assertFalse(rectangle1.isOverlapping(rectangle2));
40+
}
41+
42+
}

‎apache-cxf/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
- [Apache CXF Support for RESTful Web Services](http://www.baeldung.com/apache-cxf-rest-api)
44
- [A Guide to Apache CXF with Spring](http://www.baeldung.com/apache-cxf-with-spring)
55
- [Introduction to Apache CXF](http://www.baeldung.com/introduction-to-apache-cxf)
6+
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)

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

Lines changed: 1 addition & 1 deletion
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-lambda/pom.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
<gson.version>2.8.2</gson.version>
9696
<aws-java-sdk.version>1.11.241</aws-java-sdk.version>
9797
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
98-
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
9998
</properties>
10099

101100
</project>

‎aws/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target/
2+
.idea/

‎aws/pom.xml‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,6 @@
100100
</execution>
101101
</executions>
102102
</plugin>
103-
104-
<plugin>
105-
<groupId>org.apache.maven.plugins</groupId>
106-
<artifactId>maven-dependency-plugin</artifactId>
107-
<version>${maven-dependency-plugin.version}</version>
108-
<executions>
109-
<execution>
110-
<id>copy-dependencies</id>
111-
<phase>test-compile</phase>
112-
<goals>
113-
<goal>copy-dependencies</goal>
114-
</goals>
115-
<configuration>
116-
<includeScope>test</includeScope>
117-
<includeTypes>so,dll,dylib</includeTypes>
118-
<outputDirectory>${project.basedir}/native-libs</outputDirectory>
119-
</configuration>
120-
</execution>
121-
</executions>
122-
</plugin>
123103
</plugins>
124104
</build>
125105

@@ -144,7 +124,6 @@
144124
<commons-codec-version>1.10.L001</commons-codec-version>
145125
<jets3t-version>0.9.4.0006L</jets3t-version>
146126
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
147-
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
148127
</properties>
149128

150129
</project>
Lines changed: 184 additions & 0 deletions
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+
}
Lines changed: 4 additions & 0 deletions
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)