Created
August 7, 2020 06:12
-
-
Save armstnp/4cb82f9f2e6917f49097d992754ed528 to your computer and use it in GitHub Desktop.
Java Anonymous Classes and Lambdas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
/*** | |
* Tests collections of samples all at once. | |
*/ | |
class BatchTestingMachine { | |
public List<String> processBatch(List<PatientSample> batch, ImmunityTest test) { | |
List<String> results = new ArrayList<>(batch.size()); | |
for(PatientSample sample : batch){ | |
String result = generateResult(sample, test); | |
results.add(result); | |
} | |
return results; | |
} | |
private String generateResult(PatientSample sample, ImmunityTest test) { | |
return sample.getName() + | |
(test.isImmune(sample) | |
? " has a clean bill of health!" | |
: " is sentenced to quarantine."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* An immunity test tailored to Covid19. | |
* | |
* Author's caveat: do not take this as a realistic test. This is entirely fictitious. :) | |
*/ | |
class Covid19ImmunityTest implements ImmunityTest { | |
@Override | |
public boolean isImmune(PatientSample sample){ | |
return sample.hasTCellMemory() || sample.hasAntibodyDensityOver(0.2); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* A test, returning whether a sample indicates immunity to some disease. | |
*/ | |
@FunctionalInterface | |
interface ImmunityTest { | |
boolean isImmune(PatientSample sample); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
/*** | |
* Path A - Use a named class for our test. | |
*/ | |
public class PathA { | |
public static void main(String[] args){ | |
List<PatientSample> batch = Arrays.asList( | |
new PatientSample("Alfred", 0.2, false), | |
new PatientSample("Betty", 0.5, false), | |
new PatientSample("Carlos", 0.1, true), | |
new PatientSample("Dietrich", 0.05, false), | |
new PatientSample("Evans", 0.9, true), | |
new PatientSample("Fran", 0.0, false) | |
); | |
BatchTestingMachine machine = new BatchTestingMachine(); | |
ImmunityTest test = new Covid19ImmunityTest(); | |
List<String> results = machine.processBatch(batch, test); | |
for(String result : results) { | |
System.out.println(result); | |
} | |
/* | |
Output: | |
Alfred has a clean bill of health! | |
Betty has a clean bill of health! | |
Carlos has a clean bill of health! | |
Dietrich is sentenced to quarantine. | |
Evans has a clean bill of health! | |
Fran is sentenced to quarantine. | |
*/ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
/*** | |
* Path B - Use an anonymous class for our test. | |
*/ | |
public class PathB { | |
public static void main(String[] args){ | |
List<PatientSample> batch = Arrays.asList( | |
new PatientSample("Alfred", 0.2, false), | |
new PatientSample("Betty", 0.5, false), | |
new PatientSample("Carlos", 0.1, true), | |
new PatientSample("Dietrich", 0.05, false), | |
new PatientSample("Evans", 0.9, true), | |
new PatientSample("Fran", 0.0, false) | |
); | |
BatchTestingMachine machine = new BatchTestingMachine(); | |
ImmunityTest test = new ImmunityTest() { // Note this new class doesn't have a name of its own! | |
@Override | |
public boolean isImmune(PatientSample sample){ | |
return sample.hasTCellMemory() || sample.hasAntibodyDensityOver(0.2); | |
} | |
}; | |
List<String> results = machine.processBatch(batch, test); | |
for(String result : results) { | |
System.out.println(result); | |
} | |
/* | |
Output: | |
Alfred has a clean bill of health! | |
Betty has a clean bill of health! | |
Carlos has a clean bill of health! | |
Dietrich is sentenced to quarantine. | |
Evans has a clean bill of health! | |
Fran is sentenced to quarantine. | |
*/ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
/*** | |
* Path C - Use a lambda for our test. | |
*/ | |
public class PathC { | |
public static void main(String[] args){ | |
List<PatientSample> batch = Arrays.asList( | |
new PatientSample("Alfred", 0.2, false), | |
new PatientSample("Betty", 0.5, false), | |
new PatientSample("Carlos", 0.1, true), | |
new PatientSample("Dietrich", 0.05, false), | |
new PatientSample("Evans", 0.9, true), | |
new PatientSample("Fran", 0.0, false) | |
); | |
BatchTestingMachine machine = new BatchTestingMachine(); | |
ImmunityTest test = (PatientSample sample) -> sample.hasTCellMemory() || sample.hasAntibodyDensityOver(0.2); | |
// OR, even simpler: | |
// | |
// ImmunityTest test = sample -> sample.hasTCellMemory() || sample.hasAntibodyDensityOver(0.2); | |
List<String> results = machine.processBatch(batch, test); | |
for(String result : results) { | |
System.out.println(result); | |
} | |
/* | |
Output: | |
Alfred has a clean bill of health! | |
Betty has a clean bill of health! | |
Carlos has a clean bill of health! | |
Dietrich is sentenced to quarantine. | |
Evans has a clean bill of health! | |
Fran is sentenced to quarantine. | |
*/ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Data from a sample we've taken from a patient. | |
*/ | |
class PatientSample { | |
private final String name; | |
private final double antibodyDensity; | |
private final boolean hasTCellMemory; | |
public PatientSample(String name, double antibodyDensity, boolean hasTCellMemory){ | |
this.name = name; | |
this.antibodyDensity = antibodyDensity; | |
this.hasTCellMemory = hasTCellMemory; | |
} | |
public String getName(){ | |
return name; | |
} | |
public boolean hasAntibodyDensityOver(double minimum){ | |
return antibodyDensity >= minimum; | |
} | |
public boolean hasTCellMemory(){ | |
return hasTCellMemory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment