-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add performance metrics and refactor various classes
Added code to measure execution time in `UsePerson.java`. Refactored `UsePerson.java`, `Person.java`, `Golfer.java`, `FileFilterTest.java`, and `LoopsSortsAndIfs.java` for clarity, usability, and performance. Created `CompanyEmployee.java` and `LazyMessageSupplierTest.java` to implement interfaces and testing, respectively. Made amendments in the test classes `CompanyEmployeeTest.java` and `FileFilterTest.java`.
- Loading branch information
Showing
8 changed files
with
94 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package interfaces; | ||
|
||
public class CompanyEmployee implements Company, Employee { | ||
|
||
private final String first; | ||
private final String last; | ||
|
||
public CompanyEmployee(String first, String last) { | ||
this.first = first; | ||
this.last = last; | ||
} | ||
|
||
public String getName() { | ||
return Employee.super.getName() + " works for " + Company.super.getName(); | ||
} | ||
|
||
@Override | ||
public String getFirst() { | ||
return first; | ||
} | ||
|
||
@Override | ||
public String getLast() { | ||
return last; | ||
} | ||
|
||
@Override | ||
public void doWork() { | ||
System.out.println("Converting caffeine into code for $$$"); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,16 @@ | ||
package refactoring.after; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class LoopsSortsAndIfs { | ||
public static void main(String[] args) { | ||
String[] strings = "this is an array of strings".split(" "); | ||
|
||
List<String> evenLengths = new ArrayList<>(); | ||
for (String s : strings) { | ||
if (s.length() % 2 == 0) { | ||
evenLengths.add(s.toUpperCase()); | ||
} | ||
} | ||
|
||
Collections.sort(evenLengths, new Comparator<String>() { | ||
@Override | ||
public int compare(String s1, String s2) { | ||
return s1.length() - s2.length(); | ||
} | ||
}); | ||
|
||
for (String s : evenLengths) { | ||
System.out.println(s); | ||
} | ||
Arrays.stream("this is an array of strings".split(" ")) | ||
.filter(s -> s.length() % 2 == 0) | ||
.map(String::toUpperCase) | ||
.sorted(Comparator.comparingInt(String::length) | ||
.thenComparing(Comparator.naturalOrder())) | ||
.forEach(System.out::println); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package lambdas; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class LazyMessageSupplierTest { | ||
private final Logger logger = Logger.getLogger(LazyMessageSupplierTest.class.getName()); | ||
|
||
private String getErrorMessage() { | ||
System.out.println("Generating error message..."); | ||
return "An error occurred"; | ||
} | ||
|
||
private String getLogMessage() { | ||
System.out.println("Generating log message..."); | ||
return "A log message"; | ||
} | ||
|
||
@Test | ||
void testBoolean() { | ||
boolean x = true; | ||
logger.fine(getLogMessage()); | ||
assertTrue(x, getErrorMessage()); | ||
} | ||
|
||
@Test | ||
void testBooleanWithLazyErrorMessage() { | ||
boolean x = true; | ||
logger.fine(() -> getLogMessage()); | ||
assertTrue(x, () -> getErrorMessage()); | ||
} | ||
} |