Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascraigschmidt committed Sep 7, 2022
1 parent 28562ad commit 846dcfa
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 123 deletions.
7 changes: 4 additions & 3 deletions Java8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ Here's an overview of what's currently included in these examples:
ReentrantReadWriteLock, synchronized statements, and
VarHandle.

. ex40 - This program demonstrates how to use modern Java features to
build a cosine vector Map from a CSV file containing the
cosine values for movies.
. ex40 - This program demonstrates how to use modern Java features
(including lambda expressions, method references, and
parallel streams) to build a cosine vector Map from a CSV
file containing the cosine values for movies.

. ex41 - This program shows the difference between using traditional
Java 7 features to replace substrings vs. using modern Java
Expand Down
2 changes: 1 addition & 1 deletion Java8/ex10/.run/ex10.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="ex10" type="Application" factoryName="Application">
<option name="ALTERNATIVE_JRE_PATH" value="openjdk-18" />
<option name="ALTERNATIVE_JRE_PATH" value="$USER_HOME$/Library/Java/JavaVirtualMachines/corretto-11.0.14.1/Contents/Home" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="ex10" />
<module name="Example 10" />
Expand Down
2 changes: 1 addition & 1 deletion Java8/ex10/src/ex10.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static public void main(String[] argv) {
Map<String, Integer> stooges = makeMap();

System.out.println(stooges);

// This lambda expression removes entries with IQ less than or
// equal to 100.
stooges.entrySet().removeIf(entry -> entry.getValue() <= 100);
Expand All @@ -50,7 +51,6 @@ static public void main(String[] argv) {
// This lambda expression removes entries with IQ less than or
// equal to 100 with the name "curly".
stooges.entrySet().removeIf(iq.and(curly));

System.out.println(stooges);
}
}
Expand Down
8 changes: 6 additions & 2 deletions Java8/ex40/src/main/java/ex40.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import static java.util.stream.Collectors.toMap;

/**
* This program demonstrates how to use modern Java features to build
* This program demonstrates how to use modern Java features (including
* lambda expressions, method references, and parallel streams) to build
* a cosine vector {@link Map} from a CSV file containing the cosine
* values for movies.
*/
Expand Down Expand Up @@ -59,10 +60,13 @@ public static Map<String, List<Double>> vectorMap(final String dataset) {
*/
private static Map<String, List<Double>> loadCSVFile(String path) {
// Read all lines from filename and convert into a Stream of
// Strings. The "try-with-resources" statement endures the
// Strings. The "try-with-resources" statement ensures the
// Stream cleanup is done automatically!
try (Stream<String> lines = Files.lines(Paths.get(path))) {
return lines
// Convert the stream into a parallel stream.
.parallel()

// Consume the first line, which gives the format of
// the CSV file.
.skip(1)
Expand Down
21 changes: 19 additions & 2 deletions Java8/ex41/src/main/java/ex41.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ class ex41 {
* @return The modified {@link List} of URLs
*/
private static List<String> java7Replace(List<String> urls) {
// Make a copy of the urls.
urls = new ArrayList<>(urls);

// Loop through all the urls.
for (int i = 0; i < urls.size(); ++i) {
// Remove the url at index 'i' if it doesn't
// match what's expected.
if (!urls.get(i).contains("cse.wustl")) {
urls.remove(i);
continue;
}
// Replace the url at index 'i'.
urls.set(i,
urls.get(i).replace("cse.wustl","dre.vanderbilt"));
urls.get(i).replace("cse.wustl",
"dre.vanderbilt"));
}
return urls;
}
Expand All @@ -46,9 +52,17 @@ private static List<String> java7Replace(List<String> urls) {
*/
private static List<String> modernJavaReplace(List<String> urls) {
return urls
// Convert the List to a Stream.
.stream()

// Remove items from the Stream if they don't match what's expected.
.filter(s -> s.contains("cse.wustl"))

// Perform the replacement on each item remaining in the stream.
.map(s -> s.replace("cse.wustl", "dre.vanderbilt"))

// Trigger intermediate processing and collect the results
// into a List.
.collect(toList());
}

Expand All @@ -60,8 +74,11 @@ public static void main (String[] argv) {
List<String> list = Arrays.asList(sUrlArray);
System.out.println(list);

// Perform the replacements and print the results.
// Perform the Java 7 replacements and print the results.
System.out.println(java7Replace(list));

System.out.println(list);
// Perform the modern Java replacements and print the results.
System.out.println(modernJavaReplace((list)));
}
}
2 changes: 1 addition & 1 deletion Java8/ex7/.run/ex7.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="ex7" type="Application" factoryName="Application">
<option name="ALTERNATIVE_JRE_PATH" value="corretto-11" />
<option name="ALTERNATIVE_JRE_PATH" value="openjdk-18" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="CrDemo$CrDemoEx" />
<module name="Example 7" />
Expand Down
111 changes: 0 additions & 111 deletions Java8/ex7/src/CrDemo.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,12 @@
import java.util.function.Supplier;

/**
* This example of shows how the Java functional interfaces (including
* {@link Supplier} and a custom functional interface) can be used in
* conjunction with Java constructor references.
*/
public class CrDemo
implements Runnable {
/**
* This class extends {@link CrDemo} and overrides its {@code
* run()} method to uppercase the string.
*/
private static class CrDemoEx
extends CrDemo {
/**
* Constructor.
*/
CrDemoEx() {
super();
}

/**
* Print the upper-cased value of mString.
*/
@Override
public void run() {
System.out.println(mString.toUpperCase());
}
}

/**
* A field that stores a string.
*/
String mString;

/**
* Main entry point into this program.
*/
public static void main(String[] argv) {
zeroParamConstructorRef();
threeParamConstructorRef();
}

/**
* Default constructor.
*/
Expand All @@ -58,88 +23,12 @@ private CrDemo(String s, Integer i, Long l) {
mString = s + i + l;
}

/**
* Demonstrate how a {@link Supplier} can be used as a factory for
* a zero-parameter constructor reference.
*/
private static void zeroParamConstructorRef() {
// Assign a constructor reference to a supplier that acts as a
// factory for a zero-param object of CrDemo.
Supplier<CrDemo> factory = CrDemo::new;

// Use the factory to create a new instance of CrDemo and call
// its run() method.
CrDemo crDemo = factory.get();
crDemo.run();
}

/**
* Demonstrate how {@link Supplier} objects can be used as
* factories for multiple zero-parameter constructor references.
*/
private static void zeroParamConstructorRefEx() {
// Assign a constructor reference to a supplier that acts as a
// factory for a zero-param object of CrDemo.
Supplier<CrDemo> crDemoFactory = CrDemo::new;

// Assign a constructor reference to a supplier that acts as a
// factory for a zero-param object of CrDemoEx.
Supplier<CrDemoEx> crDemoFactoryEx = CrDemoEx::new;

// This helper method invokes the given supplier to create a
// new object and call its run() method.
runDemo(crDemoFactory);
runDemo(crDemoFactoryEx);
}

/**
* Use the given {@code factory} to create a new object and call
* its {@code run()} method.
*/
private static <T extends Runnable> void runDemo(Supplier<T> factory) {
factory.get().run();
}

/**
* Print the value of mString.
*/
@Override
public void run() {
System.out.println(mString);
}

/**
* Demonstrate how a custom functional interface (i.e., {@link
* TriFactor}, which is defined below) can be used as a factory
* for a three-parameter constructor reference.
*/
private static void threeParamConstructorRef() {
// Assign a constructor reference to a customized functional
// interface that acts as a factory to create a
// three-parameter constructor for CrDemo.
TriFactory<String, Integer, Long, CrDemo> factory =
CrDemo::new;

// Use the factory to create a new instance of CrDemo and call
// its run() method.
factory.of("The answer is ", 4, 2L).run();
}

/**
* Represents a factory that accepts three arguments and produces
* a result. This is a functional interface whose abstract method
* is {@link #of(Object, Object, Object)}.
*/
@FunctionalInterface
interface TriFactory<P1,
P2,
P3,
R> {
/**
* Create an object of type {@code R} using the three
* parameters and return the object.
*/
R of(P1 p1, P2 p2, P3 p3);
}
}

22 changes: 22 additions & 0 deletions Java8/ex7/src/CrDemoEx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This class extends {@link CrDemo} and overrides its {@code run()}
* method to uppercase the string.
*/
public class CrDemoEx
extends CrDemo {
/**
* Constructor.
*/
CrDemoEx() {
super();
}

/**
* Print the upper-cased value of mString.
*/
@Override
public void run() {
System.out.println(mString.toUpperCase());
}
}

99 changes: 99 additions & 0 deletions Java8/ex7/src/ex7.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.util.function.Supplier;

/**
* This example of shows how the Java functional interfaces (including
* {@link Supplier} and a custom functional interface) can be used in
* conjunction with Java constructor references.
*/
public class ex7 {
/**
* Main entry point into this program.
*/
public static void main(String[] argv) {
zeroParamConstructorRef();
zeroParamConstructorRefEx();
threeParamConstructorRef();
}

/**
* Demonstrate how a {@link Supplier} can be used as a factory for
* a zero-parameter constructor reference.
*/
private static void zeroParamConstructorRef() {
System.out.println("zeroParamConstructorRef()");

// Assign a constructor reference to a supplier that acts as a
// factory for a zero-param object of CrDemo.
Supplier<CrDemo> factory = CrDemo::new;

// Use the factory to create a new instance of CrDemo and call
// its run() method.
CrDemo crDemo = factory.get();
crDemo.run();
}

/**
* Demonstrate how {@link Supplier} objects can be used as
* factories for multiple zero-parameter constructor references.
*/
private static void zeroParamConstructorRefEx() {
System.out.println("zeroParamConstructorRefEx()");

// Assign a constructor reference to a supplier that acts as a
// factory for a zero-param object of CrDemo.
Supplier<CrDemo> crDemoFactory = CrDemo::new;

// Assign a constructor reference to a supplier that acts as a
// factory for a zero-param object of CrDemoEx.
Supplier<CrDemoEx> crDemoFactoryEx = CrDemoEx::new;

// This helper method invokes the given supplier to create a
// new object and call its run() method.
runDemo(crDemoFactory);
runDemo(crDemoFactoryEx);
}

/**
* Use the given {@code factory} to create a new object and call
* its {@code run()} method.
*/
private static <T extends Runnable> void runDemo(Supplier<T> factory) {
factory.get().run();
}

/**
* Represents a factory that accepts three arguments and produces
* a result. This is a functional interface whose abstract method
* is {@link #of(Object, Object, Object)}.
*/
@FunctionalInterface
interface TriFactory<P1,
P2,
P3,
R> {
/**
* Create an object of type {@code R} using the three
* parameters and return the object.
*/
R of(P1 p1, P2 p2, P3 p3);
}

/**
* Demonstrate how a custom functional interface (i.e., {@link
* TriFactory}, which is defined below) can be used as a factory
* for a three-parameter constructor reference.
*/
public static void threeParamConstructorRef() {
System.out.println("threeParamConstructorRef()");

// Assign a constructor reference to a customized functional
// interface that acts as a factory to create a
// three-parameter constructor for CrDemo.
TriFactory<String, Integer, Long, CrDemo> factory =
CrDemo::new;

// Use the factory to create a new instance of CrDemo and call
// its run() method.
factory.of("The answer is ", 4, 2L).run();
}
}
Loading

0 comments on commit 846dcfa

Please sign in to comment.