Skip to content

Commit 035d695

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 96c05ab + 851deed commit 035d695

247 files changed

Lines changed: 8136 additions & 2189 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.

‎aspectj/pom.xml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@
9898
<execution>
9999
<goals>
100100
<goal>compile</goal>
101-
<goal>test-compile</goal>
101+
<goal>test-compile</goal>
102102
</goals>
103103
</execution>
104104
</executions>
105105
</plugin>
106106

107-
<!-- <plugin>
107+
<!--
108+
<plugin>
108109
<groupId>org.apache.maven.plugins</groupId>
109110
<artifactId>maven-surefire-plugin</artifactId>
110111
<version>2.10</version>
@@ -113,7 +114,8 @@
113114
<useSystemClassLoader>true</useSystemClassLoader>
114115
<forkMode>always</forkMode>
115116
</configuration>
116-
</plugin> -->
117+
</plugin>
118+
-->
117119
</plugins>
118120
</build>
119121

‎aspectj/src/main/java/com/baeldung/aspectj/Account.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ public class Account {
44
int balance = 20;
55

66
public boolean withdraw(int amount) {
7-
if (balance - amount > 0) {
8-
balance = balance - amount;
9-
return true;
10-
} else
7+
if (balance < amount) {
118
return false;
9+
}
10+
balance = balance - amount;
11+
return true;
1212
}
1313
}

‎aspectj/src/main/java/com/baeldung/aspectj/AccountAspect.aj‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ public aspect AccountAspect {
1616
}
1717

1818
boolean around(int amount, Account account) : callWithDraw(amount, account) {
19-
if (account.balance - amount >= MIN_BALANCE)
20-
return proceed(amount, account);
21-
else {
19+
if (account.balance < amount) {
2220
logger.info("Withdrawal Rejected!");
2321
return false;
2422
}
23+
return proceed(amount, account);
2524
}
2625

2726
after(int amount, Account balance) : callWithDraw(amount, balance) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.generics;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class Building {
7+
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
8+
9+
public void paint() {
10+
LOGGER.info("Painting Building");
11+
}
12+
}
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package com.baeldung.generics;
22

3-
import java.util.ArrayList;
43
import java.util.Arrays;
54
import java.util.List;
5+
import java.util.function.Function;
6+
import java.util.stream.Collectors;
67

78
public class Generics {
89

910
// definition of a generic method
1011
public static <T> List<T> fromArrayToList(T[] a) {
11-
List<T> list = new ArrayList<>();
12-
Arrays.stream(a).forEach(list::add);
13-
return list;
12+
return Arrays.stream(a).collect(Collectors.toList());
13+
}
14+
15+
// definition of a generic method
16+
public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
17+
return Arrays.stream(a).map(mapperFunction).collect(Collectors.toList());
1418
}
1519

1620
// example of a generic method that has Number as an upper bound for T
1721
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
18-
List<T> list = new ArrayList<>();
19-
Arrays.stream(a).forEach(list::add);
20-
return list;
22+
return Arrays.stream(a).collect(Collectors.toList());
23+
}
24+
25+
// example of a generic method with a wild card, this method can be used
26+
// with a list of any subtype of Building
27+
public static void paintAllBuildings(List<? extends Building> buildings) {
28+
buildings.forEach(Building::paint);
2129
}
2230

23-
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.generics;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class House extends Building {
7+
private static final Logger LOGGER = LoggerFactory.getLogger(House.class);
8+
9+
public void paint() {
10+
LOGGER.info("Painting House");
11+
}
12+
}

‎core-java/src/main/java/com/baeldung/hashing/SHA256Hashing.java‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.hashing;
22

3-
43
import com.google.common.hash.Hashing;
54
import org.apache.commons.codec.digest.DigestUtils;
65
import org.bouncycastle.util.encoders.Hex;
@@ -11,17 +10,14 @@
1110

1211
public class SHA256Hashing {
1312

14-
public static String HashWithJavaMessageDigest(final String originalString)
15-
throws NoSuchAlgorithmException {
13+
public static String HashWithJavaMessageDigest(final String originalString) throws NoSuchAlgorithmException {
1614
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
17-
final byte[] encodedhash = digest.digest(
18-
originalString.getBytes(StandardCharsets.UTF_8));
15+
final byte[] encodedhash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
1916
return bytesToHex(encodedhash);
2017
}
2118

2219
public static String HashWithGuava(final String originalString) {
23-
final String sha256hex = Hashing.sha256().hashString(
24-
originalString, StandardCharsets.UTF_8).toString();
20+
final String sha256hex = Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
2521
return sha256hex;
2622
}
2723

@@ -30,11 +26,9 @@ public static String HashWithApacheCommons(final String originalString) {
3026
return sha256hex;
3127
}
3228

33-
public static String HashWithBouncyCastle(final String originalString)
34-
throws NoSuchAlgorithmException {
29+
public static String HashWithBouncyCastle(final String originalString) throws NoSuchAlgorithmException {
3530
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
36-
final byte[] hash = digest.digest(
37-
originalString.getBytes(StandardCharsets.UTF_8));
31+
final byte[] hash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
3832
final String sha256hex = new String(Hex.encode(hash));
3933
return sha256hex;
4034
}
@@ -43,7 +37,8 @@ private static String bytesToHex(byte[] hash) {
4337
StringBuffer hexString = new StringBuffer();
4438
for (int i = 0; i < hash.length; i++) {
4539
String hex = Integer.toHexString(0xff & hash[i]);
46-
if(hex.length() == 1) hexString.append('0');
40+
if (hex.length() == 1)
41+
hexString.append('0');
4742
hexString.append(hex);
4843
}
4944
return hexString.toString();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.java.nio2.visitor;
2+
3+
import java.io.IOException;
4+
import java.nio.file.*;
5+
import java.nio.file.attribute.BasicFileAttributes;
6+
7+
import static java.nio.file.FileVisitResult.CONTINUE;
8+
import static java.nio.file.FileVisitResult.TERMINATE;
9+
10+
public class FileSearchExample implements FileVisitor<Path> {
11+
private final String fileName;
12+
private final Path startDir;
13+
14+
public FileSearchExample(String fileName, Path startingDir) {
15+
this.fileName = fileName;
16+
startDir = startingDir;
17+
}
18+
19+
@Override
20+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
21+
return CONTINUE;
22+
}
23+
24+
@Override
25+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
26+
String fileName = file.getFileName().toString();
27+
if (this.fileName.equals(fileName)) {
28+
System.out.println("File found: " + file.toString());
29+
return TERMINATE;
30+
}
31+
return CONTINUE;
32+
}
33+
34+
@Override
35+
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
36+
System.out.println("Failed to access file: " + file.toString());
37+
return CONTINUE;
38+
}
39+
40+
@Override
41+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
42+
boolean finishedSearch = Files.isSameFile(dir, startDir);
43+
if (finishedSearch) {
44+
System.out.println("File:" + fileName + " not found");
45+
return TERMINATE;
46+
}
47+
return CONTINUE;
48+
}
49+
50+
public static void main(String[] args) throws IOException {
51+
Path startingDir = Paths.get("C:/Users/new/Desktop");
52+
FileSearchExample crawler = new FileSearchExample("hibernate.txt", startingDir);
53+
Files.walkFileTree(startingDir, crawler);
54+
}
55+
56+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.java.nio2.visitor;
2+
3+
import java.io.IOException;
4+
import java.nio.file.FileVisitResult;
5+
import java.nio.file.FileVisitor;
6+
import java.nio.file.Path;
7+
import java.nio.file.attribute.BasicFileAttributes;
8+
9+
public class FileVisitorImpl implements FileVisitor<Path> {
10+
11+
@Override
12+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
13+
return null;
14+
}
15+
16+
@Override
17+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
18+
return null;
19+
}
20+
21+
@Override
22+
public FileVisitResult visitFileFailed(Path file, IOException exc) {
23+
return null;
24+
}
25+
26+
@Override
27+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
28+
return null;
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.java.nio2.watcher;
2+
3+
import java.io.IOException;
4+
import java.nio.file.FileSystems;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.nio.file.StandardWatchEventKinds;
8+
import java.nio.file.WatchEvent;
9+
import java.nio.file.WatchKey;
10+
import java.nio.file.WatchService;
11+
12+
public class DirectoryWatcherExample {
13+
public static void main(String[] args) throws IOException, InterruptedException {
14+
WatchService watchService = FileSystems.getDefault().newWatchService();
15+
Path path = Paths.get(System.getProperty("user.home"));
16+
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
17+
WatchKey key;
18+
while ((key = watchService.take()) != null) {
19+
for (WatchEvent<?> event : key.pollEvents()) {
20+
System.out.println("Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
21+
}
22+
key.reset();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)