Skip to content

Commit ca78752

Browse files
committed
Merge remote-tracking branch 'upstream/master' into BAEL-1754
2 parents 333168f + d2ee505 commit ca78752

52 files changed

Lines changed: 2104 additions & 33 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.hashtable;
2+
3+
public class Word {
4+
private String name;
5+
6+
public Word(String name) {
7+
this.name = name;
8+
}
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public boolean equals(Object o) {
15+
if (o == this)
16+
return true;
17+
if (!(o instanceof Word))
18+
return false;
19+
20+
Word word = (Word) o;
21+
return word.getName().equals(this.name) ? true : false;
22+
23+
}
24+
25+
public int hashCode() {
26+
return name.hashCode();
27+
}
28+
}
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package com.baeldung.hashtable;
2+
3+
import java.util.ConcurrentModificationException;
4+
import java.util.Enumeration;
5+
import java.util.Hashtable;
6+
import java.util.Iterator;
7+
import java.util.Map;
8+
9+
import static org.junit.Assert.*;
10+
import static org.hamcrest.Matchers.*;
11+
import org.junit.Test;
12+
13+
public class HashtableUnitTest {
14+
15+
@Test
16+
public void whenPutAndGet_thenReturnsValue() {
17+
Hashtable<Word, String> table = new Hashtable<Word, String>();
18+
19+
Word word = new Word("cat");
20+
table.put(word, "an animal");
21+
22+
String definition = table.get(word);
23+
24+
assertEquals("an animal", definition);
25+
26+
definition = table.remove(word);
27+
28+
assertEquals("an animal", definition);
29+
}
30+
31+
@Test
32+
public void whenThesameInstanceOfKey_thenReturnsValue() {
33+
Hashtable<Word, String> table = new Hashtable<Word, String>();
34+
Word word = new Word("cat");
35+
table.put(word, "an animal");
36+
String extracted = table.get(word);
37+
assertEquals("an animal", extracted);
38+
}
39+
40+
@Test
41+
public void whenEqualsOverridden_thenReturnsValue() {
42+
Hashtable<Word, String> table = new Hashtable<Word, String>();
43+
Word word = new Word("cat");
44+
table.put(word, "an animal");
45+
String extracted = table.get(new Word("cat"));
46+
assertEquals("an animal", extracted);
47+
}
48+
49+
@Test(expected = NullPointerException.class)
50+
public void whenNullKey_thenException() {
51+
Hashtable<Word, String> table = new Hashtable<Word, String>();
52+
table.put(null, "an animal");
53+
}
54+
55+
@Test(expected = ConcurrentModificationException.class)
56+
public void whenIterate_thenFailFast() {
57+
58+
Hashtable<Word, String> table = new Hashtable<Word, String>();
59+
table.put(new Word("cat"), "an animal");
60+
table.put(new Word("dog"), "another animal");
61+
62+
Iterator<Word> it = table.keySet().iterator();
63+
System.out.println("iterator created");
64+
65+
table.remove(new Word("dog"));
66+
System.out.println("element removed");
67+
68+
while (it.hasNext()) {
69+
Word key = it.next();
70+
System.out.println(table.get(key));
71+
}
72+
}
73+
74+
@Test
75+
public void whenEnumerate_thenNotFailFast() {
76+
77+
Hashtable<Word, String> table = new Hashtable<Word, String>();
78+
table.put(new Word("1"), "one");
79+
table.put(new Word("2"), "two");
80+
table.put(new Word("3"), "three");
81+
table.put(new Word("4"), "four");
82+
table.put(new Word("5"), "five");
83+
table.put(new Word("6"), "six");
84+
table.put(new Word("7"), "seven");
85+
table.put(new Word("8"), "eight");
86+
87+
Enumeration<Word> enumKey = table.keys();
88+
System.out.println("Enumeration created");
89+
table.remove(new Word("1"));
90+
System.out.println("element removed");
91+
while (enumKey.hasMoreElements()) {
92+
Word key = enumKey.nextElement();
93+
System.out.println(table.get(key));
94+
}
95+
}
96+
97+
@Test
98+
public void whenAddElements_thenIterationOrderUnpredicable() {
99+
100+
Hashtable<Word, String> table = new Hashtable<Word, String>();
101+
table.put(new Word("1"), "one");
102+
table.put(new Word("2"), "two");
103+
table.put(new Word("3"), "three");
104+
table.put(new Word("4"), "four");
105+
table.put(new Word("5"), "five");
106+
table.put(new Word("6"), "six");
107+
table.put(new Word("7"), "seven");
108+
table.put(new Word("8"), "eight");
109+
110+
Iterator<Map.Entry<Word, String>> it = table.entrySet().iterator();
111+
while (it.hasNext()) {
112+
Map.Entry<Word, String> entry = it.next();
113+
System.out.println(entry.getValue());
114+
}
115+
}
116+
117+
@Test
118+
public void whenGetOrDefault_thenDefaultGot() {
119+
120+
Hashtable<Word, String> table = new Hashtable<Word, String>();
121+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
122+
Word key = new Word("dog");
123+
String definition;
124+
125+
// old way
126+
/* if (table.containsKey(key)) {
127+
definition = table.get(key);
128+
} else {
129+
definition = "not found";
130+
}*/
131+
132+
// new way
133+
definition = table.getOrDefault(key, "not found");
134+
135+
assertThat(definition, is("not found"));
136+
}
137+
138+
@Test
139+
public void whenPutifAbsent_thenNotRewritten() {
140+
141+
Hashtable<Word, String> table = new Hashtable<Word, String>();
142+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
143+
144+
String definition = "an animal";
145+
// old way
146+
/* if (!table.containsKey(new Word("cat"))) {
147+
table.put(new Word("cat"), definition);
148+
}*/
149+
// new way
150+
table.putIfAbsent(new Word("cat"), definition);
151+
152+
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
153+
}
154+
155+
@Test
156+
public void whenRemovePair_thenCheckKeyAndValue() {
157+
158+
Hashtable<Word, String> table = new Hashtable<Word, String>();
159+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
160+
161+
// old way
162+
/* if (table.get(new Word("cat")).equals("an animal")) {
163+
table.remove(new Word("cat"));
164+
}*/
165+
166+
// new way
167+
boolean result = table.remove(new Word("cat"), "an animal");
168+
169+
assertThat(result, is(false));
170+
}
171+
172+
@Test
173+
public void whenReplacePair_thenValueChecked() {
174+
175+
Hashtable<Word, String> table = new Hashtable<Word, String>();
176+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
177+
178+
String definition = "an animal";
179+
180+
// old way
181+
/* if (table.containsKey(new Word("cat")) && table.get(new Word("cat")).equals("a small domesticated carnivorous mammal")) {
182+
table.put(new Word("cat"), definition);
183+
}*/
184+
// new way
185+
table.replace(new Word("cat"), "a small domesticated carnivorous mammal", definition);
186+
187+
assertThat(table.get(new Word("cat")), is("an animal"));
188+
189+
}
190+
191+
@Test
192+
public void whenKeyIsAbsent_thenNotRewritten() {
193+
194+
Hashtable<Word, String> table = new Hashtable<Word, String>();
195+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
196+
197+
// old way
198+
/* if (!table.containsKey(cat)) {
199+
String definition = "an animal";// calculate
200+
table.put(new Word("cat"), definition);
201+
}
202+
*/
203+
// new way
204+
205+
table.computeIfAbsent(new Word("cat"), key -> "an animal");
206+
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
207+
208+
}
209+
210+
@Test
211+
public void whenKeyIsPresent_thenComputeIfPresent() {
212+
213+
Hashtable<Word, String> table = new Hashtable<Word, String>();
214+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
215+
216+
Word cat = new Word("cat");
217+
// old way
218+
/* if (table.containsKey(cat)) {
219+
String concatination = cat.getName() + " - " + table.get(cat);
220+
table.put(cat, concatination);
221+
}*/
222+
223+
// new way
224+
table.computeIfPresent(cat, (key, value) -> key.getName() + " - " + value);
225+
226+
assertThat(table.get(cat), is("cat - a small domesticated carnivorous mammal"));
227+
228+
}
229+
230+
@Test
231+
public void whenCompute_thenForAllKeys() {
232+
233+
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
234+
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
235+
for (String animal : animals) {
236+
table.compute(animal, (key, value) -> (value == null ? Integer.valueOf(1) : Integer.valueOf(value) + 1));
237+
}
238+
assertThat(table.values(), hasItems(2, 2, 2, 1));
239+
240+
}
241+
242+
@Test
243+
public void whenInsteadOfCompute_thenMerge() {
244+
245+
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
246+
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
247+
for (String animal : animals) {
248+
table.merge(animal, 1, (oldValue, value) -> (oldValue + value));
249+
}
250+
assertThat(table.values(), hasItems(2, 2, 2, 1));
251+
}
252+
253+
@Test
254+
public void whenForeach_thenIterate() {
255+
256+
Hashtable<Word, String> table = new Hashtable<Word, String>();
257+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
258+
table.put(new Word("dog"), "another animal");
259+
table.forEach((k, v) -> System.out.println(k.getName() + " - " + v)
260+
261+
);
262+
}
263+
264+
@Test
265+
public void whenReplaceall_thenNoIterationNeeded() {
266+
267+
Hashtable<Word, String> table = new Hashtable<Word, String>();
268+
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
269+
table.put(new Word("dog"), "another animal");
270+
table.replaceAll((k, v) -> k.getName() + " - " + v);
271+
272+
assertThat(table.values(), hasItems("cat - a small domesticated carnivorous mammal", "dog - another animal"));
273+
}
274+
}

‎core-java/README.md‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,14 @@
7474
- [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string)
7575
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
7676
- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator)
77-
- [Singletons in Java](http://www.baeldung.com/java-singleton)
7877
- [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws)
7978
- [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded)
8079
- [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer)
8180
- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int)
82-
- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns)
8381
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
8482
- [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static)
8583
- [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array)
8684
- [Guide to Java String Pool](http://www.baeldung.com/java-string-pool)
87-
- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns)
8885
- [Quick Example - Comparator vs Comparable in Java](http://www.baeldung.com/java-comparator-comparable)
8986
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
9087
- [The Java continue and break Keywords](http://www.baeldung.com/java-continue-and-break)
@@ -116,8 +113,6 @@
116113
- [Comparing Strings in Java](http://www.baeldung.com/java-compare-strings)
117114
- [Guide to Inheritance in Java](http://www.baeldung.com/java-inheritance)
118115
- [Guide to Externalizable Interface in Java](http://www.baeldung.com/java-externalizable)
119-
- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern)
120-
- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight)
121116
- [Object Type Casting in Java](http://www.baeldung.com/java-type-casting)
122117
- [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat)
123118
- [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os)
@@ -136,7 +131,6 @@
136131
- [Class Loaders in Java](http://www.baeldung.com/java-classloaders)
137132
- [Find Sum and Average in a Java Array](http://www.baeldung.com/java-array-sum-average)
138133
- [Java List UnsupportedOperationException](http://www.baeldung.com/java-list-unsupported-operation-exception)
139-
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
140134
- [Type Erasure in Java Explained](http://www.baeldung.com/java-type-erasure)
141135
- [BigDecimal and BigInteger in Java](http://www.baeldung.com/java-bigdecimal-biginteger)
142136
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
@@ -145,5 +139,5 @@
145139
- [Sending Emails with Java](http://www.baeldung.com/java-email)
146140
- [Introduction to SSL in Java](http://www.baeldung.com/java-ssl)
147141
- [Java KeyStore API](http://www.baeldung.com/java-keystore)
148-
- [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking)
142+
- [Using Java Assertions](http://www.baeldung.com/java-assert)
149143
- [Guide to Java Clock Class](http://www.baeldung.com/java-clock)

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
package com.baeldung.hibernate;
22

3-
import com.baeldung.hibernate.pojo.Employee;
4-
import com.baeldung.hibernate.pojo.EntityDescription;
5-
import com.baeldung.hibernate.pojo.OrderEntry;
6-
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
7-
import com.baeldung.hibernate.pojo.OrderEntryPK;
8-
import com.baeldung.hibernate.pojo.PointEntity;
9-
import com.baeldung.hibernate.pojo.PolygonEntity;
10-
import com.baeldung.hibernate.pojo.Product;
11-
import com.baeldung.hibernate.pojo.Phone;
12-
import com.baeldung.hibernate.pojo.TemporalValues;
13-
import com.baeldung.hibernate.pojo.Course;
14-
import com.baeldung.hibernate.pojo.Student;
15-
import com.baeldung.hibernate.pojo.User;
16-
import com.baeldung.hibernate.pojo.UserProfile;
17-
import com.baeldung.hibernate.pojo.inheritance.Animal;
18-
import com.baeldung.hibernate.pojo.inheritance.Bag;
19-
import com.baeldung.hibernate.pojo.inheritance.Book;
20-
import com.baeldung.hibernate.pojo.inheritance.Car;
21-
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
22-
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
23-
import com.baeldung.hibernate.pojo.inheritance.Pen;
24-
import com.baeldung.hibernate.pojo.inheritance.Person;
25-
import com.baeldung.hibernate.pojo.inheritance.Pet;
26-
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
27-
3+
import com.baeldung.hibernate.pessimisticlocking.Individual;
4+
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
5+
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
6+
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
7+
import com.baeldung.hibernate.pojo.*;
8+
import com.baeldung.hibernate.pojo.Person;
9+
import com.baeldung.hibernate.pojo.inheritance.*;
2810
import org.apache.commons.lang3.StringUtils;
2911
import org.hibernate.SessionFactory;
3012
import org.hibernate.boot.Metadata;
@@ -82,6 +64,12 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
8264
metadataSources.addAnnotatedClass(PointEntity.class);
8365
metadataSources.addAnnotatedClass(PolygonEntity.class);
8466
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
67+
metadataSources.addAnnotatedClass(Individual.class);
68+
metadataSources.addAnnotatedClass(PessimisticLockingEmployee.class);
69+
metadataSources.addAnnotatedClass(PessimisticLockingStudent.class);
70+
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
71+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
72+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
8573

8674
Metadata metadata = metadataSources.buildMetadata();
8775
return metadata.getSessionFactoryBuilder()

0 commit comments

Comments
 (0)