From 1c2e491845fa2c3e221ff61b4e27ece8e74f1fbd Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Fri, 24 Jan 2020 13:54:53 -0800 Subject: [PATCH] Minor improvement to statistics Reading raw streams just to map them is not needed. Using arrays instead of lists works fine. --- .../github/GHRepositoryStatistics.java | 38 +++++++------------ .../java/org/kohsuke/github/Requester.java | 3 +- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java b/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java index 4d3f99c54..60e7d9162 100644 --- a/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java +++ b/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java @@ -1,14 +1,12 @@ package org.kohsuke.github; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.exc.MismatchedInputException; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; @@ -317,20 +315,17 @@ public class GHRepositoryStatistics { * the io exception */ public List getCodeFrequency() throws IOException { - // Map to ArrayLists first, since there are no field names in the + // Map to arrays first, since there are no field names in the // returned JSON. try { - InputStream stream = root.createRequest().withUrlPath(getApiTailUrl("code_frequency")).fetchStream(); - - ObjectMapper mapper = new ObjectMapper(); - TypeReference>> typeRef = new TypeReference>>() { - }; - ArrayList> list = mapper.readValue(stream, typeRef); + Integer[][] list = root.createRequest() + .withUrlPath(getApiTailUrl("code_frequency")) + .fetch(Integer[][].class); // Convert to proper objects. - ArrayList returnList = new ArrayList(); - for (ArrayList item : list) { - CodeFrequency cf = new CodeFrequency(item); + List returnList = new ArrayList<>(); + for (Integer[] item : list) { + CodeFrequency cf = new CodeFrequency(Arrays.asList(item)); returnList.add(cf); } @@ -351,7 +346,7 @@ public class GHRepositoryStatistics { private int additions; private int deletions; - private CodeFrequency(ArrayList item) { + private CodeFrequency(List item) { week = item.get(0); additions = item.get(1); deletions = item.get(2); @@ -462,17 +457,12 @@ public class GHRepositoryStatistics { public List getPunchCard() throws IOException { // Map to ArrayLists first, since there are no field names in the // returned JSON. - InputStream stream = root.createRequest().withUrlPath(getApiTailUrl("punch_card")).fetchStream(); - - ObjectMapper mapper = new ObjectMapper(); - TypeReference>> typeRef = new TypeReference>>() { - }; - ArrayList> list = mapper.readValue(stream, typeRef); + Integer[][] list = root.createRequest().withUrlPath(getApiTailUrl("punch_card")).fetch(Integer[][].class); // Convert to proper objects. - ArrayList returnList = new ArrayList(); - for (ArrayList item : list) { - PunchCardItem pci = new PunchCardItem(item); + ArrayList returnList = new ArrayList<>(); + for (Integer[] item : list) { + PunchCardItem pci = new PunchCardItem(Arrays.asList(item)); returnList.add(pci); } @@ -487,7 +477,7 @@ public class GHRepositoryStatistics { private int hourOfDay; private int numberOfCommits; - private PunchCardItem(ArrayList item) { + private PunchCardItem(List item) { dayOfWeek = item.get(0); hourOfDay = item.get(1); numberOfCommits = item.get(2); diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index c7c82f644..4858fc3f4 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -470,7 +470,8 @@ class Requester { } /** - * As stream input stream. + * Response input stream. There are scenarios where direct stream reading is needed, however it is better to use + * {@link #fetch(Class)} where possible. * * @return the input stream * @throws IOException