Skip to content

Commit

Permalink
Updating code example
Browse files Browse the repository at this point in the history
  • Loading branch information
wkorando committed Jul 23, 2021
1 parent b6771d1 commit 6103681
Show file tree
Hide file tree
Showing 12 changed files with 260,832 additions and 121,669 deletions.
1,001 changes: 0 additions & 1,001 deletions xxx-read-file-improvements/data-large.csv

This file was deleted.

120,574 changes: 0 additions & 120,574 deletions xxx-read-file-improvements/data-very-large.csv

This file was deleted.

12 changes: 2 additions & 10 deletions xxx-read-file-improvements/data.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
Reporting Period,Project Number,Legacy Project Number,City,,,,,,,,,,
6/30/21,305,01001-00048,Babylon,,,,,,,,,,
6/30/21,2011-188607-SLPR,,Syosset,,,,,,,,,,
6/30/21,2011-157262-SLPR,,Bellport,,,,,,,,,,
6/30/21,256419,,Queens,,,,,,,,,,
5/30/21,2014-655036-Sola,,Babylon,,,,,,,,,,
5/30/21,310687,,Babylon,,,,,,,,,,
5/30/21,2014-648451-Sola,,Centereach,,,,,,,,,,
5/30/21,2011-157191-SLPR,,Water Mill,,,,,,,,,,
4/30/21,2013-636596-Sola,,Farmingdale,,,,,,,,,,
Reporting Period,Project Number,Legacy Project Number,City,County,State,Zip Code,Sector,Program Type,Solicitation,Electric Utility,Purchase Type,Date Application Received,Date Completed,Project Status,Contractor,Primary Inverter Manufacturer,Primary Inverter Model Number,Total Inverter Quantity,Primary PV Module Manufacturer,PV Module Model Number,Total PV Module Quantity,Project Cost,$Incentive,Total Nameplate kW DC,Expected KWh Annual Production,Remote Net Metering,Affordable Solar,Community Distributed Generation,Green Jobs Green New York Participant,Georeference
06/30/2021,0000000305,01001-00048,Williamson,Wayne,NY,14589,Non-Residential,Residential/Small Commercial,PON 1184,,,08/03/2003,06/23/2005,Complete,"Solar Works, Inc.",Fronius USA,IG 2500-LV,1,BP Solar,BP3125S,16,,,2.00,,,No,No,No,POINT (-77.175308 43.241518)
120,574 changes: 120,574 additions & 0 deletions xxx-read-file-improvements/data2.csv

Large diffs are not rendered by default.

120,574 changes: 120,574 additions & 0 deletions xxx-read-file-improvements/data3.csv

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions xxx-read-file-improvements/script.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
Hey Java developers,

Reading files is difficult?
Finding your self getting lost in a max of if statements and for loops when processing large files?

Make it easier!
[Where am I?!]

Try using streams instead

Stream, added in Java 8, and updated since then, provide many benefits when performing complex processing of large data sets

In this example, to group, order, pr

Happy coding!
44 changes: 44 additions & 0 deletions xxx-read-file-improvements/src/main/java/ElectricProject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public record ElectricProject(LocalDate reportingPeriod, String projectNumber, String legacyProjectNumber, String city,
String county, String state, String zipCode, String sector, String programType, String solicitation,
String electricUtility, String purchaseType, LocalDate dateApplicationReceived, LocalDate dateCompleted,
String projectStatus, String contractor, String primaryInverterManufacturer, String primaryInverterModelNumber,
BigDecimal totalInverterQuantity, String primaryPVModuleManufacturer, String pvModuleModelNumber,
BigDecimal totalPVModuleQuantity, BigDecimal projectCost, BigDecimal incentive, BigDecimal totalNameplatekWDC,
BigDecimal expectedKWhAnnualProduction, String remoteNetMetering, String affordableSolar,
String communityDistributed, String generationGreenJobsGreenNewYorkParticipant, String georeference) {

static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
boolean reportingPeriodMatches(Month month) {
return this.reportingPeriod.getMonth().equals(month);
}

static BigDecimal handleNumeric(String value) {
try {
return new BigDecimal(value);
} catch (Exception e) {
return BigDecimal.valueOf(0);
}
}

static LocalDate handleDate(String value) {
if (value.isBlank()) {
return null;
} else {
return LocalDate.parse(value, formatter);
}
}

static ElectricProject map(String[] values) {
return new ElectricProject(handleDate(values[0]), values[1], values[2], values[3], values[4], values[5],
values[6], values[7], values[8], values[9], values[10], values[11], handleDate(values[12]),
handleDate(values[13]), values[14], values[15], values[16], values[17], handleNumeric(values[18]),
values[19], values[20], handleNumeric(values[21]), handleNumeric(values[22]), handleNumeric(values[23]),
handleNumeric(values[24]), handleNumeric(values[25]), values[26], values[27], values[28], values[29],
values[30]);
}
}
49 changes: 31 additions & 18 deletions xxx-read-file-improvements/src/main/java/FileReaderI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,52 @@
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class FileReaderI {
public static void main(String[] args) {

record Project(LocalDate reportingPeriod, String projectNumber, String legacyProjectNumber, String city) {
}
List<Project> projectsList = new ArrayList<>();
String filename = args[0];
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
Month currentMonth = Month.JUNE;


SortedMap<String, ElectricProject> projectsByCity = new TreeMap<>();
String filename = args[0];

try (BufferedReader br = new BufferedReader(new FileReader(new File(filename)))) {
String line = br.readLine();

while (line != null) {
if (!line.startsWith("Reporting Period")) {
String[] values = line.split(",");
Project project = new Project(LocalDate.parse(values[0], DateTimeFormatter.ofPattern("M/dd/yy")),
values[1], values[2], values[3]);
if(project.reportingPeriod.getMonth().equals(currentMonth)) {
projectsList.add(project);
} else {
break;
if (LocalDate.parse(values[0], formatter).getMonth().equals(currentMonth)) {
try {
ElectricProject project = ElectricProject.map(values);
if (!projectsByCity.containsKey(project.city())) {
projectsByCity.put(project.city(), project);
} else {
ElectricProject existingProject = projectsByCity.get(project.city());
if (project.expectedKWhAnnualProduction()
.compareTo(existingProject.expectedKWhAnnualProduction()) > 0) {
projectsByCity.put(project.city(), project);
}
}
} catch (Exception e) {
// Do some cool stuff to handle errors
}
}
}
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(projectsList.size());

for (ElectricProject project : projectsByCity.values()) {
System.out.println("""
City: %s
Project Number: %s
Expected Output: %s
""".formatted(project.city(), project.projectNumber(),
project.expectedKWhAnnualProduction().toString()));
}
}
}
52 changes: 33 additions & 19 deletions xxx-read-file-improvements/src/main/java/FileReaderII.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,56 @@
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.function.Function;
import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FileReaderII {
public static void main(String[] args) {

record Project(LocalDate reportingPeriod, String projectNumber, String legacyProjectNumber, String city) {
}
List<Project> projectsList = null;
String filename = args[0];
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
Month currentMonth = Month.JUNE;
String filename = args[0];

Function<String, Stream<Project>> flatMapper = line -> {
BiConsumer<String, Consumer<ElectricProject>> retrieveResultsForCurrentMonth = (line, consumer) -> {
String[] values = line.split(",");
Project project = new Project(LocalDate.parse(values[0], DateTimeFormatter.ofPattern("M/dd/yy")), values[1],
values[2], values[3]);
return Stream.of(project);
if (LocalDate.parse(values[0], formatter).getMonth().equals(currentMonth)) {
try {
consumer.accept(ElectricProject.map(values));
} catch (Exception e) {
// Do some cool stuff to handle errors
}
}
};

Predicate<String> titleLinePredicate = line -> {
return !line.startsWith("Reporting Period");
Consumer<ElectricProject> printResults = project -> {
System.out.println("""
City: %s
Project Number: %s
Expected Output: %s
""".formatted(project.city(), project.projectNumber(),
project.expectedKWhAnnualProduction().toString()));
};

Predicate<Project> thisMonth = project -> {
return project.reportingPeriod.getMonth().equals(currentMonth);
Predicate<String> isTitleLine = line -> line.startsWith("Reporting Period");

BinaryOperator<ElectricProject> findBestProducingProject = (newProject, currentProject) -> {
if (newProject.expectedKWhAnnualProduction().compareTo(currentProject.expectedKWhAnnualProduction()) > 0) {
return newProject;
} else {
return currentProject;
}
};

try (Stream<String> lines = Files.lines(Path.of(filename))) {
projectsList = lines.filter(titleLinePredicate).flatMap(flatMapper).takeWhile(thisMonth).toList();
lines.filter(isTitleLine.negate()).mapMulti(retrieveResultsForCurrentMonth) //
.collect(Collectors.toMap(ElectricProject::city, p -> p, findBestProducingProject, TreeMap::new))
.values().stream().forEach(printResults);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(projectsList.size());
}

}
45 changes: 0 additions & 45 deletions xxx-read-file-improvements/src/main/java/FileReaderIII.java

This file was deleted.

Loading

0 comments on commit 6103681

Please sign in to comment.