Skip to content

Commit 5a9297a

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into story/BAEL-7636
2 parents 623ae0b + f55e379 commit 5a9297a

12 files changed

Lines changed: 332 additions & 159 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.keywords.finalize;
2+
3+
public class FinalizeObject {
4+
5+
@Override
6+
protected void finalize() throws Throwable {
7+
System.out.println("Execute finalize method");
8+
super.finalize();
9+
}
10+
11+
public static void main(String[] args) throws Exception {
12+
FinalizeObject object = new FinalizeObject();
13+
object = null;
14+
System.gc();
15+
Thread.sleep(1000);
16+
}
17+
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.keywords.finalkeyword;
2+
3+
public final class Child extends Parent {
4+
5+
@Override
6+
void method1(int arg1, final int arg2) {
7+
// OK
8+
}
9+
10+
/* @Override
11+
void method2() {
12+
// Compilation error
13+
}*/
14+
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.keywords.finalkeyword;
2+
3+
/*public class GrandChild extends Child {
4+
// Compilation error
5+
}*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.keywords.finalkeyword;
2+
3+
public class Parent {
4+
5+
int field1 = 1;
6+
final int field2 = 2;
7+
8+
Parent() {
9+
field1 = 2; // OK
10+
// field2 = 3; // Compilation error
11+
}
12+
13+
void method1(int arg1, final int arg2) {
14+
arg1 = 2; // OK
15+
// arg2 = 3; // Compilation error
16+
}
17+
18+
final void method2() {
19+
final int localVar = 2; // OK
20+
// localVar = 3; // Compilation error
21+
}
22+
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.keywords.finallykeyword;
2+
3+
public class FinallyExample {
4+
5+
public static void main(String args[]) throws Exception {
6+
try {
7+
System.out.println("Execute try block");
8+
throw new Exception();
9+
} catch (Exception e) {
10+
System.out.println("Execute catch block");
11+
} finally {
12+
System.out.println("Execute finally block");
13+
}
14+
15+
try {
16+
System.out.println("Execute try block");
17+
} finally {
18+
System.out.println("Execute finally block");
19+
}
20+
21+
try {
22+
System.out.println("Execute try block");
23+
throw new Exception();
24+
} finally {
25+
System.out.println("Execute finally block");
26+
}
27+
}
28+
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.regexp.datepattern.optmization;
2+
3+
public class OptimizedMatcher {
4+
5+
6+
}

‎core-java/src/test/java/com/baeldung/java/listInitialization/ListInitializationUnitTest.java‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,15 @@
99
import lombok.extern.java.Log;
1010

1111
import org.junit.Assert;
12-
import org.junit.Rule;
1312
import org.junit.Test;
14-
import org.junit.rules.ExpectedException;
1513

1614
@Log
1715
public class ListInitializationUnitTest {
1816

19-
@Rule
20-
public ExpectedException exception = ExpectedException.none();
21-
2217
@Test
2318
public void givenAnonymousInnerClass_thenInitialiseList() {
2419
List<String> cities = new ArrayList() {
25-
// Inside declaration of the subclass
26-
27-
// You can have multiple initializer block
28-
{
29-
log.info("Inside the first initializer block.");
30-
}
31-
3220
{
33-
log.info("Inside the second initializer block.");
3421
add("New York");
3522
add("Rio");
3623
add("Tokyo");
@@ -47,11 +34,10 @@ public void givenArraysAsList_thenInitialiseList() {
4734
Assert.assertTrue(list.contains("foo"));
4835
}
4936

50-
@Test
37+
@Test(expected = UnsupportedOperationException.class)
5138
public void givenArraysAsList_whenAdd_thenUnsupportedException() {
5239
List<String> list = Arrays.asList("foo", "bar");
5340

54-
exception.expect(UnsupportedOperationException.class);
5541
list.add("baz");
5642
}
5743

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.baeldung.regexp.optmization;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Random;
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
12+
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class OptimizedMatcherUnitTest {
16+
17+
private long time;
18+
private long mstimePreCompiled;
19+
private long mstimeNotPreCompiled;
20+
21+
private String action;
22+
23+
private List<String> items;
24+
25+
@Before
26+
public void setup() {
27+
Random random = new Random();
28+
items = new ArrayList<String>();
29+
long average = 0;
30+
31+
for (int i = 0; i < 100000; ++i) {
32+
StringBuilder s = new StringBuilder();
33+
int characters = random.nextInt(7) + 1;
34+
for (int k = 0; k < characters; ++ k) {
35+
char c = (char)(random.nextInt('Z' - 'A') + 'A');
36+
int rep = random.nextInt(95) + 5;
37+
for (int j = 0; j < rep; ++ j)
38+
s.append(c);
39+
average += rep;
40+
}
41+
items.add(s.toString());
42+
}
43+
44+
average /= 100000;
45+
System.out.println("generated data, average length: " + average);
46+
}
47+
48+
49+
@Test
50+
public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
51+
52+
testPatterns("A*");
53+
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
54+
}
55+
56+
@Test
57+
public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
58+
59+
testPatterns("A*B*C*");
60+
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
61+
}
62+
63+
@Test
64+
public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
65+
66+
testPatterns("E*C*W*F*");
67+
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
68+
}
69+
70+
private void testPatterns(String regex) {
71+
time = System.nanoTime();
72+
int matched = 0;
73+
int unmatched = 0;
74+
75+
for (String item : this.items) {
76+
if (item.matches(regex)) {
77+
++matched;
78+
}
79+
else {
80+
++unmatched;
81+
}
82+
}
83+
84+
this.action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
85+
86+
this.mstimeNotPreCompiled = (System.nanoTime() - time) / 1000000;
87+
System.out.println(this.action + ": " + mstimeNotPreCompiled + "ms");
88+
89+
time = System.nanoTime();
90+
91+
Matcher matcher = Pattern.compile(regex).matcher("");
92+
matched = 0;
93+
unmatched = 0;
94+
95+
for (String item : this.items) {
96+
if (matcher.reset(item).matches()) {
97+
++matched;
98+
}
99+
else {
100+
++unmatched;
101+
}
102+
}
103+
104+
this.action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
105+
106+
this.mstimePreCompiled = (System.nanoTime() - time) / 1000000;
107+
System.out.println(this.action + ": " + mstimePreCompiled + "ms");
108+
}
109+
}

‎testing-modules/java-faker/pom.xml‎

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎testing-modules/java-faker/src/test/java/com/baeldung/JavaFakerTest.java‎

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)