Skip to content

Commit

Permalink
Do multiple refactors for better performance/code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
khalidzahra committed Sep 29, 2021
1 parent 12e5408 commit eb15a3c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 107 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/github/khalidzahra/TopologyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,23 @@ public boolean writeJSON(String topologyID, String filePath) {
/**
* Queries all topologies currently stored in memory
*
* @return Returns a List of Topology containing all loaded topologies. Returns an empty list if no topologies are found.
* @return Returns a List of Topology containing all loaded topologies.
* Returns an empty list if no topologies are found.
*/
public List<Topology> queryTopologies() {
return topologyRegistry.getTopologyList();
}

/**
* Finds topology with specified topology ID
*
* @param topologyID String variable containing the topology ID
* @return Returns Topology object with specified topology ID
*/
public Topology findTopology(String topologyID) {
return topologyRegistry.getTopologyList()
.stream()
.filter(topology -> topology.getId().equals(topologyID))
.filter(topology -> topology.getTopologyId().equals(topologyID))
.findAny()
.orElse(null);
}
Expand All @@ -85,7 +87,8 @@ public boolean deleteTopology(String topologyID) {
* Queries the devices connected within a topology
*
* @param topologyID String variable containing the topology ID
* @return Returns a List of Device containing all connected devices. Returns an empty list if no devices are found.
* @return Returns a List of Device containing all connected devices.
* Returns an empty list if no devices are found.
*/
public List<Device> queryDevices(String topologyID) {
Topology topology = topologyRegistry.findTopology(topologyID);
Expand All @@ -97,7 +100,8 @@ public List<Device> queryDevices(String topologyID) {
*
* @param topologyID String variable containing the topology ID
* @param netlistNodeID String variable containing the node ID
* @return Returns a List of Device containing all connected devices. Returns an empty list if no devices are found.
* @return Returns a List of Device containing all connected devices.
* Returns an empty list if no devices are found.
*/
public List<Device> queryDevicesWithNetlistNode(String topologyID, String netlistNodeID) {
Topology topology = topologyRegistry.findTopology(topologyID);
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/github/khalidzahra/internal/device/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
import com.github.khalidzahra.internal.util.JsonKeyUtil;
import com.google.gson.annotations.SerializedName;

import java.util.HashMap;
import java.util.Map;

/**
* Created by Khalid on 9/28/21.
*/
public class Device {

@SerializedName(value = JsonKeyUtil.DEVICE_TYPE_KEY)
private DeviceType deviceType;
private String id;
@SerializedName(JsonKeyUtil.DEVICE_TYPE_KEY)
private final DeviceType deviceType;
@SerializedName("id")
private final String deviceId;
@SerializedName(value = JsonKeyUtil.DEVICE_PROPERTIES_KEY,
alternate = {JsonKeyUtil.DEVICE_PROPERTIES_RESISTANCE_KEY,
JsonKeyUtil.DEVICE_PROPERTIES_CAPACITANCE_KEY,
JsonKeyUtil.DEVICE_PROPERTIES_INDUCTANCE_KEY,
JsonKeyUtil.DEVICE_PROPERTIES_TRANSISTOR_KEY})
private DeviceProperties deviceProperties;
@SerializedName(value = JsonKeyUtil.DEVICE_NETLIST_KEY)
private HashMap<String, String> netList;
private final DeviceProperties deviceProperties;
@SerializedName(JsonKeyUtil.DEVICE_NETLIST_KEY)
private final Map<String, String> netList;

public Device(DeviceType deviceType, String id, DeviceProperties deviceProperties, HashMap<String, String> netList) {
public Device(DeviceType deviceType, String deviceId, DeviceProperties deviceProperties, Map<String, String> netList) {
this.deviceType = deviceType;
this.id = id;
this.deviceId = deviceId;
this.deviceProperties = deviceProperties;
this.netList = netList;
}
Expand All @@ -33,15 +34,15 @@ public DeviceType getDeviceType() {
return deviceType;
}

public String getId() {
return id;
public String getDeviceId() {
return deviceId;
}

public DeviceProperties getDeviceProperties() {
return deviceProperties;
}

public HashMap<String, String> getNetList() {
public Map<String, String> getNetList() {
return netList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
*/
public class DeviceProperties {

private double min, max;
private double min;
private double max;

@SerializedName("default")
private double defaultValue;

public DeviceProperties(double min, double max, double defaultValue) {
this.min = min;
this.max = max;
this.defaultValue = defaultValue;
}

public double getMin() {
return min;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
*/
public class Topology {

private String id;
@SerializedName("id")
private final String topologyId;
@SerializedName("components")
private List<Device> devices;
private final List<Device> devices;

public Topology(String id) {
this.id = id;
public Topology(String topologyId) {
this.topologyId = topologyId;
this.devices = new ArrayList<>();
}

public Topology(String id, List<Device> deviceList) {
this.id = id;
public Topology(String topologyId, List<Device> deviceList) {
this.topologyId = topologyId;
this.devices = deviceList;
}

Expand All @@ -42,8 +43,8 @@ public void addDevice(Device device) {
devices.add(device);
}

public String getId() {
return id;
public String getTopologyId() {
return topologyId;
}

public List<Device> getDevices() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.khalidzahra.internal.topology;

import com.github.khalidzahra.internal.util.json.GsonHandler;
import com.github.khalidzahra.internal.util.json.GsonUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,53 +18,64 @@ public TopologyRegistry() {

/**
* Loads the topology from the specified file and adds it to topologyList.
*
* @param filePath String variable containing path to the JSON file
* @return Returns true if the topology is successfully loaded and registered
*/
public boolean loadAndRegisterTopology(String filePath) {
Topology topology = GsonHandler.loadTopology(filePath);
Topology topology = GsonUtil.loadTopology(filePath);
return registerTopology(topology);
}

/**
* Writes the specified topology to the specified file
*
* @param topologyID String variable containing the topology ID
* @param filePath String variable containing path to the JSON file
* @param filePath String variable containing path to the JSON file
* @return Returns true if the topology is successfully found and saved
*/
public boolean saveTopology(String topologyID, String filePath) {
Topology topology = findTopology(topologyID);
if (topology == null) return false;
return GsonHandler.saveTopology(filePath, topology);
if (topology == null) {
return false;
}
return GsonUtil.saveTopology(filePath, topology);
}

/**
* Adds the topology to topologyList
*
* @param topology Topology object to be registered
* @return Returns true if the topology is successfully registered
*/
public boolean registerTopology(Topology topology) {
if (topology == null) return false;
if (topology == null) {
return false;
}
return topologyList.add(topology);
}

/**
* Finds the topology matching the specified ID
*
* @param topologyID String variable containing the topology ID
* @return Returns Topology object matching the specified ID. Returns null if no object matching the ID is found.
*/
public Topology findTopology(String topologyID) {
return topologyList.stream().filter(topology -> topology.getId().equals(topologyID)).findAny().orElse(null);
return topologyList.stream().filter(topology -> topology.getTopologyId().equals(topologyID)).findAny().orElse(null);
}

/**
* Unloads the topology from memory
*
* @param topologyID String variable containing the topology ID
* @return Returns true if topology is successfully found and unloaded.
*/
public boolean deregisterTopology(String topologyID) {
Topology topology = findTopology(topologyID);
if (topology == null) return false;
if (topology == null) {
return false;
}
return topologyList.remove(topology);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DeviceSerializer implements JsonSerializer<Device> {
public JsonElement serialize(Device device, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(JsonKeyUtil.DEVICE_TYPE_KEY, device.getDeviceType().toString().toLowerCase(Locale.ROOT));
jsonObject.addProperty("id", device.getId());
jsonObject.addProperty("id", device.getDeviceId());
jsonObject.add(device.getDeviceType().getSerializedPropertyName(), jsonSerializationContext.serialize(device.getDeviceProperties()));
jsonObject.add(JsonKeyUtil.DEVICE_NETLIST_KEY, jsonSerializationContext.serialize(device.getNetList()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import com.google.gson.GsonBuilder;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* Created by Khalid on 9/28/21.
*/
public class GsonHandler {
public class GsonUtil {

/**
* GsonBuilder object that will be used throughout project
*/
private static final Gson gson = new GsonBuilder()
private static final Gson GSON_OBJECT = new GsonBuilder()
.registerTypeAdapter(Device.class, new DeviceSerializer())
.setPrettyPrinting().create();

Expand All @@ -27,10 +29,9 @@ public class GsonHandler {
*/
public static Topology loadTopology(String filePath) {
Topology topology;
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath))) {
topology = getGson().fromJson(reader, Topology.class);
} catch (FileNotFoundException e) {
} catch (IOException e) {
return null;
}
return topology;
Expand All @@ -44,7 +45,7 @@ public static Topology loadTopology(String filePath) {
* @return Returns true if save was successful
*/
public static boolean saveTopology(String filePath, Topology topology) {
try (FileWriter writer = new FileWriter(filePath)) {
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(filePath))) {
getGson().toJson(topology, writer);
} catch (IOException e) {
return false;
Expand All @@ -53,6 +54,6 @@ public static boolean saveTopology(String filePath, Topology topology) {
}

public static Gson getGson() {
return gson;
return GSON_OBJECT;
}
}
Loading

0 comments on commit eb15a3c

Please sign in to comment.