Skip to content

Commit 750aad4

Browse files
author
DianeDuan
committed
merge upstream
2 parents 361874d + 6cca052 commit 750aad4

80 files changed

Lines changed: 1987 additions & 279 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.generics;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Generics {
8+
9+
// definition of a generic method
10+
public static <T> List<T> fromArrayToList(T[] a) {
11+
List<T> list = new ArrayList<>();
12+
Arrays.stream(a).forEach(list::add);
13+
return list;
14+
}
15+
16+
// example of a generic method that has Number as an upper bound for T
17+
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;
21+
}
22+
23+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.java.nio2.visitor;
2+
3+
import static java.nio.file.FileVisitResult.CONTINUE;
4+
import static java.nio.file.FileVisitResult.TERMINATE;
5+
6+
import java.io.IOException;
7+
import java.nio.file.FileVisitResult;
8+
import java.nio.file.FileVisitor;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.nio.file.attribute.BasicFileAttributes;
13+
14+
public class FileSearchExample implements FileVisitor<Path> {
15+
private static String FILE_NAME;
16+
private static Path START_DIR;
17+
18+
public FileSearchExample(String fileName, Path startingDir) {
19+
FILE_NAME = fileName;
20+
START_DIR = startingDir;
21+
}
22+
23+
@Override
24+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
25+
return CONTINUE;
26+
}
27+
28+
@Override
29+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
30+
String fileName = file.getFileName().toString();
31+
if (FILE_NAME.equals(fileName)) {
32+
System.out.println("File found: " + file.toString());
33+
return TERMINATE;
34+
}
35+
return CONTINUE;
36+
}
37+
38+
@Override
39+
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
40+
System.out.println("Failed to access file: " + file.toString());
41+
return CONTINUE;
42+
}
43+
44+
@Override
45+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
46+
boolean finishedSearch = Files.isSameFile(dir, START_DIR);
47+
if (finishedSearch) {
48+
System.out.println("File:" + FILE_NAME + " not found");
49+
return TERMINATE;
50+
}
51+
return CONTINUE;
52+
}
53+
54+
public static void main(String[] args) throws IOException {
55+
Path startingDir = Paths.get("C:/Users/new/Desktop");
56+
FileSearchExample crawler = new FileSearchExample("hibernate.txt", startingDir);
57+
Files.walkFileTree(startingDir, crawler);
58+
}
59+
60+
}
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.generics;
2+
3+
import org.junit.Test;
4+
5+
import java.util.List;
6+
7+
import static org.hamcrest.CoreMatchers.hasItems;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
10+
public class GenericsTest {
11+
12+
// testing the generic method with Integer
13+
@Test
14+
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
15+
Integer[] intArray = {1, 2, 3, 4, 5};
16+
List<Integer> list = Generics.fromArrayToList(intArray);
17+
18+
assertThat(list, hasItems(intArray));
19+
}
20+
21+
// testing the generic method with String
22+
@Test
23+
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
24+
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
25+
List<String> list = Generics.fromArrayToList(stringArray);
26+
27+
assertThat(list, hasItems(stringArray));
28+
}
29+
30+
// testing the generic method with Number as upper bound with Integer
31+
// if we test fromArrayToListWithUpperBound with any type that doesn't
32+
// extend Number it will fail to compile
33+
@Test
34+
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
35+
Integer[] intArray = {1, 2, 3, 4, 5};
36+
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
37+
38+
assertThat(list, hasItems(intArray));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)