Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascraigschmidt committed Sep 19, 2022
1 parent 1090032 commit 2bc3c14
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Java8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,6 @@ Here's an overview of what's currently included in these examples:
Java 7 features to replace substrings vs. using modern Java
features.

. ex42 - This example shows how to count the number of images in a
recursively-defined folder structure using the Java
sequential stream framework and the teeing Collector.
4 changes: 2 additions & 2 deletions Java8/ex12/src/main/java/tests/CollectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ public static void runTeeingCollector() {
// characters not starting with 'H' or 'h' into
// a separate List.
teeing(// Filter out non 'H' or 'h' characters.
filtering(startsWithHh(),
filtering(startsWithHh(true),
toList()),
// Filter out 'H' or 'h' characters.
filtering(startsWithHh(),
filtering(startsWithHh(false),
toList()),
// Merge the Lists together.
(l1, l2) -> {
Expand Down
11 changes: 7 additions & 4 deletions Java8/ex12/src/main/java/tests/Generators.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ public class Generators {
List.of("Fortinbras")};

/**
* @return true if the {@link String} starts with 'H' or 'h'.
* @return true if the {@link String} starts with 'H' or 'h'.
*/
static Predicate<String> startsWithHh() {
return s -> toLowerCase(s.charAt(0)) == 'h';
static Predicate<String> startsWithHh(boolean yes) {
if (yes)
return s -> toLowerCase(s.charAt(0)) == 'h';
else
return s -> toLowerCase(s.charAt(0)) != 'h';
}

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

// Capitalize the first letter in the string.
.map(Generators::capitalize)
Expand Down
38 changes: 22 additions & 16 deletions Java8/ex42/src/main/java/ImageCounter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.jsoup.nodes.Document;
import utils.Options;

import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -10,9 +11,9 @@

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

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

// Return 0 if we've exceeded the depth param.
return 0;
}

// Atomically check to see if we've already visited this URL
// and add the new url to the hashset, so we don't try to
// revisit it again unnecessarily.
else if (mUniqueUris
// Get the ConcurrentHashMap that implements the
// KeySetView.
.getMap()
// Perform the atomic-check-then-act operation.
.putIfAbsent(pageUri,
mUniqueUris.getMappedValue()) != null) {
print(TAG
+ "[Depth"
+ depth
+ "]: Already processed "
+ pageUri);
+ "[Depth"
+ depth
+ "]: Already processed "
+ pageUri);

// Return 0 if we've already examined this url.
return 0;
}

// Synchronously (1) count the number of images on this page
// and (2) crawl other hyperlinks accessible via this page and
// count their images.
else {
long count = countImagesImpl(pageUri,
depth);
} else {
// Synchronously (1) count the number of images on this page
// and (2) crawl other hyperlinks accessible via this page and
// count their images.
long count = countImagesImpl(pageUri, depth);
print(TAG
+ "[Depth"
+ depth
Expand All @@ -95,6 +99,8 @@ else if (mUniqueUris
+ pageUri
+ " in thread "
+ Thread.currentThread().getId());

// Return the count from this level in the traversal.
return count;
}
}
Expand Down Expand Up @@ -164,7 +170,7 @@ private long getCountOfImagesInPage(Document page) {
/**
* Recursively crawl through hyperlinks that are in {@code page}.
*
* @param page The page containing HTML
* @param page The page containing the HTML document
* @param depth The depth of the level of web page traversal
* @return A count of how many images were in each hyperlink on
* the page
Expand Down
2 changes: 1 addition & 1 deletion Java8/ex42/src/main/java/ex42.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This example shows how to count the number of images in a
* recursively-defined folder structure using the Java sequential
* stream framework.
* stream framework and the {@code teeing} {@link Collector}.
*/
public class ex42 {
/**
Expand Down

0 comments on commit 2bc3c14

Please sign in to comment.