From abb2f8614b7fbc25892e277b55fa216c7d81fd54 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 13 Jul 2015 22:38:13 +1200 Subject: Use switch statemens with strings --- src/org/traccar/Context.java | 18 +++++--- src/org/traccar/geocode/GoogleReverseGeocoder.java | 53 +++++++++++----------- src/org/traccar/http/CommandsServlet.java | 28 ++++++------ src/org/traccar/http/DeviceServlet.java | 26 +++++++---- src/org/traccar/http/MainServlet.java | 28 +++++++----- src/org/traccar/http/PositionServlet.java | 11 +++-- src/org/traccar/http/ServerServlet.java | 18 +++++--- src/org/traccar/http/UserServlet.java | 28 +++++++----- src/org/traccar/protocol/NavisProtocolDecoder.java | 20 ++++---- 9 files changed, 130 insertions(+), 100 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 0b74eacd0..707e6edb6 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -103,14 +103,18 @@ public class Context { } if (config.getBoolean("geocoder.enable")) { - String type = config.getString("geocoder.type"); + String type = config.getString("geocoder.type", "google"); String url = config.getString("geocoder.url"); - if (type != null && type.equals("nominatim")) { - reverseGeocoder = new NominatimReverseGeocoder(url); - } else if (type != null && type.equals("gisgraphy")) { - reverseGeocoder = new GisgraphyReverseGeocoder(url); - } else { - reverseGeocoder = new GoogleReverseGeocoder(); + switch (type) { + case "google": + reverseGeocoder = new GoogleReverseGeocoder(); + break; + case "nominatim": + reverseGeocoder = new NominatimReverseGeocoder(url); + break; + case "gisgraphy": + reverseGeocoder = new GisgraphyReverseGeocoder(url); + break; } } diff --git a/src/org/traccar/geocode/GoogleReverseGeocoder.java b/src/org/traccar/geocode/GoogleReverseGeocoder.java index df8d99545..c37d4188f 100644 --- a/src/org/traccar/geocode/GoogleReverseGeocoder.java +++ b/src/org/traccar/geocode/GoogleReverseGeocoder.java @@ -15,14 +15,9 @@ */ package org.traccar.geocode; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonString; -import org.traccar.helper.Log; public class GoogleReverseGeocoder extends JsonReverseGeocoder { @@ -45,28 +40,34 @@ public class GoogleReverseGeocoder extends JsonReverseGeocoder { String value = component.getString("short_name"); for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) { - if (type.getString().equals("street_number")) { - address.setHouse(value); - break; - } else if (type.getString().equals("route")) { - address.setStreet(value); - break; - } else if (type.getString().equals("locality")) { - address.setSettlement(value); - break; - } else if (type.getString().equals("administrative_area_level_2")) { - address.setDistrict(value); - break; - } else if (type.getString().equals("administrative_area_level_1")) { - address.setState(value); - break; - } else if (type.getString().equals("country")) { - address.setCountry(value); - break; - } else if (type.getString().equals("postal_code")) { - address.setPostcode(value); - break; + + switch (type.getString()) { + case "street_number": + address.setHouse(value); + break; + case "route": + address.setStreet(value); + break; + case "locality": + address.setSettlement(value); + break; + case "administrative_area_level_2": + address.setDistrict(value); + break; + case "administrative_area_level_1": + address.setState(value); + break; + case "country": + address.setCountry(value); + break; + case "postal_code": + address.setPostcode(value); + break; + default: + continue; } + + break; } } diff --git a/src/org/traccar/http/CommandsServlet.java b/src/org/traccar/http/CommandsServlet.java index bf40b79e6..8b3d43fe7 100644 --- a/src/org/traccar/http/CommandsServlet.java +++ b/src/org/traccar/http/CommandsServlet.java @@ -1,29 +1,29 @@ package org.traccar.http; -import org.traccar.Context; -import org.traccar.database.ActiveDevice; -import org.traccar.command.CommandType; -import org.traccar.command.GpsCommand; - import javax.json.Json; import javax.json.JsonObject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.traccar.Context; +import org.traccar.command.CommandType; +import org.traccar.command.GpsCommand; +import org.traccar.database.ActiveDevice; public class CommandsServlet extends BaseServlet { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { - if (command.equals("/send")) { - send(req, resp); - } - else if (command.equals("/raw")) { - sendRawCommand(req, resp); - } - else { - return false; + + switch (command) { + case "/send": + send(req, resp); + return true; + case "/raw": + sendRawCommand(req, resp); + return true; + default: + return false; } - return true; } private void send(HttpServletRequest req, HttpServletResponse resp) throws Exception { diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java index 2a3e15a80..d1af1c106 100644 --- a/src/org/traccar/http/DeviceServlet.java +++ b/src/org/traccar/http/DeviceServlet.java @@ -24,16 +24,22 @@ public class DeviceServlet extends BaseServlet { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { - if (command.equals("/get")) { - get(req, resp); - } else if (command.equals("/add")) { - add(req, resp); - } else if (command.equals("/update")) { - update(req, resp); - } else if (command.equals("/remove")) { - remove(req, resp); - } else { - return false; + + switch (command) { + case "/get": + get(req, resp); + break; + case "/add": + add(req, resp); + break; + case "/update": + update(req, resp); + break; + case "/remove": + remove(req, resp); + break; + default: + return false; } return true; } diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java index 18430f0c3..29eaaec33 100644 --- a/src/org/traccar/http/MainServlet.java +++ b/src/org/traccar/http/MainServlet.java @@ -26,18 +26,24 @@ public class MainServlet extends BaseServlet { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { - if (command.equals("/session")) { - session(req, resp); - } else if (command.equals("/login")) { - login(req, resp); - } else if (command.equals("/logout")) { - logout(req, resp); - } else if (command.equals("/register")) { - register(req, resp); - } else { - return false; + + switch (command) { + case "/session": + session(req, resp); + break; + case "/login": + login(req, resp); + break; + case "/logout": + logout(req, resp); + break; + case "/register": + register(req, resp); + break; + default: + return false; } - return true; + return true; } private void session(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { diff --git a/src/org/traccar/http/PositionServlet.java b/src/org/traccar/http/PositionServlet.java index 57b411a79..6f973be3c 100644 --- a/src/org/traccar/http/PositionServlet.java +++ b/src/org/traccar/http/PositionServlet.java @@ -23,10 +23,15 @@ public class PositionServlet extends BaseServlet { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { - if (command.equals("/get")) { - get(req, resp); + + switch (command) { + case "/get": + get(req, resp); + break; + default: + return false; } - return true; + return true; } private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { diff --git a/src/org/traccar/http/ServerServlet.java b/src/org/traccar/http/ServerServlet.java index d814769a8..836a109fc 100644 --- a/src/org/traccar/http/ServerServlet.java +++ b/src/org/traccar/http/ServerServlet.java @@ -24,14 +24,18 @@ public class ServerServlet extends BaseServlet { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { - if (command.equals("/get")) { - get(req, resp); - } else if (command.equals("/update")) { - update(req, resp); - } else { - return false; + + switch (command) { + case "/get": + get(req, resp); + break; + case "/update": + update(req, resp); + break; + default: + return false; } - return true; + return true; } private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { diff --git a/src/org/traccar/http/UserServlet.java b/src/org/traccar/http/UserServlet.java index 71af74485..976a62890 100644 --- a/src/org/traccar/http/UserServlet.java +++ b/src/org/traccar/http/UserServlet.java @@ -24,18 +24,24 @@ public class UserServlet extends BaseServlet { @Override protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception { - if (command.equals("/get")) { - get(req, resp); - } else if (command.equals("/add")) { - add(req, resp); - } else if (command.equals("/update")) { - update(req, resp); - } else if (command.equals("/remove")) { - remove(req, resp); - } else { - return false; + + switch (command) { + case "/get": + get(req, resp); + break; + case "/add": + add(req, resp); + break; + case "/update": + update(req, resp); + break; + case "/remove": + remove(req, resp); + break; + default: + return false; } - return true; + return true; } private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception { diff --git a/src/org/traccar/protocol/NavisProtocolDecoder.java b/src/org/traccar/protocol/NavisProtocolDecoder.java index 02d408575..46d249fe7 100644 --- a/src/org/traccar/protocol/NavisProtocolDecoder.java +++ b/src/org/traccar/protocol/NavisProtocolDecoder.java @@ -15,19 +15,16 @@ */ package org.traccar.protocol; +import java.net.SocketAddress; import java.nio.ByteOrder; import java.nio.charset.Charset; -import java.net.SocketAddress; import java.util.Calendar; import java.util.LinkedList; import java.util.List; import java.util.TimeZone; - import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; - import org.traccar.BaseProtocolDecoder; import org.traccar.model.Event; import org.traccar.model.Position; @@ -303,13 +300,14 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { // Read message type String type = buf.toString(buf.readerIndex(), 3, charset); buf.skipBytes(type.length()); - - if (type.equals("*>T")) { - return processSingle(channel, buf); - } else if (type.equals("*>A")) { - return processArray(channel, buf); - } else if (type.equals("*>S")) { - return processHandshake(channel, buf); + + switch (type) { + case "*>T": + return processSingle(channel, buf); + case "*>A": + return processArray(channel, buf); + case "*>S": + return processHandshake(channel, buf); } return null; -- cgit v1.2.3