Skip to content

Commit 2bc3c14

Browse files
review comments
1 parent 1090032 commit 2bc3c14

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

Java8/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,6 @@ Here's an overview of what's currently included in these examples:
233233
Java 7 features to replace substrings vs. using modern Java
234234
features.
235235

236+
. ex42 - This example shows how to count the number of images in a
237+
recursively-defined folder structure using the Java
238+
sequential stream framework and the teeing Collector.

Java8/ex12/src/main/java/tests/CollectTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ public static void runTeeingCollector() {
142142
// characters not starting with 'H' or 'h' into
143143
// a separate List.
144144
teeing(// Filter out non 'H' or 'h' characters.
145-
filtering(startsWithHh(),
145+
filtering(startsWithHh(true),
146146
toList()),
147147
// Filter out 'H' or 'h' characters.
148-
filtering(startsWithHh(),
148+
filtering(startsWithHh(false),
149149
toList()),
150150
// Merge the Lists together.
151151
(l1, l2) -> {

Java8/ex12/src/main/java/tests/Generators.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ public class Generators {
6767
List.of("Fortinbras")};
6868

6969
/**
70-
* @return true if the {@link String} starts with 'H' or 'h'.
70+
* @return true if the {@link String} starts with 'H' or 'h'.
7171
*/
72-
static Predicate<String> startsWithHh() {
73-
return s -> toLowerCase(s.charAt(0)) == 'h';
72+
static Predicate<String> startsWithHh(boolean yes) {
73+
if (yes)
74+
return s -> toLowerCase(s.charAt(0)) == 'h';
75+
else
76+
return s -> toLowerCase(s.charAt(0)) != 'h';
7477
}
7578

7679
/**
@@ -98,7 +101,7 @@ public static String capitalize(String s) {
98101
public static Stream<String> generateHCharacters(Stream<String> characters) {
99102
return characters
100103
// Remove any strings that don't start with 'h' or 'H'.
101-
.filter(startsWithHh())
104+
.filter(startsWithHh(true))
102105

103106
// Capitalize the first letter in the string.
104107
.map(Generators::capitalize)

Java8/ex42/src/main/java/ImageCounter.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import org.jsoup.nodes.Document;
22
import utils.Options;
33

4+
import java.util.stream.Collector;
45
import java.util.stream.Collectors;
56
import java.util.stream.Stream;
67
import java.util.concurrent.ConcurrentHashMap;
@@ -10,9 +11,9 @@
1011

1112
/**
1213
* This class counts the number of images in a recursively-defined
13-
* folder structure using the Java sequential stream framework. The
14-
* root folder can either reside locally (filesystem -based) or
15-
* remotely (web-based).
14+
* folder structure using the Java sequential stream framework and
15+
* the {@code teeing} {@link Collector}. The root folder can either
16+
* reside locally (filesystem -based) or remotely (web-based).
1617
*/
1718
class ImageCounter {
1819
/**
@@ -22,6 +23,8 @@ class ImageCounter {
2223

2324
/**
2425
* A cache of unique URIs that have already been processed.
26+
* {@link KeySetView} is part of the {@link ConcurrentHashMap}
27+
* class.
2528
*/
2629
private final KeySetView<Object, Boolean> mUniqueUris =
2730
ConcurrentHashMap.newKeySet();
@@ -60,32 +63,33 @@ private long countImages(String pageUri,
6063
+ "]: Exceeded max depth of "
6164
+ Options.instance().maxDepth());
6265

66+
// Return 0 if we've exceeded the depth param.
6367
return 0;
6468
}
6569

6670
// Atomically check to see if we've already visited this URL
6771
// and add the new url to the hashset, so we don't try to
6872
// revisit it again unnecessarily.
6973
else if (mUniqueUris
74+
// Get the ConcurrentHashMap that implements the
75+
// KeySetView.
7076
.getMap()
77+
// Perform the atomic-check-then-act operation.
7178
.putIfAbsent(pageUri,
7279
mUniqueUris.getMappedValue()) != null) {
7380
print(TAG
74-
+ "[Depth"
75-
+ depth
76-
+ "]: Already processed "
77-
+ pageUri);
81+
+ "[Depth"
82+
+ depth
83+
+ "]: Already processed "
84+
+ pageUri);
7885

7986
// Return 0 if we've already examined this url.
8087
return 0;
81-
}
82-
83-
// Synchronously (1) count the number of images on this page
84-
// and (2) crawl other hyperlinks accessible via this page and
85-
// count their images.
86-
else {
87-
long count = countImagesImpl(pageUri,
88-
depth);
88+
} else {
89+
// Synchronously (1) count the number of images on this page
90+
// and (2) crawl other hyperlinks accessible via this page and
91+
// count their images.
92+
long count = countImagesImpl(pageUri, depth);
8993
print(TAG
9094
+ "[Depth"
9195
+ depth
@@ -95,6 +99,8 @@ else if (mUniqueUris
9599
+ pageUri
96100
+ " in thread "
97101
+ Thread.currentThread().getId());
102+
103+
// Return the count from this level in the traversal.
98104
return count;
99105
}
100106
}
@@ -164,7 +170,7 @@ private long getCountOfImagesInPage(Document page) {
164170
/**
165171
* Recursively crawl through hyperlinks that are in {@code page}.
166172
*
167-
* @param page The page containing HTML
173+
* @param page The page containing the HTML document
168174
* @param depth The depth of the level of web page traversal
169175
* @return A count of how many images were in each hyperlink on
170176
* the page

Java8/ex42/src/main/java/ex42.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* This example shows how to count the number of images in a
55
* recursively-defined folder structure using the Java sequential
6-
* stream framework.
6+
* stream framework and the {@code teeing} {@link Collector}.
77
*/
88
public class ex42 {
99
/**

0 commit comments

Comments
 (0)