Skip to content

Commit 604f5b1

Browse files
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-1525-Check-if-a-String-is-a-palindrome
2 parents 581e859 + 372cba1 commit 604f5b1

41 files changed

Lines changed: 1517 additions & 25 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.

‎core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.concurrent.CompletableFuture;
1313
import java.util.concurrent.ExecutionException;
14+
import java.util.concurrent.ExecutorService;
1415
import java.util.concurrent.Executors;
1516
import java.util.stream.Collectors;
1617

@@ -116,18 +117,20 @@ public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyn
116117
.GET()
117118
.build();
118119

120+
ExecutorService executorService = Executors.newFixedThreadPool(2);
121+
119122
CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder()
120-
.executor(Executors.newFixedThreadPool(2))
123+
.executor(executorService)
121124
.build()
122125
.sendAsync(request, HttpResponse.BodyHandler.asString());
123126

124127
CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder()
125-
.executor(Executors.newFixedThreadPool(2))
128+
.executor(executorService)
126129
.build()
127130
.sendAsync(request, HttpResponse.BodyHandler.asString());
128131

129132
CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder()
130-
.executor(Executors.newFixedThreadPool(2))
133+
.executor(executorService)
131134
.build()
132135
.sendAsync(request, HttpResponse.BodyHandler.asString());
133136

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
package com.baeldung.javac;
22

3-
import java.io.Serializable;
43
import java.util.ArrayList;
54
import java.util.List;
65

7-
public class Data implements Serializable {
8-
static List<String> textList = new ArrayList();
6+
public class Data {
7+
List<String> textList = new ArrayList();
98

10-
private static void addText() {
11-
textList.add("baeldung");
12-
textList.add(".");
13-
textList.add("com");
9+
public void addText(String text) {
10+
textList.add(text);
1411
}
1512

1613
public List getTextList() {
17-
this.addText();
18-
List<String> result = new ArrayList<String>();
19-
String firstElement = (String) textList.get(0);
20-
switch (firstElement) {
21-
case "baeldung":
22-
result.add("baeldung");
23-
case "com":
24-
result.add("com");
25-
}
26-
return result;
14+
return this.textList;
2715
}
28-
}
16+
}

‎hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
8181
metadataSources.addAnnotatedClass(Bag.class);
8282
metadataSources.addAnnotatedClass(PointEntity.class);
8383
metadataSources.addAnnotatedClass(PolygonEntity.class);
84+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
8485

8586
Metadata metadata = metadataSources.buildMetadata();
8687
return metadata.getSessionFactoryBuilder()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.hibernate.converters;
2+
3+
import javax.persistence.AttributeConverter;
4+
import javax.persistence.Converter;
5+
6+
import com.baeldung.hibernate.pojo.PersonName;
7+
8+
@Converter
9+
public class PersonNameConverter implements AttributeConverter<PersonName, String> {
10+
11+
private static final String SEPARATOR = ", ";
12+
13+
@Override
14+
public String convertToDatabaseColumn(PersonName person) {
15+
StringBuilder sb = new StringBuilder();
16+
if (person.getSurname() != null) {
17+
sb.append(person.getSurname());
18+
sb.append(SEPARATOR);
19+
}
20+
21+
if (person.getName() != null) {
22+
sb.append(person.getName());
23+
}
24+
25+
return sb.toString();
26+
}
27+
28+
@Override
29+
public PersonName convertToEntityAttribute(String dbPerson) {
30+
String[] pieces = dbPerson.split(SEPARATOR);
31+
32+
if (pieces == null || pieces.length != 2) {
33+
return null;
34+
}
35+
36+
PersonName personName = new PersonName();
37+
personName.setSurname(pieces[0]);
38+
personName.setName(pieces[1]);
39+
40+
return personName;
41+
}
42+
43+
}

‎hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.hibernate.Transaction;
1010
import org.hibernate.type.Type;
1111

12-
public class CustomInterceptorImpl implements Interceptor {
12+
public class CustomInterceptorImpl implements Interceptor, Serializable {
1313

1414
@Override
1515
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Convert;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
8+
import com.baeldung.hibernate.converters.PersonNameConverter;
9+
10+
@Entity(name = "PersonTable")
11+
public class Person {
12+
13+
@Id
14+
@GeneratedValue
15+
private Long id;
16+
17+
@Convert(converter = PersonNameConverter.class)
18+
private PersonName personName;
19+
20+
public PersonName getPersonName() {
21+
return personName;
22+
}
23+
24+
public void setPersonName(PersonName personName) {
25+
this.personName = personName;
26+
}
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import java.io.Serializable;
4+
5+
public class PersonName implements Serializable {
6+
7+
private static final long serialVersionUID = 7883094644631050150L;
8+
9+
private String name;
10+
11+
private String surname;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public String getSurname() {
22+
return surname;
23+
}
24+
25+
public void setSurname(String surname) {
26+
this.surname = surname;
27+
}
28+
29+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.hibernate.converter;
2+
3+
import java.io.IOException;
4+
5+
import org.hibernate.Session;
6+
import org.hibernate.Transaction;
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import com.baeldung.hibernate.HibernateUtil;
12+
import com.baeldung.hibernate.pojo.Person;
13+
import com.baeldung.hibernate.pojo.PersonName;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class PersonNameConverterTest {
18+
19+
private Session session;
20+
private Transaction transaction;
21+
22+
@Before
23+
public void setUp() throws IOException {
24+
session = HibernateUtil.getSessionFactory()
25+
.openSession();
26+
transaction = session.beginTransaction();
27+
28+
session.createNativeQuery("delete from personTable")
29+
.executeUpdate();
30+
31+
transaction.commit();
32+
transaction = session.beginTransaction();
33+
}
34+
35+
@After
36+
public void tearDown() {
37+
transaction.rollback();
38+
session.close();
39+
}
40+
41+
@Test
42+
public void givenPersonName_WhenSaving_ThenNameAndSurnameConcat() {
43+
final String name = "name";
44+
final String surname = "surname";
45+
46+
PersonName personName = new PersonName();
47+
personName.setName(name);
48+
personName.setSurname(surname);
49+
50+
Person person = new Person();
51+
person.setPersonName(personName);
52+
53+
Long id = (Long) session.save(person);
54+
55+
session.flush();
56+
session.clear();
57+
58+
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
59+
.setParameter("id", id)
60+
.getSingleResult();
61+
62+
assertEquals(surname + ", " + name, dbPersonName);
63+
64+
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
65+
.setParameter("id", id)
66+
.getSingleResult();
67+
68+
assertEquals(dbPerson.getPersonName()
69+
.getName(), name);
70+
assertEquals(dbPerson.getPersonName()
71+
.getSurname(), surname);
72+
}
73+
74+
}

‎influxdb/README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
### Relevant Article:
44
- [Introduction to using InfluxDB with Java](http://www.baeldung.com/using-influxdb-with-java/)
5-
- [Using InfluxDB with Java](http://www.baeldung.com/java-influxdb)
65

76
### Overview
87
This Maven project contains the Java code for the article linked above.

‎influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.influxdb.impl.InfluxDBResultMapper;
99
import org.junit.Test;
1010

11-
import java.io.IOException;
1211
import java.util.List;
1312
import java.util.concurrent.TimeUnit;
1413

@@ -103,7 +102,7 @@ public void whenPointsWrittenPointsExists() throws Exception {
103102
// another brief pause.
104103
Thread.sleep(10);
105104

106-
List<MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
105+
List<com.baeldung.influxdb.MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
107106

108107
assertEquals(10, memoryPointList.size());
109108

0 commit comments

Comments
 (0)