Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
2d54c25de3
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.hashtable;
|
||||
|
||||
public class Word {
|
||||
private String name;
|
||||
|
||||
public Word(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof Word))
|
||||
return false;
|
||||
|
||||
Word word = (Word) o;
|
||||
return word.getName().equals(this.name) ? true : false;
|
||||
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,274 @@
|
|||
package com.baeldung.hashtable;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HashtableUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenPutAndGet_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
|
||||
String definition = table.get(word);
|
||||
|
||||
assertEquals("an animal", definition);
|
||||
|
||||
definition = table.remove(word);
|
||||
|
||||
assertEquals("an animal", definition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenThesameInstanceOfKey_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
String extracted = table.get(word);
|
||||
assertEquals("an animal", extracted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEqualsOverridden_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
String extracted = table.get(new Word("cat"));
|
||||
assertEquals("an animal", extracted);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenNullKey_thenException() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(null, "an animal");
|
||||
}
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenIterate_thenFailFast() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "an animal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
|
||||
Iterator<Word> it = table.keySet().iterator();
|
||||
System.out.println("iterator created");
|
||||
|
||||
table.remove(new Word("dog"));
|
||||
System.out.println("element removed");
|
||||
|
||||
while (it.hasNext()) {
|
||||
Word key = it.next();
|
||||
System.out.println(table.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEnumerate_thenNotFailFast() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("1"), "one");
|
||||
table.put(new Word("2"), "two");
|
||||
table.put(new Word("3"), "three");
|
||||
table.put(new Word("4"), "four");
|
||||
table.put(new Word("5"), "five");
|
||||
table.put(new Word("6"), "six");
|
||||
table.put(new Word("7"), "seven");
|
||||
table.put(new Word("8"), "eight");
|
||||
|
||||
Enumeration<Word> enumKey = table.keys();
|
||||
System.out.println("Enumeration created");
|
||||
table.remove(new Word("1"));
|
||||
System.out.println("element removed");
|
||||
while (enumKey.hasMoreElements()) {
|
||||
Word key = enumKey.nextElement();
|
||||
System.out.println(table.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddElements_thenIterationOrderUnpredicable() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("1"), "one");
|
||||
table.put(new Word("2"), "two");
|
||||
table.put(new Word("3"), "three");
|
||||
table.put(new Word("4"), "four");
|
||||
table.put(new Word("5"), "five");
|
||||
table.put(new Word("6"), "six");
|
||||
table.put(new Word("7"), "seven");
|
||||
table.put(new Word("8"), "eight");
|
||||
|
||||
Iterator<Map.Entry<Word, String>> it = table.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Word, String> entry = it.next();
|
||||
System.out.println(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetOrDefault_thenDefaultGot() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
Word key = new Word("dog");
|
||||
String definition;
|
||||
|
||||
// old way
|
||||
/* if (table.containsKey(key)) {
|
||||
definition = table.get(key);
|
||||
} else {
|
||||
definition = "not found";
|
||||
}*/
|
||||
|
||||
// new way
|
||||
definition = table.getOrDefault(key, "not found");
|
||||
|
||||
assertThat(definition, is("not found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPutifAbsent_thenNotRewritten() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
String definition = "an animal";
|
||||
// old way
|
||||
/* if (!table.containsKey(new Word("cat"))) {
|
||||
table.put(new Word("cat"), definition);
|
||||
}*/
|
||||
// new way
|
||||
table.putIfAbsent(new Word("cat"), definition);
|
||||
|
||||
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemovePair_thenCheckKeyAndValue() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
// old way
|
||||
/* if (table.get(new Word("cat")).equals("an animal")) {
|
||||
table.remove(new Word("cat"));
|
||||
}*/
|
||||
|
||||
// new way
|
||||
boolean result = table.remove(new Word("cat"), "an animal");
|
||||
|
||||
assertThat(result, is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplacePair_thenValueChecked() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
String definition = "an animal";
|
||||
|
||||
// old way
|
||||
/* if (table.containsKey(new Word("cat")) && table.get(new Word("cat")).equals("a small domesticated carnivorous mammal")) {
|
||||
table.put(new Word("cat"), definition);
|
||||
}*/
|
||||
// new way
|
||||
table.replace(new Word("cat"), "a small domesticated carnivorous mammal", definition);
|
||||
|
||||
assertThat(table.get(new Word("cat")), is("an animal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyIsAbsent_thenNotRewritten() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
// old way
|
||||
/* if (!table.containsKey(cat)) {
|
||||
String definition = "an animal";// calculate
|
||||
table.put(new Word("cat"), definition);
|
||||
}
|
||||
*/
|
||||
// new way
|
||||
|
||||
table.computeIfAbsent(new Word("cat"), key -> "an animal");
|
||||
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyIsPresent_thenComputeIfPresent() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
Word cat = new Word("cat");
|
||||
// old way
|
||||
/* if (table.containsKey(cat)) {
|
||||
String concatination = cat.getName() + " - " + table.get(cat);
|
||||
table.put(cat, concatination);
|
||||
}*/
|
||||
|
||||
// new way
|
||||
table.computeIfPresent(cat, (key, value) -> key.getName() + " - " + value);
|
||||
|
||||
assertThat(table.get(cat), is("cat - a small domesticated carnivorous mammal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompute_thenForAllKeys() {
|
||||
|
||||
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
|
||||
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
|
||||
for (String animal : animals) {
|
||||
table.compute(animal, (key, value) -> (value == null ? Integer.valueOf(1) : Integer.valueOf(value) + 1));
|
||||
}
|
||||
assertThat(table.values(), hasItems(2, 2, 2, 1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsteadOfCompute_thenMerge() {
|
||||
|
||||
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
|
||||
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
|
||||
for (String animal : animals) {
|
||||
table.merge(animal, 1, (oldValue, value) -> (oldValue + value));
|
||||
}
|
||||
assertThat(table.values(), hasItems(2, 2, 2, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenForeach_thenIterate() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
table.forEach((k, v) -> System.out.println(k.getName() + " - " + v)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceall_thenNoIterationNeeded() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
table.replaceAll((k, v) -> k.getName() + " - " + v);
|
||||
|
||||
assertThat(table.values(), hasItems("cat - a small domesticated carnivorous mammal", "dog - another animal"));
|
||||
}
|
||||
}
|
|
@ -74,17 +74,14 @@
|
|||
- [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string)
|
||||
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||
- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator)
|
||||
- [Singletons in Java](http://www.baeldung.com/java-singleton)
|
||||
- [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws)
|
||||
- [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded)
|
||||
- [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer)
|
||||
- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int)
|
||||
- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns)
|
||||
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
|
||||
- [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static)
|
||||
- [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array)
|
||||
- [Guide to Java String Pool](http://www.baeldung.com/java-string-pool)
|
||||
- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns)
|
||||
- [Quick Example - Comparator vs Comparable in Java](http://www.baeldung.com/java-comparator-comparable)
|
||||
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
|
||||
- [The Java continue and break Keywords](http://www.baeldung.com/java-continue-and-break)
|
||||
|
@ -116,8 +113,6 @@
|
|||
- [Comparing Strings in Java](http://www.baeldung.com/java-compare-strings)
|
||||
- [Guide to Inheritance in Java](http://www.baeldung.com/java-inheritance)
|
||||
- [Guide to Externalizable Interface in Java](http://www.baeldung.com/java-externalizable)
|
||||
- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern)
|
||||
- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight)
|
||||
- [Object Type Casting in Java](http://www.baeldung.com/java-type-casting)
|
||||
- [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat)
|
||||
- [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os)
|
||||
|
@ -136,7 +131,6 @@
|
|||
- [Class Loaders in Java](http://www.baeldung.com/java-classloaders)
|
||||
- [Find Sum and Average in a Java Array](http://www.baeldung.com/java-array-sum-average)
|
||||
- [Java List UnsupportedOperationException](http://www.baeldung.com/java-list-unsupported-operation-exception)
|
||||
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
|
||||
- [Type Erasure in Java Explained](http://www.baeldung.com/java-type-erasure)
|
||||
- [BigDecimal and BigInteger in Java](http://www.baeldung.com/java-bigdecimal-biginteger)
|
||||
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
|
||||
|
@ -145,4 +139,5 @@
|
|||
- [Sending Emails with Java](http://www.baeldung.com/java-email)
|
||||
- [Introduction to SSL in Java](http://www.baeldung.com/java-ssl)
|
||||
- [Java KeyStore API](http://www.baeldung.com/java-keystore)
|
||||
- [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking)
|
||||
- [Using Java Assertions](http://www.baeldung.com/java-assert)
|
||||
- [Guide to Java Clock Class](http://www.baeldung.com/java-clock)
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public interface Color {
|
||||
String getColor();
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public interface Polygon {
|
||||
String getType();
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package com.baeldung.numberofdigits;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class NumberOfDigitsDriver {
|
||||
private static NumberOfDigits numberOfDigits;
|
||||
|
||||
private static Logger LOG = Logger.getLogger(NumberOfDigitsDriver.class);
|
||||
|
||||
static {
|
||||
numberOfDigits = new NumberOfDigits();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
|
||||
public class Benchmarking {
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(Benchmarking.class.getSimpleName())
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
|
||||
@State(Scope.Thread)
|
||||
public static class ExecutionPlan {
|
||||
public String number = Integer.toString(Integer.MAX_VALUE);
|
||||
public boolean isNumber = false;
|
||||
public IsNumeric isNumeric= new IsNumeric();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingCoreJava(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingCoreJava(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingRegularExpressions(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingRegularExpressions(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingNumberUtils_isCreatable(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingNumberUtils_isCreatable(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingNumberUtils_isParsable(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingNumberUtils_isParsable(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingStringUtils_isNumeric(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingStringUtils_isNumeric(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingStringUtils_isNumericSpace(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingStringUtils_isNumericSpace(plan.number);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
public class IsNumeric {
|
||||
public boolean usingCoreJava(String strNum) {
|
||||
try {
|
||||
double d = Double.parseDouble(strNum);
|
||||
} catch (NumberFormatException | NullPointerException nfe) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean usingRegularExpressions(String strNum) {
|
||||
return strNum.matches("-?\\d+(\\.\\d+)?");
|
||||
}
|
||||
|
||||
public boolean usingNumberUtils_isCreatable(String strNum) {
|
||||
return NumberUtils.isCreatable(strNum);
|
||||
}
|
||||
|
||||
public boolean usingNumberUtils_isParsable(String strNum) {
|
||||
return NumberUtils.isParsable(strNum);
|
||||
}
|
||||
|
||||
public boolean usingStringUtils_isNumeric(String strNum) {
|
||||
return StringUtils.isNumeric(strNum);
|
||||
}
|
||||
|
||||
public boolean usingStringUtils_isNumericSpace(String strNum) {
|
||||
return StringUtils.isNumericSpace(strNum);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class IsNumericDriver {
|
||||
private static IsNumeric isNumeric;
|
||||
private static Logger LOG = Logger.getLogger(IsNumericDriver.class);
|
||||
static {
|
||||
isNumeric =new IsNumeric();
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
LOG.info("Testing all methods...");
|
||||
|
||||
boolean res = isNumeric.usingCoreJava("1001");
|
||||
LOG.info("Using Core Java : " + res);
|
||||
|
||||
res = isNumeric.usingRegularExpressions("1001");
|
||||
LOG.info("Using Regular Expressions : " + res);
|
||||
|
||||
res =isNumeric.usingNumberUtils_isCreatable("1001");
|
||||
LOG.info("Using NumberUtils.isCreatable : " + res);
|
||||
|
||||
res =isNumeric.usingNumberUtils_isParsable("1001");
|
||||
LOG.info("Using NumberUtils.isParsable : " + res);
|
||||
|
||||
res =isNumeric.usingStringUtils_isNumeric("1001");
|
||||
LOG.info("Using StringUtils.isNumeric : " + res);
|
||||
|
||||
res =isNumeric.usingStringUtils_isNumericSpace("1001");
|
||||
LOG.info("Using StringUtils.isNumericSpace : " + res);
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CoreJavaIsNumericUnitTest {
|
||||
public static boolean isNumeric(String strNum) {
|
||||
try {
|
||||
double d = Double.parseDouble(strNum);
|
||||
} catch (NumberFormatException | NullPointerException nfe) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCoreJava_thenTrue() {
|
||||
// Valid Numbers
|
||||
assertThat(isNumeric("22")).isTrue();
|
||||
assertThat(isNumeric("5.05")).isTrue();
|
||||
assertThat(isNumeric("-200")).isTrue();
|
||||
assertThat(isNumeric("10.0d")).isTrue();
|
||||
assertThat(isNumeric(" 22 ")).isTrue();
|
||||
|
||||
// Invalid Numbers
|
||||
assertThat(isNumeric(null)).isFalse();
|
||||
assertThat(isNumeric("")).isFalse();
|
||||
assertThat(isNumeric("abc")).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NumberUtilsIsCreatableUnitTest {
|
||||
@Test
|
||||
public void givenApacheCommons_whenUsingIsParsable_thenTrue() {
|
||||
// Valid Numbers
|
||||
assertThat(NumberUtils.isCreatable("22")).isTrue();
|
||||
assertThat(NumberUtils.isCreatable("5.05")).isTrue();
|
||||
assertThat(NumberUtils.isCreatable("-200")).isTrue();
|
||||
assertThat(NumberUtils.isCreatable("10.0d")).isTrue();
|
||||
assertThat(NumberUtils.isCreatable("1000L")).isTrue();
|
||||
assertThat(NumberUtils.isCreatable("0xFF")).isTrue();
|
||||
assertThat(NumberUtils.isCreatable("07")).isTrue();
|
||||
assertThat(NumberUtils.isCreatable("2.99e+8")).isTrue();
|
||||
|
||||
// Invalid Numbers
|
||||
assertThat(NumberUtils.isCreatable(null)).isFalse();
|
||||
assertThat(NumberUtils.isCreatable("")).isFalse();
|
||||
assertThat(NumberUtils.isCreatable("abc")).isFalse();
|
||||
assertThat(NumberUtils.isCreatable(" 22 ")).isFalse();
|
||||
assertThat(NumberUtils.isCreatable("09")).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NumberUtilsIsParsableUnitTest {
|
||||
@Test
|
||||
public void givenApacheCommons_whenUsingIsParsable_thenTrue() {
|
||||
// Valid Numbers
|
||||
assertThat(NumberUtils.isParsable("22")).isTrue();
|
||||
assertThat(NumberUtils.isParsable("-23")).isTrue();
|
||||
assertThat(NumberUtils.isParsable("2.2")).isTrue();
|
||||
assertThat(NumberUtils.isParsable("09")).isTrue();
|
||||
|
||||
// Invalid Numbers
|
||||
assertThat(NumberUtils.isParsable(null)).isFalse();
|
||||
assertThat(NumberUtils.isParsable("")).isFalse();
|
||||
assertThat(NumberUtils.isParsable("6.2f")).isFalse();
|
||||
assertThat(NumberUtils.isParsable("9.8d")).isFalse();
|
||||
assertThat(NumberUtils.isParsable("22L")).isFalse();
|
||||
assertThat(NumberUtils.isParsable("0xFF")).isFalse();
|
||||
assertThat(NumberUtils.isParsable("2.99e+8")).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RegularExpressionsUnitTest {
|
||||
public static boolean isNumeric(String strNum) {
|
||||
return strNum.matches("-?\\d+(\\.\\d+)?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingRegularExpressions_thenTrue() {
|
||||
// Valid Numbers
|
||||
assertThat(isNumeric("22")).isTrue();
|
||||
assertThat(isNumeric("5.05")).isTrue();
|
||||
assertThat(isNumeric("-200")).isTrue();
|
||||
|
||||
// Invalid Numbers
|
||||
assertThat(isNumeric("abc")).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringUtilsIsNumericSpaceUnitTest {
|
||||
@Test
|
||||
public void givenApacheCommons_whenUsingIsNumericSpace_thenTrue() {
|
||||
// Valid Numbers
|
||||
assertThat(StringUtils.isNumericSpace("123")).isTrue();
|
||||
assertThat(StringUtils.isNumericSpace("١٢٣")).isTrue();
|
||||
assertThat(StringUtils.isNumericSpace("")).isTrue();
|
||||
assertThat(StringUtils.isNumericSpace(" ")).isTrue();
|
||||
assertThat(StringUtils.isNumericSpace("12 3")).isTrue();
|
||||
|
||||
// Invalid Numbers
|
||||
assertThat(StringUtils.isNumericSpace(null)).isFalse();
|
||||
assertThat(StringUtils.isNumericSpace("ab2c")).isFalse();
|
||||
assertThat(StringUtils.isNumericSpace("12.3")).isFalse();
|
||||
assertThat(StringUtils.isNumericSpace("-123")).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.stringisnumeric;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringUtilsIsNumericUnitTest {
|
||||
@Test
|
||||
public void givenApacheCommons_whenUsingIsNumeric_thenTrue() {
|
||||
// Valid Numbers
|
||||
assertThat(StringUtils.isNumeric("123")).isTrue();
|
||||
assertThat(StringUtils.isNumeric("١٢٣")).isTrue();
|
||||
assertThat(StringUtils.isNumeric("१२३")).isTrue();
|
||||
|
||||
// Invalid Numbers
|
||||
assertThat(StringUtils.isNumeric(null)).isFalse();
|
||||
assertThat(StringUtils.isNumeric("")).isFalse();
|
||||
assertThat(StringUtils.isNumeric(" ")).isFalse();
|
||||
assertThat(StringUtils.isNumeric("12 3")).isFalse();
|
||||
assertThat(StringUtils.isNumeric("ab2c")).isFalse();
|
||||
assertThat(StringUtils.isNumeric("12.3")).isFalse();
|
||||
assertThat(StringUtils.isNumeric("-123")).isFalse();
|
||||
}
|
||||
}
|
|
@ -63,6 +63,24 @@
|
|||
<artifactId>kotlinx-coroutines-core</artifactId>
|
||||
<version>${kotlinx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-api</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-subject-extension</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-junit-platform-engine</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nhaarman</groupId>
|
||||
<artifactId>mockito-kotlin</artifactId>
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.kotlin.spek
|
||||
|
||||
import com.baeldung.kotlin.junit5.Calculator
|
||||
import org.jetbrains.spek.api.dsl.describe
|
||||
import org.jetbrains.spek.api.dsl.it
|
||||
import org.jetbrains.spek.subject.SubjectSpek
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
|
||||
class CalculatorSubjectTest5 : SubjectSpek<Calculator>({
|
||||
subject { Calculator() }
|
||||
describe("A calculator") {
|
||||
describe("Addition") {
|
||||
val result = subject.add(3, 5)
|
||||
it("Produces the correct answer") {
|
||||
assertEquals(8, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.kotlin.spek
|
||||
|
||||
import com.baeldung.kotlin.junit5.Calculator
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.jetbrains.spek.api.dsl.describe
|
||||
import org.jetbrains.spek.api.dsl.given
|
||||
import org.jetbrains.spek.api.dsl.it
|
||||
import org.jetbrains.spek.api.dsl.on
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
|
||||
class CalculatorTest5 : Spek({
|
||||
given("A calculator") {
|
||||
val calculator = Calculator()
|
||||
on("Adding 3 and 5") {
|
||||
val result = calculator.add(3, 5)
|
||||
it("Produces 8") {
|
||||
assertEquals(8, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe("A calculator") {
|
||||
val calculator = Calculator()
|
||||
describe("Addition") {
|
||||
val result = calculator.add(3, 5)
|
||||
it("Produces the correct answer") {
|
||||
assertEquals(8, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
})
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.kotlin.spek
|
||||
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.jetbrains.spek.api.dsl.describe
|
||||
import org.jetbrains.spek.api.dsl.it
|
||||
import org.junit.jupiter.api.Assertions
|
||||
|
||||
class DataDrivenTest5 : Spek({
|
||||
describe("A data driven test") {
|
||||
mapOf(
|
||||
"hello" to "HELLO",
|
||||
"world" to "WORLD"
|
||||
).forEach { input, expected ->
|
||||
describe("Capitalising $input") {
|
||||
it("Correctly returns $expected") {
|
||||
Assertions.assertEquals(expected, input.toUpperCase())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.kotlin.spek
|
||||
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.jetbrains.spek.api.dsl.describe
|
||||
import org.jetbrains.spek.api.dsl.it
|
||||
|
||||
class GroupTest5 : Spek({
|
||||
describe("Outer group") {
|
||||
beforeEachTest {
|
||||
System.out.println("BeforeEachTest 0")
|
||||
}
|
||||
beforeGroup {
|
||||
System.out.println("BeforeGroup 0")
|
||||
}
|
||||
afterEachTest {
|
||||
System.out.println("AfterEachTest 0")
|
||||
}
|
||||
afterGroup {
|
||||
System.out.println("AfterGroup 0")
|
||||
}
|
||||
describe("Inner group 1") {
|
||||
beforeEachTest {
|
||||
System.out.println("BeforeEachTest 1")
|
||||
}
|
||||
beforeGroup {
|
||||
System.out.println("BeforeGroup 1")
|
||||
}
|
||||
afterEachTest {
|
||||
System.out.println("AfterEachTest 1")
|
||||
}
|
||||
afterGroup {
|
||||
System.out.println("AfterGroup 1")
|
||||
}
|
||||
it("Test 1") {
|
||||
System.out.println("Test 1")
|
||||
}
|
||||
it("Test 2") {
|
||||
System.out.println("Test 2")
|
||||
}
|
||||
}
|
||||
describe("Inner group 2") {
|
||||
beforeEachTest {
|
||||
System.out.println("BeforeEachTest 2")
|
||||
}
|
||||
beforeGroup {
|
||||
System.out.println("BeforeGroup 2")
|
||||
}
|
||||
afterEachTest {
|
||||
System.out.println("AfterEachTest 2")
|
||||
}
|
||||
afterGroup {
|
||||
System.out.println("AfterGroup 2")
|
||||
}
|
||||
it("Test 3") {
|
||||
System.out.println("Test 3")
|
||||
}
|
||||
it("Test 4") {
|
||||
System.out.println("Test 4")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
|
@ -32,6 +32,14 @@
|
|||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>java-hamcrest</artifactId>
|
||||
<version>${java-hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -52,6 +60,7 @@
|
|||
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -12,6 +12,7 @@ import static org.hamcrest.Matchers.hasItems;
|
|||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.Matchers.emptyIterable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
@ -57,6 +58,12 @@ public class HamcrestExamplesUnitTest {
|
|||
final List<String> collection = Lists.newArrayList();
|
||||
assertThat(collection, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenIterableIsEmpty_whenChecking_thenEmpty() {
|
||||
final Iterable<String> collection = Lists.newArrayList();
|
||||
assertThat(collection, emptyIterable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenCollectionIsNotEmpty_whenChecking_thenNotEmpty() {
|
||||
|
|
|
@ -1,30 +1,12 @@
|
|||
package com.baeldung.hibernate;
|
||||
|
||||
import com.baeldung.hibernate.pojo.Employee;
|
||||
import com.baeldung.hibernate.pojo.EntityDescription;
|
||||
import com.baeldung.hibernate.pojo.OrderEntry;
|
||||
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
|
||||
import com.baeldung.hibernate.pojo.OrderEntryPK;
|
||||
import com.baeldung.hibernate.pojo.PointEntity;
|
||||
import com.baeldung.hibernate.pojo.PolygonEntity;
|
||||
import com.baeldung.hibernate.pojo.Product;
|
||||
import com.baeldung.hibernate.pojo.Phone;
|
||||
import com.baeldung.hibernate.pojo.TemporalValues;
|
||||
import com.baeldung.hibernate.pojo.Course;
|
||||
import com.baeldung.hibernate.pojo.Student;
|
||||
import com.baeldung.hibernate.pojo.User;
|
||||
import com.baeldung.hibernate.pojo.UserProfile;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Animal;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Bag;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Book;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Car;
|
||||
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
|
||||
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pen;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Person;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pet;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
|
||||
|
||||
import com.baeldung.hibernate.pessimisticlocking.Individual;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
|
||||
import com.baeldung.hibernate.pojo.*;
|
||||
import com.baeldung.hibernate.pojo.Person;
|
||||
import com.baeldung.hibernate.pojo.inheritance.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
|
@ -82,6 +64,12 @@ public class HibernateUtil {
|
|||
metadataSources.addAnnotatedClass(PointEntity.class);
|
||||
metadataSources.addAnnotatedClass(PolygonEntity.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
|
||||
metadataSources.addAnnotatedClass(Individual.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingEmployee.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingStudent.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
|
||||
|
||||
Metadata metadata = metadataSources.buildMetadata();
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
|
||||
private String country;
|
||||
private String city;
|
||||
|
||||
public Address(String country, String city) {
|
||||
this.country = country;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
private Long customerId;
|
||||
private String name;
|
||||
private String lastName;
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "customer_address")
|
||||
private List<Address> addressList;
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
public Customer(Long customerId, String name, String lastName, List<Address> addressList) {
|
||||
this.customerId = customerId;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
this.addressList = addressList;
|
||||
}
|
||||
|
||||
public Long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(Long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List<Address> getAddressList() {
|
||||
return addressList;
|
||||
}
|
||||
|
||||
public void setAddressList(List<Address> addressList) {
|
||||
this.addressList = addressList;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
public class Individual {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
private String lastName;
|
||||
|
||||
public Individual(Long id, String name, String lastName) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Individual() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingCourse {
|
||||
|
||||
@Id
|
||||
private Long courseId;
|
||||
private String name;
|
||||
@ManyToOne
|
||||
@JoinTable(name = "student_course")
|
||||
private PessimisticLockingStudent student;
|
||||
|
||||
public PessimisticLockingCourse(Long courseId, String name, PessimisticLockingStudent student) {
|
||||
this.courseId = courseId;
|
||||
this.name = name;
|
||||
this.student = student;
|
||||
}
|
||||
|
||||
public PessimisticLockingCourse() {
|
||||
}
|
||||
|
||||
public Long getCourseId() {
|
||||
return courseId;
|
||||
}
|
||||
|
||||
public void setCourseId(Long courseId) {
|
||||
this.courseId = courseId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public PessimisticLockingStudent getStudent() {
|
||||
return student;
|
||||
}
|
||||
|
||||
public void setStudent(PessimisticLockingStudent students) {
|
||||
this.student = students;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingEmployee extends Individual {
|
||||
|
||||
private BigDecimal salary;
|
||||
|
||||
public PessimisticLockingEmployee(Long id, String name, String lastName, BigDecimal salary) {
|
||||
super(id, name, lastName);
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public PessimisticLockingEmployee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BigDecimal getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(BigDecimal average) {
|
||||
this.salary = average;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingStudent {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "student")
|
||||
private List<PessimisticLockingCourse> courses;
|
||||
|
||||
public PessimisticLockingStudent(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public PessimisticLockingStudent() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<PessimisticLockingCourse> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<PessimisticLockingCourse> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.vividsolutions.jts.util.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BasicPessimisticLockingIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOHN");
|
||||
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "MATH", student);
|
||||
student.setCourses(Arrays.asList(course));
|
||||
entityManager.persist(course);
|
||||
entityManager.persist(student);
|
||||
entityManager.getTransaction()
|
||||
.commit();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFoundRecordWithPessimisticRead_whenFindingNewOne_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
entityManager.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticReadQuery_whenQueryingNewOne_PessimisticLockExceptionThrown() throws IOException {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
Query query = entityManager.createQuery("from Student where studentId = :studentId");
|
||||
query.setParameter("studentId", 1L);
|
||||
query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
|
||||
query.getResultList();
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
Query query2 = entityManager2.createQuery("from Student where studentId = :studentId");
|
||||
query2.setParameter("studentId", 1L);
|
||||
query2.setLockMode(LockModeType.PESSIMISTIC_READ);
|
||||
query2.getResultList();
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticReadLock_whenFindingNewOne_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.lock(resultStudent, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordAndRefreshWithPessimisticRead_whenFindingWithPessimisticWrite_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticRead_whenUpdatingRecord_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent2 = entityManager2.find(PessimisticLockingStudent.class, 1L);
|
||||
resultStudent2.setName("Change");
|
||||
entityManager2.persist(resultStudent2);
|
||||
entityManager2.getTransaction()
|
||||
.commit();
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticWrite_whenUpdatingRecord_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_WRITE);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent2 = entityManager2.find(PessimisticLockingStudent.class, 1L);
|
||||
resultStudent2.setName("Change");
|
||||
entityManager2.persist(resultStudent2);
|
||||
entityManager2.getTransaction()
|
||||
.commit();
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
protected static EntityManager getEntityManagerWithOpenTransaction() throws IOException {
|
||||
String propertyFileName = "hibernate-pessimistic-locking.properties";
|
||||
EntityManager entityManager = HibernateUtil.getSessionFactory(propertyFileName)
|
||||
.openSession();
|
||||
entityManager.getTransaction()
|
||||
.begin();
|
||||
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,115 @@
|
|||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.LockModeType;
|
||||
import javax.persistence.PessimisticLockScope;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PessimisticLockScopesIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "JOHN", "SMITH", new BigDecimal(4.5));
|
||||
em.persist(employee);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingEmployee foundEmployee = em2.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundEmployee = em3.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em3.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEntityWithElementCollection_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
Address address = new Address("Poland", "Warsaw");
|
||||
Customer customer = new Customer(1L, "JOE", "DOE", Arrays.asList(address));
|
||||
em.persist(customer);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEntityWithOneToMany_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE");
|
||||
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student);
|
||||
student.setCourses(Arrays.asList(course));
|
||||
em.persist(course);
|
||||
em.persist(student);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingCourse foundCourse = em2.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundCourse = em3.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em3.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
protected EntityManager getEntityManagerWithOpenTransaction() throws IOException {
|
||||
String propertyFileName = "hibernate-pessimistic-locking.properties";
|
||||
EntityManager entityManager = HibernateUtil.getSessionFactory(propertyFileName)
|
||||
.openSession();
|
||||
entityManager.getTransaction()
|
||||
.begin();
|
||||
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
hibernate.connection.driver_class=org.h2.Driver
|
||||
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=100;MVCC=FALSE
|
||||
hibernate.connection.username=sa
|
||||
hibernate.connection.autocommit=true
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
|
@ -24,6 +24,7 @@ import org.junit.Test;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
@ -89,7 +90,7 @@ public class HttpClientAuthLiveTest {
|
|||
public final void givenAuthorizationHeaderIsSetManually_whenExecutingGetRequest_thenSuccess2() throws IOException {
|
||||
final HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
|
||||
final String auth = DEFAULT_USER + ":" + DEFAULT_PASS;
|
||||
final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("ISO-8859-1")));
|
||||
final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
|
||||
final String authHeader = "Basic " + new String(encodedAuth);
|
||||
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
|
||||
|
||||
|
@ -129,7 +130,7 @@ public class HttpClientAuthLiveTest {
|
|||
|
||||
private String authorizationHeader(final String username, final String password) {
|
||||
final String auth = username + ":" + password;
|
||||
final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("ISO-8859-1")));
|
||||
final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
|
||||
|
||||
return "Basic " + new String(encodedAuth);
|
||||
}
|
||||
|
|
|
@ -1,46 +1,75 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>javax-servlets</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>javax-servlets</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${org.apache.httpcomponents.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<!-- File Uploading -->
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
<properties>
|
||||
<javax.servlet.version>3.1.0</javax.servlet.version>
|
||||
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
|
||||
<spring-test.version>5.0.5.RELEASE</spring-test.version>
|
||||
</properties>
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
||||
<artifactId>jstl-api</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${org.apache.httpcomponents.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
|
||||
<spring-test.version>5.0.5.RELEASE</spring-test.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String UPLOAD_DIRECTORY = "upload";
|
||||
public static final String DEFAULT_FILENAME = "default.file";
|
||||
|
||||
public static final int MEMORY_THRESHOLD = 1024 * 1024 * 3;
|
||||
public static final int MAX_FILE_SIZE = 1024 * 1024 * 40;
|
||||
public static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50;
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import com.baeldung.Constants;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.MultipartConfig;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.Part;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.baeldung.Constants.UPLOAD_DIRECTORY;
|
||||
|
||||
@WebServlet(
|
||||
name = "MultiPartServlet",
|
||||
urlPatterns = {"/multiPartServlet"}
|
||||
)
|
||||
@MultipartConfig(fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
|
||||
public class MultipartServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String getFileName(Part part) {
|
||||
for (String content : part.getHeader("content-disposition").split(";")) {
|
||||
if (content.trim().startsWith("filename"))
|
||||
return content.substring(content.indexOf("=") + 2, content.length() - 1);
|
||||
}
|
||||
return Constants.DEFAULT_FILENAME;
|
||||
}
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
|
||||
File uploadDir = new File(uploadPath);
|
||||
if (!uploadDir.exists())
|
||||
uploadDir.mkdir();
|
||||
|
||||
try {
|
||||
String fileName = "";
|
||||
for (Part part : request.getParts()) {
|
||||
fileName = getFileName(part);
|
||||
part.write(uploadPath + File.separator + fileName);
|
||||
}
|
||||
request.setAttribute("message", "File " + fileName + " has uploaded successfully!");
|
||||
} catch (FileNotFoundException fne) {
|
||||
request.setAttribute("message", "There was an error: " + fne.getMessage());
|
||||
}
|
||||
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static com.baeldung.Constants.*;
|
||||
|
||||
@WebServlet(
|
||||
name = "UploadServlet",
|
||||
urlPatterns = {"/uploadFile"}
|
||||
)
|
||||
public class UploadServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
if (ServletFileUpload.isMultipartContent(request)) {
|
||||
|
||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||
factory.setSizeThreshold(MEMORY_THRESHOLD);
|
||||
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
|
||||
|
||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||
upload.setFileSizeMax(MAX_FILE_SIZE);
|
||||
upload.setSizeMax(MAX_REQUEST_SIZE);
|
||||
String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
|
||||
File uploadDir = new File(uploadPath);
|
||||
if (!uploadDir.exists()) {
|
||||
uploadDir.mkdir();
|
||||
}
|
||||
|
||||
try {
|
||||
List<FileItem> formItems = upload.parseRequest(request);
|
||||
|
||||
if (formItems != null && formItems.size() > 0) {
|
||||
for (FileItem item : formItems) {
|
||||
if (!item.isFormField()) {
|
||||
String fileName = new File(item.getName()).getName();
|
||||
String filePath = uploadPath + File.separator + fileName;
|
||||
File storeFile = new File(filePath);
|
||||
item.write(storeFile);
|
||||
request.setAttribute("message", "File " + fileName + " has uploaded successfully!");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
request.setAttribute("message", "There was an error: " + ex.getMessage());
|
||||
}
|
||||
getServletContext().getRequestDispatcher("/result.jsp").forward(request, response);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet(name = "UploadWelcomeServlet", urlPatterns = "/uploadwelcome")
|
||||
public class UploadWelcomeServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
request.getRequestDispatcher("/upload.jsp").forward(request, response);
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
</web-app>
|
|
@ -0,0 +1,13 @@
|
|||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>Upload Result</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>${message}</h2>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,23 @@
|
|||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>File Upload Demo</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div>Apache FileUpload</div>
|
||||
<form method="post" action="uploadFile" enctype="multipart/form-data">
|
||||
Choose a file: <input type="file" name="uploadFile"/><input type="submit" value="Upload"/>
|
||||
</form>
|
||||
|
||||
</br>
|
||||
|
||||
<div>Servlet Multipart</div>
|
||||
<form method="post" action="multiPartServlet" enctype="multipart/form-data">
|
||||
Choose a file: <input type="file" name="multiPartServlet"/><input type="submit" value="Upload"/>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
|
@ -680,6 +680,12 @@
|
|||
<version>${unirest.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- javalin -->
|
||||
<dependency>
|
||||
<groupId>io.javalin</groupId>
|
||||
<artifactId>javalin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
</dependency>
|
||||
<!-- Atlassian Fugue -->
|
||||
<dependency>
|
||||
<groupId>io.atlassian.fugue</groupId>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.javalin;
|
||||
|
||||
import com.baeldung.javalin.User.UserController;
|
||||
import io.javalin.Javalin;
|
||||
|
||||
public class JavalinApp {
|
||||
public static void main(String[] args) {
|
||||
Javalin app = Javalin.create()
|
||||
.port(7000)
|
||||
.start();
|
||||
|
||||
app.get("/hello", ctx -> ctx.html("Hello, Javalin!"));
|
||||
app.get("/users", UserController.fetchAllUsernames);
|
||||
app.get("/users/:id", UserController.fetchById);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.javalin.User;
|
||||
|
||||
public class User {
|
||||
public final int id;
|
||||
public final String name;
|
||||
|
||||
public User(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.javalin.User;
|
||||
|
||||
import io.javalin.Handler;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class UserController {
|
||||
public static Handler fetchAllUsernames = ctx -> {
|
||||
UserDao dao = UserDao.instance();
|
||||
Iterable<String> allUsers = dao.getAllUsernames();
|
||||
ctx.json(allUsers);
|
||||
};
|
||||
|
||||
public static Handler fetchById = ctx -> {
|
||||
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
|
||||
UserDao dao = UserDao.instance();
|
||||
User user = dao.getUserById(id);
|
||||
if (user == null) {
|
||||
ctx.html("Not Found");
|
||||
} else {
|
||||
ctx.json(user);
|
||||
}
|
||||
};
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.javalin.User;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class UserDao {
|
||||
|
||||
private final List<User> users = Arrays.asList(
|
||||
new User(0, "Steve Rogers"),
|
||||
new User(1, "Tony Stark"),
|
||||
new User(2, "Carol Danvers")
|
||||
);
|
||||
|
||||
private static UserDao userDao = null;
|
||||
|
||||
private UserDao() {
|
||||
}
|
||||
|
||||
static UserDao instance() {
|
||||
if (userDao == null) {
|
||||
userDao = new UserDao();
|
||||
}
|
||||
return userDao;
|
||||
}
|
||||
|
||||
Optional<User> getUserById(int id) { return users.stream().filter(u -> u.id == id).findFirst(); }
|
||||
|
||||
Iterable<String> getAllUsernames() {
|
||||
return users.stream().map(user -> user.name).collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.dto;
|
||||
|
||||
public class TransactionDTO {
|
||||
|
||||
private String uuid;
|
||||
private Long totalInCents;
|
||||
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public Long getTotalInCents() {
|
||||
return totalInCents;
|
||||
}
|
||||
|
||||
public void setTotalInCents(Long totalInCents) {
|
||||
this.totalInCents = totalInCents;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.entity;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Transaction {
|
||||
|
||||
private Long id;
|
||||
private String uuid = UUID.randomUUID().toString();
|
||||
private BigDecimal total;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public BigDecimal getTotal() {
|
||||
return total;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.mapper;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import com.baeldung.dto.TransactionDTO;
|
||||
import com.baeldung.entity.Transaction;
|
||||
|
||||
@Mapper
|
||||
abstract class TransactionMapper {
|
||||
|
||||
public TransactionDTO toTransactionDTO(Transaction transaction) {
|
||||
TransactionDTO transactionDTO = new TransactionDTO();
|
||||
transactionDTO.setUuid(transaction.getUuid());
|
||||
transactionDTO.setTotalInCents(transaction.getTotal().multiply(new BigDecimal("100")).longValue());
|
||||
return transactionDTO;
|
||||
}
|
||||
|
||||
public abstract List<TransactionDTO> toTransactionDTO(Collection<Transaction> transactions);
|
||||
}
|
|
@ -3,4 +3,5 @@
|
|||
- [Introduction to Intercepting Filter Pattern in Java](http://www.baeldung.com/intercepting-filter-pattern-in-java)
|
||||
- [Implementing the Template Method Pattern in Java](http://www.baeldung.com/java-template-method-pattern)
|
||||
- [Chain of Responsibility Design Pattern in Java](http://www.baeldung.com/chain-of-responsibility-pattern)
|
||||
- [The Command Pattern in Java](http://www.baeldung.com/java-command-pattern)
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
### Relevant Articles:
|
||||
- [Facade Design Pattern in Java](http://www.baeldung.com/java-facade-pattern)
|
||||
- [Singletons in Java](http://www.baeldung.com/java-singleton)
|
||||
- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns)
|
||||
- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns)
|
||||
- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern)
|
||||
- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight)
|
||||
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
|
||||
- [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking)
|
||||
- [Composite Design Pattern in Java](http://www.baeldung.com/java-composite-pattern)
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.pattern.templatemethod</groupId>
|
||||
<artifactId>pattern.templatemethod</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>design-patterns</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.patterns</groupId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns-parent</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
|
@ -19,7 +19,7 @@
|
|||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>1.3</version>
|
||||
|
@ -31,10 +31,22 @@
|
|||
<version>3.8.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.grep4j</groupId>
|
||||
<artifactId>grep4j</artifactId>
|
||||
<version>${grep4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.designpatterns.adapter;
|
||||
package com.baeldung.adapter;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
import static com.baeldung.util.LogerUtil.LOG;
|
||||
|
||||
public class AdapterPatternDriver {
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class AstonMartin implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 220;
|
||||
}
|
||||
}
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class AstonMartin implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 220;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class BugattiVeyron implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 268;
|
||||
}
|
||||
}
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class BugattiVeyron implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 268;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class McLaren implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 241;
|
||||
}
|
||||
}
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class McLaren implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 241;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface Movable {
|
||||
// returns speed in MPH
|
||||
double getSpeed();
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public interface Movable {
|
||||
// returns speed in MPH
|
||||
double getSpeed();
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.adapter;
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public interface MovableAdapter {
|
||||
// returns speed in KMPH
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.adapter;
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class MovableAdapterImpl implements MovableAdapter {
|
||||
private Movable luxuryCars;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.bridge;
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Blue implements Color {
|
||||
@Override
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.bridge;
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class BridgePatternDriver {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.bridge;
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public interface Color {
|
||||
String fill();
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.bridge;
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Red implements Color {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.bridge;
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public abstract class Shape {
|
||||
protected Color color;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.bridge;
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Square extends Shape {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.bridge;
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Triangle extends Shape {
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/3/2018.
|
||||
*/
|
||||
public class CompositeDemo {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Department salesDepartment = new SalesDepartment(1, "Sales department");
|
||||
Department financialDepartment = new FinancialDepartment(2, "Financial department");
|
||||
|
||||
HeadDepartment headDepartment = new HeadDepartment(3, "Head department");
|
||||
|
||||
headDepartment.addDepartMent(salesDepartment);
|
||||
headDepartment.addDepartMent(financialDepartment);
|
||||
|
||||
headDepartment.printDepartmentName();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public interface Department {
|
||||
|
||||
void printDepartmentName();
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public class FinancialDepartment implements Department {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public FinancialDepartment(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void printDepartmentName() {
|
||||
System.out.println(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.composite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public class HeadDepartment implements Department {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
private List<Department> childDepartments;
|
||||
|
||||
public HeadDepartment(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.childDepartments = new ArrayList<Department>();
|
||||
}
|
||||
|
||||
public void printDepartmentName() {
|
||||
childDepartments.forEach(Department::printDepartmentName);
|
||||
}
|
||||
|
||||
public void addDepartMent(Department department) {
|
||||
childDepartments.add(department);
|
||||
}
|
||||
|
||||
public void removeDepartment(Department department) {
|
||||
childDepartments.remove(department);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public class SalesDepartment implements Department {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public SalesDepartment(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void printDepartmentName() {
|
||||
System.out.println(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public interface AbstractFactory {
|
||||
Animal getAnimal(String toyType) ;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class AbstractPatternDriver {
|
||||
public static void main(String[] args) {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public interface Animal {
|
||||
String getType();
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class AnimalFactory implements AbstractFactory {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class Brown implements Color {
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public interface Color {
|
||||
String getColor();
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class ColorFactory implements AbstractFactory {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class Dog implements Animal {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class Duck implements Animal {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class FactoryProvider {
|
||||
public static AbstractFactory getFactory(String choice){
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class White implements Color {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.builder;
|
||||
package com.baeldung.creational.builder;
|
||||
|
||||
public class BankAccount {
|
||||
private String name;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.builder;
|
||||
package com.baeldung.creational.builder;
|
||||
|
||||
public class BuilderPatternDriver {
|
||||
public static void main(String[] args) {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.factory;
|
||||
package com.baeldung.creational.factory;
|
||||
|
||||
public class FactoryDriver {
|
||||
public static void main(String[] args) {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.designpatterns.creational.factory;
|
||||
package com.baeldung.creational.factory;
|
||||
|
||||
public class Heptagon implements Polygon {
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue