commit 59b315716133f5da7c11c724cb7be9b3f3c3464a Author: Karsten Loesing karsten.loesing@gmx.net Date: Thu Aug 31 21:57:22 2017 +0200
Use switch-on-String in multiple places. --- .../java/org/torproject/metrics/clients/Main.java | 42 ++++++---- .../cron/RelayDescriptorDatabaseImporter.java | 31 ++++--- .../org/torproject/metrics/web/GraphServlet.java | 95 ++++++++++------------ .../org/torproject/metrics/web/NewsServlet.java | 29 ++++--- .../metrics/web/graphs/RObjectGenerator.java | 13 ++- 5 files changed, 117 insertions(+), 93 deletions(-)
diff --git a/modules/clients/src/main/java/org/torproject/metrics/clients/Main.java b/modules/clients/src/main/java/org/torproject/metrics/clients/Main.java index 384c621..ea198ff 100644 --- a/modules/clients/src/main/java/org/torproject/metrics/clients/Main.java +++ b/modules/clients/src/main/java/org/torproject/metrics/clients/Main.java @@ -296,12 +296,18 @@ public class Main { * greater than 4, put in a default that we'll attribute all responses * to. */ if (total == 0) { - if (category.equals("country")) { - frequenciesCopy.put("??", 4.0); - } else if (category.equals("transport")) { - frequenciesCopy.put("<OR>", 4.0); - } else if (category.equals("version")) { - frequenciesCopy.put("v4", 4.0); + switch (category) { + case "country": + frequenciesCopy.put("??", 4.0); + break; + case "transport": + frequenciesCopy.put("<OR>", 4.0); + break; + case "version": + frequenciesCopy.put("v4", 4.0); + break; + default: + /* Ignore any other categories. */ } total = 4.0; } @@ -309,15 +315,21 @@ public class Main { double intervalFraction = ((double) (toMillis - fromMillis)) / ((double) dirreqStatsIntervalLengthMillis); double val = resp * intervalFraction * e.getValue() / total; - if (category.equals("country")) { - writeOutputLine(fingerprint, "bridge", "responses", e.getKey(), - "", "", fromMillis, toMillis, val, publishedMillis); - } else if (category.equals("transport")) { - writeOutputLine(fingerprint, "bridge", "responses", "", - e.getKey(), "", fromMillis, toMillis, val, publishedMillis); - } else if (category.equals("version")) { - writeOutputLine(fingerprint, "bridge", "responses", "", "", - e.getKey(), fromMillis, toMillis, val, publishedMillis); + switch (category) { + case "country": + writeOutputLine(fingerprint, "bridge", "responses", e.getKey(), + "", "", fromMillis, toMillis, val, publishedMillis); + break; + case "transport": + writeOutputLine(fingerprint, "bridge", "responses", "", + e.getKey(), "", fromMillis, toMillis, val, publishedMillis); + break; + case "version": + writeOutputLine(fingerprint, "bridge", "responses", "", "", + e.getKey(), fromMillis, toMillis, val, publishedMillis); + break; + default: + /* Ignore any other categories. */ } } } diff --git a/modules/legacy/src/main/java/org/torproject/ernie/cron/RelayDescriptorDatabaseImporter.java b/modules/legacy/src/main/java/org/torproject/ernie/cron/RelayDescriptorDatabaseImporter.java index f214f4c..a05c86c 100644 --- a/modules/legacy/src/main/java/org/torproject/ernie/cron/RelayDescriptorDatabaseImporter.java +++ b/modules/legacy/src/main/java/org/torproject/ernie/cron/RelayDescriptorDatabaseImporter.java @@ -747,18 +747,25 @@ public final class RelayDescriptorDatabaseImporter { int offset = (int) (lastIntervalTime / (15L * 60L * 1000L)) - longValues.length + 1; String type = parts[2]; - if (type.equals("read-history")) { - readArray = longValues; - readOffset = offset; - } else if (type.equals("write-history")) { - writtenArray = longValues; - writtenOffset = offset; - } else if (type.equals("dirreq-read-history")) { - dirreadArray = longValues; - dirreadOffset = offset; - } else if (type.equals("dirreq-write-history")) { - dirwrittenArray = longValues; - dirwrittenOffset = offset; + switch (type) { + case "read-history": + readArray = longValues; + readOffset = offset; + break; + case "write-history": + writtenArray = longValues; + writtenOffset = offset; + break; + case "dirreq-read-history": + dirreadArray = longValues; + dirreadOffset = offset; + break; + case "dirreq-write-history": + dirwrittenArray = longValues; + dirwrittenOffset = offset; + break; + default: + /* Ignore any other types. */ } lastDate = currentDate; } diff --git a/website/src/main/java/org/torproject/metrics/web/GraphServlet.java b/website/src/main/java/org/torproject/metrics/web/GraphServlet.java index 12652dc..b376be5 100644 --- a/website/src/main/java/org/torproject/metrics/web/GraphServlet.java +++ b/website/src/main/java/org/torproject/metrics/web/GraphServlet.java @@ -166,58 +166,53 @@ public class GraphServlet extends MetricServlet { request.getParameterMap()); StringBuilder urlBuilder = new StringBuilder(); for (String parameter : this.parameters.get(requestedId)) { - if (parameter.equals("start") || parameter.equals("end")) { - String[] requestParameter; - if (checkedParameters != null - && checkedParameters.containsKey(parameter)) { - requestParameter = checkedParameters.get(parameter); - } else { - requestParameter = new String[] { - dateFormat.format(parameter.equals("start") - ? defaultStartDate : defaultEndDate) }; - } - urlBuilder.append(String.format("&%s=%s", parameter, - requestParameter[0])); - request.setAttribute(parameter, requestParameter); - } else if (parameter.equals("p") - || parameter.equals("n") - || parameter.equals("flag") - || parameter.equals("country") - || parameter.equals("events") - || parameter.equals("transport") - || parameter.equals("version") - || parameter.equals("source") - || parameter.equals("server") - || parameter.equals("filesize")) { - String[][] defaultParameters = - this.defaultParameters.get(parameter); - String[][] requestParameters = - new String[defaultParameters.length][]; - Set<String> checked = null; - if (checkedParameters != null - && checkedParameters.containsKey(parameter)) { - checked = new HashSet<>(Arrays.asList( - checkedParameters.get(parameter))); - } - String checkedOrSelected = parameter.equals("country") - || parameter.equals("events") || parameter.equals("version") - ? " selected" : " checked"; - for (int i = 0; i < defaultParameters.length; i++) { - requestParameters[i] = - new String[defaultParameters[i].length]; - System.arraycopy(defaultParameters[i], 0, - requestParameters[i], 0, defaultParameters[i].length); - if (checked != null) { - if (checked.contains(requestParameters[i][0])) { - requestParameters[i][1] = checkedOrSelected; - urlBuilder.append(String.format("&%s=%s", parameter, - requestParameters[i][0])); - } else { - requestParameters[i][1] = ""; + switch (parameter) { + case "start": + case "end": + String[] requestParameter; + if (checkedParameters != null + && checkedParameters.containsKey(parameter)) { + requestParameter = checkedParameters.get(parameter); + } else { + requestParameter = new String[] { + dateFormat.format(parameter.equals("start") + ? defaultStartDate : defaultEndDate) }; + } + urlBuilder.append(String.format("&%s=%s", parameter, + requestParameter[0])); + request.setAttribute(parameter, requestParameter); + break; + default: + String[][] defaultParameters = + this.defaultParameters.get(parameter); + String[][] requestParameters = + new String[defaultParameters.length][]; + Set<String> checked = null; + if (checkedParameters != null + && checkedParameters.containsKey(parameter)) { + checked = new HashSet<>(Arrays.asList( + checkedParameters.get(parameter))); + } + String checkedOrSelected = parameter.equals("country") + || parameter.equals("events") || parameter.equals("version") + ? " selected" : " checked"; + for (int i = 0; i < defaultParameters.length; i++) { + requestParameters[i] = + new String[defaultParameters[i].length]; + System.arraycopy(defaultParameters[i], 0, + requestParameters[i], 0, defaultParameters[i].length); + if (checked != null) { + if (checked.contains(requestParameters[i][0])) { + requestParameters[i][1] = checkedOrSelected; + urlBuilder.append(String.format("&%s=%s", parameter, + requestParameters[i][0])); + } else { + requestParameters[i][1] = ""; + } } } - } - request.setAttribute(parameter, requestParameters); + request.setAttribute(parameter, requestParameters); + break; } } if (urlBuilder.length() > 5) { diff --git a/website/src/main/java/org/torproject/metrics/web/NewsServlet.java b/website/src/main/java/org/torproject/metrics/web/NewsServlet.java index 6dfaba7..1dfea4c 100644 --- a/website/src/main/java/org/torproject/metrics/web/NewsServlet.java +++ b/website/src/main/java/org/torproject/metrics/web/NewsServlet.java @@ -103,18 +103,23 @@ public class NewsServlet extends AnyServlet { } if (news.getProtocols() != null) { for (String protocol : news.getProtocols()) { - if (protocol.equals("relay")) { - sb.append(" <span class="label label-success">" - + "Relays</span>"); - } else if (protocol.equals("bridge")) { - sb.append(" <span class="label label-primary">" - + "Bridges</span>"); - } else if (protocol.equals("<OR>")) { - sb.append(" <span class="label label-info">" - + "<OR></span>"); - } else { - sb.append(" <span class="label label-info">" - + protocol + "</span>"); + switch (protocol) { + case "relay": + sb.append(" <span class="label label-success">" + + "Relays</span>"); + break; + case "bridge": + sb.append(" <span class="label label-primary">" + + "Bridges</span>"); + break; + case "<OR>": + sb.append(" <span class="label label-info">" + + "<OR></span>"); + break; + default: + sb.append(" <span class="label label-info">" + + protocol + "</span>"); + break; } } } diff --git a/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java b/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java index fa9e1d8..719a5c4 100644 --- a/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java +++ b/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java @@ -62,10 +62,15 @@ public class RObjectGenerator implements ServletContextListener { for (Metric metric : ContentProvider.getInstance().getMetricsList()) { String type = metric.getType(); String id = metric.getId(); - if ("Graph".equals(type)) { - this.availableGraphs.put(id, metric); - } else if ("Table".equals(type)) { - this.availableTables.put(id, metric); + switch (type) { + case "Graph": + this.availableGraphs.put(id, metric); + break; + case "Table": + this.availableTables.put(id, metric); + break; + default: + /* Just skip any other types. */ } }
tor-commits@lists.torproject.org