Improved recycling of HashMaps

This commit is contained in:
Rohit Awate 2018-05-13 21:48:38 +05:30
parent c2f12d146e
commit c90a22cc7b
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
10 changed files with 67 additions and 50 deletions

View file

@ -135,24 +135,16 @@ public class BodyTabController implements Initializable {
try { try {
switch (dashboardState.getContentType()) { switch (dashboardState.getContentType()) {
case MediaType.TEXT_PLAIN: case MediaType.TEXT_PLAIN:
rawInputArea.setText(dashboardState.getBody()); setRawTab(dashboardState, "PLAIN TEXT");
rawInputTypeBox.getSelectionModel().select("PLAIN TEXT");
bodyTabPane.getSelectionModel().select(rawTab);
break; break;
case MediaType.APPLICATION_JSON: case MediaType.APPLICATION_JSON:
rawInputArea.setText(dashboardState.getBody()); setRawTab(dashboardState, "JSON");
rawInputTypeBox.getSelectionModel().select("JSON");
bodyTabPane.getSelectionModel().select(rawTab);
break; break;
case MediaType.APPLICATION_XML: case MediaType.APPLICATION_XML:
rawInputArea.setText(dashboardState.getBody()); setRawTab(dashboardState, "XML");
rawInputTypeBox.getSelectionModel().select("XML");
bodyTabPane.getSelectionModel().select(rawTab);
break; break;
case MediaType.TEXT_HTML: case MediaType.TEXT_HTML:
rawInputArea.setText(dashboardState.getBody()); setRawTab(dashboardState, "HTML");
rawInputTypeBox.getSelectionModel().select("HTML");
bodyTabPane.getSelectionModel().select(rawTab);
break; break;
case MediaType.MULTIPART_FORM_DATA: case MediaType.MULTIPART_FORM_DATA:
// For file tuples // For file tuples
@ -178,4 +170,10 @@ public class BodyTabController implements Initializable {
Services.loggingService.logInfo("Dashboard loaded with blank request body.", LocalDateTime.now()); Services.loggingService.logInfo("Dashboard loaded with blank request body.", LocalDateTime.now());
} }
} }
private void setRawTab(DashboardState dashboardState, String contentType) {
rawInputArea.setText(dashboardState.getBody());
rawInputTypeBox.getSelectionModel().select(contentType);
bodyTabPane.getSelectionModel().select(rawTab);
}
} }

View file

@ -98,6 +98,7 @@ public class DashboardController implements Initializable {
private GETRequest getRequest; private GETRequest getRequest;
private DataDispatchRequest dataRequest; private DataDispatchRequest dataRequest;
private DELETERequest deleteRequest; private DELETERequest deleteRequest;
private HashMap<String, String> params;
@Override @Override
public void initialize(URL url, ResourceBundle rb) { public void initialize(URL url, ResourceBundle rb) {
@ -190,7 +191,7 @@ public class DashboardController implements Initializable {
getRequest = new GETRequest(); getRequest = new GETRequest();
getRequest.setTarget(addressField.getText()); getRequest.setTarget(addressField.getText());
getRequest.setHeaders(headerTabController.getHeaders()); getRequest.setHeaders(headerTabController.getSelectedHeaders());
requestManager = Services.pool.get(); requestManager = Services.pool.get();
requestManager.setRequest(getRequest); requestManager.setRequest(getRequest);
@ -207,7 +208,7 @@ public class DashboardController implements Initializable {
dataRequest.setRequestType(httpMethodBox.getValue()); dataRequest.setRequestType(httpMethodBox.getValue());
dataRequest.setTarget(addressField.getText()); dataRequest.setTarget(addressField.getText());
dataRequest.setHeaders(headerTabController.getHeaders()); dataRequest.setHeaders(headerTabController.getSelectedHeaders());
if (bodyTabController.rawTab.isSelected()) { if (bodyTabController.rawTab.isSelected()) {
String contentType; String contentType;
@ -253,7 +254,7 @@ public class DashboardController implements Initializable {
deleteRequest = new DELETERequest(); deleteRequest = new DELETERequest();
deleteRequest.setTarget(addressField.getText()); deleteRequest.setTarget(addressField.getText());
deleteRequest.setHeaders(headerTabController.getHeaders()); deleteRequest.setHeaders(headerTabController.getSelectedHeaders());
requestManager = Services.pool.delete(); requestManager = Services.pool.delete();
requestManager.setRequest(deleteRequest); requestManager.setRequest(deleteRequest);
@ -430,8 +431,10 @@ public class DashboardController implements Initializable {
} }
private HashMap<String, String> getParams() { private HashMap<String, String> getParams() {
HashMap<String, String> params = new HashMap<>(); if (params == null)
params = new HashMap<>();
params.clear();
for (StringKeyValueFieldController controller : paramsControllers) for (StringKeyValueFieldController controller : paramsControllers)
if (controller.isChecked()) if (controller.isChecked())
params.put(controller.getHeader().getKey(), controller.getHeader().getValue()); params.put(controller.getHeader().getKey(), controller.getHeader().getValue());
@ -500,7 +503,7 @@ public class DashboardController implements Initializable {
case "PUT": case "PUT":
case "PATCH": case "PATCH":
dashboardState = new DashboardState(bodyTabController.getBasicRequest(httpMethodBox.getValue())); dashboardState = new DashboardState(bodyTabController.getBasicRequest(httpMethodBox.getValue()));
dashboardState.setHeaders(headerTabController.getHeaders()); dashboardState.setHeaders(headerTabController.getSelectedHeaders());
break; break;
default: default:
// For GET, DELETE requests // For GET, DELETE requests
@ -513,7 +516,7 @@ public class DashboardController implements Initializable {
Services.loggingService.logInfo("Dashboard state was saved with an invalid URL.", LocalDateTime.now()); Services.loggingService.logInfo("Dashboard state was saved with an invalid URL.", LocalDateTime.now());
} }
dashboardState.setHttpMethod(httpMethodBox.getValue()); dashboardState.setHttpMethod(httpMethodBox.getValue());
dashboardState.setHeaders(headerTabController.getHeaders()); dashboardState.setHeaders(headerTabController.getSelectedHeaders());
dashboardState.setParams(getParams()); dashboardState.setParams(getParams());
return dashboardState; return dashboardState;

View file

@ -44,6 +44,9 @@ public class FormDataTabController implements Initializable {
private List<FileKeyValueFieldController> fileControllers; private List<FileKeyValueFieldController> fileControllers;
private IntegerProperty fileControllersCount, stringControllersCount; private IntegerProperty fileControllersCount, stringControllersCount;
private HashMap<String, String> stringMap;
private HashMap<String, String> fileMap;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
stringControllers = new ArrayList<>(); stringControllers = new ArrayList<>();
@ -56,24 +59,22 @@ public class FormDataTabController implements Initializable {
addStringField(); addStringField();
} }
private void addFileField() {
addFileField("", "", null);
}
@FXML @FXML
private void addFileField(ActionEvent event) { private void addFileField(ActionEvent event) {
addFileField("", "", event); addFileField("", "", event);
} }
private void addFileField() {
addFileField("", "", null);
}
public void addFileField(String key, String value) { public void addFileField(String key, String value) {
addFileField(key, value, null); addFileField(key, value, null);
} }
private void addFileField(String key, String value, ActionEvent event) { private void addFileField(String key, String value, ActionEvent event) {
/* //Re-uses previous field if it is empty else loads a new one.
Re-uses previous field if it is empty,
else loads a new one.
*/
if (fileControllers.size() > 0 && event == null) { if (fileControllers.size() > 0 && event == null) {
FileKeyValueFieldController previousController = fileControllers.get(fileControllers.size() - 1); FileKeyValueFieldController previousController = fileControllers.get(fileControllers.size() - 1);
@ -106,15 +107,15 @@ public class FormDataTabController implements Initializable {
} }
} }
private void addStringField() {
addStringField("", "", null);
}
@FXML @FXML
private void addStringField(ActionEvent event) { private void addStringField(ActionEvent event) {
addStringField("", "", event); addStringField("", "", event);
} }
private void addStringField() {
addStringField("", "", null);
}
public void addStringField(String key, String value) { public void addStringField(String key, String value) {
addStringField(key, value, null); addStringField(key, value, null);
} }
@ -156,20 +157,26 @@ public class FormDataTabController implements Initializable {
} }
public HashMap<String, String> getStringTuples() { public HashMap<String, String> getStringTuples() {
HashMap<String, String> tuples = new HashMap<>(); if (stringMap == null)
stringMap = new HashMap<>();
stringMap.clear();
for (StringKeyValueFieldController controller : stringControllers) { for (StringKeyValueFieldController controller : stringControllers) {
if (controller.isChecked()) if (controller.isChecked())
tuples.put(controller.getHeader().getKey(), controller.getHeader().getValue()); stringMap.put(controller.getHeader().getKey(), controller.getHeader().getValue());
} }
return tuples; return stringMap;
} }
public HashMap<String, String> getFileTuples() { public HashMap<String, String> getFileTuples() {
HashMap<String, String> tuples = new HashMap<>(); if (fileMap == null)
fileMap = new HashMap<>();
fileMap.clear();
for (FileKeyValueFieldController controller : fileControllers) { for (FileKeyValueFieldController controller : fileControllers) {
if (controller.isChecked()) if (controller.isChecked())
tuples.put(controller.getHeader().getKey(), controller.getHeader().getValue()); fileMap.put(controller.getHeader().getKey(), controller.getHeader().getValue());
} }
return tuples; return fileMap;
} }
} }

View file

@ -43,6 +43,8 @@ public class HeaderTabController implements Initializable {
private List<StringKeyValueFieldController> controllers; private List<StringKeyValueFieldController> controllers;
private IntegerProperty controllersCount; private IntegerProperty controllersCount;
private HashMap<String, String> headers;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
controllers = new ArrayList<>(); controllers = new ArrayList<>();
@ -100,9 +102,14 @@ public class HeaderTabController implements Initializable {
} }
} }
/**
* Returns a map of the selected headers.
*/
public HashMap<String, String> getSelectedHeaders() {
if (headers == null)
headers = new HashMap<>();
public HashMap<String, String> getHeaders() { headers.clear();
HashMap<String, String> headers = new HashMap<>();
for (StringKeyValueFieldController controller : controllers) { for (StringKeyValueFieldController controller : controllers) {
if (controller.isChecked()) if (controller.isChecked())
headers.put(controller.getHeader().getKey(), controller.getHeader().getValue()); headers.put(controller.getHeader().getKey(), controller.getHeader().getValue());

View file

@ -42,6 +42,7 @@ public class URLTabController implements Initializable {
private List<StringKeyValueFieldController> controllers; private List<StringKeyValueFieldController> controllers;
private IntegerProperty controllersCount; private IntegerProperty controllersCount;
private HashMap<String, String> tuples;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
@ -101,11 +102,14 @@ public class URLTabController implements Initializable {
} }
public HashMap<String, String> getStringTuples() { public HashMap<String, String> getStringTuples() {
HashMap<String, String> headers = new HashMap<>(); if (tuples == null)
tuples = new HashMap<>();
tuples.clear();
for (StringKeyValueFieldController controller : controllers) { for (StringKeyValueFieldController controller : controllers) {
if (controller.isChecked()) if (controller.isChecked())
headers.put(controller.getHeader().getKey(), controller.getHeader().getValue()); tuples.put(controller.getHeader().getKey(), controller.getHeader().getValue());
} }
return headers; return tuples;
} }
} }

View file

@ -58,12 +58,13 @@ class Visualizer extends ScrollPane {
if (root.isArray()) { if (root.isArray()) {
Iterator<JsonNode> iterator = root.elements(); Iterator<JsonNode> iterator = root.elements();
int i = 0;
while (iterator.hasNext()) { while (iterator.hasNext()) {
currentNode = iterator.next(); currentNode = iterator.next();
if (currentNode.isValueNode()) { if (currentNode.isValueNode()) {
valueLabel = new Label(currentNode.toString()); valueLabel = new Label(i++ + ": " + currentNode.toString());
valueLabel.getStyleClass().addAll("visualizerValueLabel", "visualizerLabel"); valueLabel.getStyleClass().addAll("visualizerValueLabel", "visualizerLabel");
valueLabel.setWrapText(true); valueLabel.setWrapText(true);
valueTooltip = new Tooltip(currentNode.toString()); valueTooltip = new Tooltip(currentNode.toString());
@ -74,7 +75,7 @@ class Visualizer extends ScrollPane {
} else if (currentNode.isObject()) { } else if (currentNode.isObject()) {
TreeItem<HBox> newRoot = new TreeItem<>(); TreeItem<HBox> newRoot = new TreeItem<>();
items.add(newRoot); items.add(newRoot);
populate(newRoot, "[Anonymous Object]", currentNode); populate(newRoot, i++ + ": [Anonymous Object]", currentNode);
} }
} }
} else { } else {

View file

@ -61,8 +61,8 @@ public class HistoryManager {
/** /**
* Creates and initializes the database with necessary tables if not already done. * Creates and initializes the database with necessary tables if not already done.
* *
* @throws IOException * @throws IOException - If unable to establish a connection to the database.
* @throws SQLException * @throws SQLException - If invalid statement is executed on the database.
*/ */
private void initDatabase() throws IOException, SQLException { private void initDatabase() throws IOException, SQLException {
// Read all queries from Queries.json // Read all queries from Queries.json

View file

@ -31,5 +31,5 @@ public class Settings {
public static int connectionReadTimeOut = 30000; public static int connectionReadTimeOut = 30000;
public static String theme = "Adreana"; public static String theme = "Adreana";
public static int showHistoryRange = 3; public static int showHistoryRange = 7;
} }

View file

@ -17,7 +17,6 @@
package com.rohitawate.everest.util.settings; package com.rohitawate.everest.util.settings;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rohitawate.everest.util.EverestUtilities; import com.rohitawate.everest.util.EverestUtilities;
import com.rohitawate.everest.util.Services; import com.rohitawate.everest.util.Services;
@ -44,8 +43,7 @@ public class SettingsLoader implements Runnable {
System.out.println("Settings file found. Loading settings... "); System.out.println("Settings file found. Loading settings... ");
ObjectMapper mapper = new ObjectMapper(); nodes = EverestUtilities.mapper.readTree(settingsFile);
nodes = mapper.readTree(settingsFile);
Settings.responseAreaFont = setStringSetting(Settings.responseAreaFont, "responseAreaFont"); Settings.responseAreaFont = setStringSetting(Settings.responseAreaFont, "responseAreaFont");
Settings.responseAreaFontSize = setIntegerSetting(Settings.responseAreaFontSize, "responseAreaFontSize"); Settings.responseAreaFontSize = setIntegerSetting(Settings.responseAreaFontSize, "responseAreaFontSize");

View file

@ -27,6 +27,7 @@ import java.util.List;
public class ThemeManager { public class ThemeManager {
private static List<Parent> parentNodes = new ArrayList<>(); private static List<Parent> parentNodes = new ArrayList<>();
private static File themeFile = new File("Everest/themes/" + Settings.theme + ".css");
/** /**
* Refreshes the theme of all the registered parents by replacing * Refreshes the theme of all the registered parents by replacing
@ -35,7 +36,6 @@ public class ThemeManager {
*/ */
public static void refreshTheme() { public static void refreshTheme() {
if (!Settings.theme.equals("Adreana")) { if (!Settings.theme.equals("Adreana")) {
File themeFile = new File("Everest/themes/" + Settings.theme + ".css");
if (themeFile.exists()) { if (themeFile.exists()) {
String themePath = themeFile.toURI().toString(); String themePath = themeFile.toURI().toString();
@ -53,7 +53,6 @@ public class ThemeManager {
public static void setTheme(Parent parent) { public static void setTheme(Parent parent) {
if (!Settings.theme.equals("Adreana")) { if (!Settings.theme.equals("Adreana")) {
File themeFile = new File("Everest/themes/" + Settings.theme + ".css");
if (themeFile.exists()) { if (themeFile.exists()) {
parent.getStylesheets().add(themeFile.toURI().toString()); parent.getStylesheets().add(themeFile.toURI().toString());
parentNodes.add(parent); parentNodes.add(parent);