From ac143be317d8305eaf62e599a7efdc4de090b8d4 Mon Sep 17 00:00:00 2001 From: Lavish Date: Sun, 26 Sep 2021 18:39:36 +0530 Subject: [PATCH] Code Cleanup --- .../cloudbus/cloudsim/CloudletScheduler.java | 10 +- .../cloudsim/CloudletSchedulerTimeShared.java | 3 +- .../java/org/cloudbus/cloudsim/Consts.java | 3 +- .../org/cloudbus/cloudsim/Datacenter.java | 26 ++--- .../main/java/org/cloudbus/cloudsim/Host.java | 3 +- .../core/CloudInformationService.java | 20 ++-- .../org/cloudbus/cloudsim/core/CloudSim.java | 110 +++++++++--------- 7 files changed, 82 insertions(+), 93 deletions(-) diff --git a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletScheduler.java b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletScheduler.java index 03254210b..3ae02e6dc 100644 --- a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletScheduler.java +++ b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletScheduler.java @@ -56,11 +56,11 @@ public abstract class CloudletScheduler { */ public CloudletScheduler() { setPreviousTime(0.0); - cloudletWaitingList = new LinkedList(); - cloudletExecList = new LinkedList(); - cloudletPausedList = new LinkedList(); - cloudletFinishedList = new LinkedList(); - cloudletFailedList = new LinkedList(); + cloudletWaitingList = new LinkedList<>(); + cloudletExecList = new LinkedList<>(); + cloudletPausedList = new LinkedList<>(); + cloudletFinishedList = new LinkedList<>(); + cloudletFailedList = new LinkedList<>(); } /** diff --git a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletSchedulerTimeShared.java b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletSchedulerTimeShared.java index 2e967de83..fcb7e2c02 100644 --- a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletSchedulerTimeShared.java +++ b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/CloudletSchedulerTimeShared.java @@ -56,13 +56,12 @@ public double updateVmProcessing(double currentTime, List mipsShare) { // check finished cloudlets double nextEvent = Double.MAX_VALUE; - List toRemove = new ArrayList(); + List toRemove = new ArrayList<>(); for (ResCloudlet rcl : getCloudletExecList()) { long remainingLength = rcl.getRemainingCloudletLength(); if (remainingLength == 0) {// finished: remove from the list toRemove.add(rcl); cloudletFinish(rcl); - continue; } } getCloudletExecList().removeAll(toRemove); diff --git a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Consts.java b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Consts.java index d52896dc9..f695a1a74 100644 --- a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Consts.java +++ b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Consts.java @@ -9,8 +9,9 @@ */ public final class Consts { - /** Suppreses intantiation. */ + /** Suppresses instantiation. */ private Consts() { + throw new RuntimeException("This class should not be instantiated"); } /** One million. */ diff --git a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Datacenter.java b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Datacenter.java index 0cea06f2d..e3a7cb810 100644 --- a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Datacenter.java +++ b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Datacenter.java @@ -133,24 +133,24 @@ public void processEvent(SimEvent ev) { switch (ev.getTag()) { // Resource characteristics inquiry case CloudSimTags.RESOURCE_CHARACTERISTICS: - srcId = ((Integer) ev.getData()).intValue(); + srcId = (Integer) ev.getData(); sendNow(srcId, ev.getTag(), getCharacteristics()); break; // Resource dynamic info inquiry case CloudSimTags.RESOURCE_DYNAMICS: - srcId = ((Integer) ev.getData()).intValue(); + srcId = (Integer) ev.getData(); sendNow(srcId, ev.getTag(), 0); break; case CloudSimTags.RESOURCE_NUM_PE: - srcId = ((Integer) ev.getData()).intValue(); + srcId = (Integer) ev.getData(); int numPE = getCharacteristics().getNumberOfPes(); sendNow(srcId, ev.getTag(), numPE); break; case CloudSimTags.RESOURCE_NUM_FREE_PE: - srcId = ((Integer) ev.getData()).intValue(); + srcId = (Integer) ev.getData(); int freePesNumber = getCharacteristics().getNumberOfFreePes(); sendNow(srcId, ev.getTag(), freePesNumber); break; @@ -282,7 +282,7 @@ protected void processDataDelete(SimEvent ev, boolean ack) { } String filename = (String) data[0]; - int req_source = ((Integer) data[1]).intValue(); + int req_source = (Integer) data[1]; int tag = -1; // check if this file can be deleted (do not delete is right now) @@ -297,7 +297,7 @@ protected void processDataDelete(SimEvent ev, boolean ack) { // send back to sender Object pack[] = new Object[2]; pack[0] = filename; - pack[1] = Integer.valueOf(msg); + pack[1] = msg; sendNow(req_source, tag, pack); } @@ -322,7 +322,7 @@ protected void processDataAdd(SimEvent ev, boolean ack) { File file = (File) pack[0]; // get the file file.setMasterCopy(true); // set the file into a master copy - int sentFrom = ((Integer) pack[1]).intValue(); // get sender ID + int sentFrom = (Integer) pack[1]; // get sender ID /****** * // DEBUG Log.printLine(super.get_name() + ".addMasterFile(): " + file.getName() + @@ -335,8 +335,8 @@ protected void processDataAdd(SimEvent ev, boolean ack) { int msg = addFile(file); // add the file if (ack) { - data[1] = Integer.valueOf(-1); // no sender id - data[2] = Integer.valueOf(msg); // the result of adding a master file + data[1] = -1; // no sender id + data[2] = msg; // the result of adding a master file sendNow(sentFrom, DataCloudTags.FILE_ADD_MASTER_RESULT, data); } } @@ -902,8 +902,7 @@ protected void updateCloudletProcessing() { List list = getVmAllocationPolicy().getHostList(); double smallerTime = Double.MAX_VALUE; // for each host... - for (int i = 0; i < list.size(); i++) { - Host host = list.get(i); + for (Host host : list) { // inform VMs to update processing double time = host.updateVmsProcessing(CloudSim.clock()); // what time do we expect that the next cloudlet will finish? @@ -911,7 +910,7 @@ protected void updateCloudletProcessing() { smallerTime = time; } } - // gurantees a minimal interval before scheduling the event + // guarantees a minimal interval before scheduling the event if (smallerTime < CloudSim.clock() + CloudSim.getMinTimeBetweenEvents() + 0.01) { smallerTime = CloudSim.clock() + CloudSim.getMinTimeBetweenEvents() + 0.01; } @@ -931,8 +930,7 @@ protected void updateCloudletProcessing() { */ protected void checkCloudletCompletion() { List list = getVmAllocationPolicy().getHostList(); - for (int i = 0; i < list.size(); i++) { - Host host = list.get(i); + for (Host host : list) { for (Vm vm : host.getVmList()) { while (vm.getCloudletScheduler().isFinishedCloudlets()) { Cloudlet cl = vm.getCloudletScheduler().getNextFinishedCloudlet(); diff --git a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Host.java b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Host.java index 54cb90d6b..ffba9dbb4 100644 --- a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Host.java +++ b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/Host.java @@ -104,8 +104,7 @@ public double updateVmsProcessing(double currentTime) { double smallerTime = Double.MAX_VALUE; for (Vm vm : getVmList()) { - double time = vm.updateVmProcessing( - currentTime, getVmScheduler().getAllocatedMipsForVm(vm)); + double time = vm.updateVmProcessing(currentTime, getVmScheduler().getAllocatedMipsForVm(vm)); if (time > 0.0 && time < smallerTime) { smallerTime = time; } diff --git a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudInformationService.java b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudInformationService.java index 02be0f721..3c88b1ab8 100644 --- a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudInformationService.java +++ b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudInformationService.java @@ -64,9 +64,9 @@ public class CloudInformationService extends SimEntity { */ public CloudInformationService(String name) throws Exception { super(name); - resList = new LinkedList(); - arList = new LinkedList(); - gisList = new LinkedList(); + resList = new LinkedList<>(); + arList = new LinkedList<>(); + gisList = new LinkedList<>(); } /** @@ -89,7 +89,7 @@ public void processEvent(SimEvent ev) { case CloudSimTags.REQUEST_REGIONAL_GIS: // Get ID of an entity that send this event - id = ((Integer) ev.getData()).intValue(); + id = (Integer) ev.getData(); // Send the regional GIS list back to sender super.send(id, 0L, ev.getTag(), gisList); @@ -110,7 +110,7 @@ public void processEvent(SimEvent ev) { case CloudSimTags.RESOURCE_LIST: // Get ID of an entity that send this event - id = ((Integer) ev.getData()).intValue(); + id = (Integer) ev.getData(); // Send the resource list back to the sender super.send(id, 0L, ev.getTag(), resList); @@ -120,7 +120,7 @@ public void processEvent(SimEvent ev) { case CloudSimTags.RESOURCE_AR_LIST: // Get ID of an entity that send this event - id = ((Integer) ev.getData()).intValue(); + id = (Integer) ev.getData(); // Send the resource AR list back to the sender super.send(id, 0L, ev.getTag(), arList); @@ -279,13 +279,9 @@ private boolean checkResource(Collection list, int id) { return flag; } - Integer obj = null; - Iterator it = list.iterator(); - // a loop to find the match the resource id in a list - while (it.hasNext()) { - obj = it.next(); - if (obj.intValue() == id) { + for (Integer integer : list) { + if (integer == id) { flag = true; break; } diff --git a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudSim.java b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudSim.java index 5e69cccb7..c9c383c58 100644 --- a/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudSim.java +++ b/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/core/CloudSim.java @@ -29,7 +29,7 @@ * network. Later, nodes in such file are mapped to CloudSim entities. Delay calculated from the * BRITE model are added to the messages send through CloudSim. Messages using the old model are * converted to the apropriate methods with the correct parameters. - * + * * @author Rodrigo N. Calheiros * @author Anton Beloglazov * @since CloudSim Toolkit 1.0 @@ -64,10 +64,10 @@ public class CloudSim { /** The minimal time between events. Events within shorter periods after the last event are discarded. */ private static double minTimeBetweenEvents = 0.01; - + /** * Initialises all the common attributes. - * + * * @param _calendar the _calendar * @param _traceFlag the _trace flag * @param numUser number of users @@ -104,7 +104,7 @@ private static void initCommonVariable(Calendar _calendar, boolean _traceFlag, i *
  • CloudSimShutdown * *

    - * + * * @param numUser the number of User Entities created. This parameters indicates that * {@link gridsim.CloudSimShutdown} first waits for all user entities's * END_OF_SIMULATION signal before issuing terminate signal to other entities @@ -143,7 +143,7 @@ public static void init(int numUser, Calendar cal, boolean traceFlag) { *

  • CloudSimShutdown * *

    - * + * * @param numUser the number of User Entities created. This parameters indicates that * {@link gridsim.CloudSimShutdown} first waits for all user entities's * END_OF_SIMULATION signal before issuing terminate signal to other entities @@ -161,20 +161,20 @@ public static void init(int numUser, Calendar cal, boolean traceFlag, double per if (periodBetweenEvents <= 0) { throw new IllegalArgumentException("The minimal time between events should be positive, but is:" + periodBetweenEvents); } - + init(numUser, cal, traceFlag); minTimeBetweenEvents = periodBetweenEvents; } - - - + + + /** * Starts the execution of CloudSim simulation. It waits for complete execution of all entities, * i.e. until all entities threads reach non-RUNNABLE state or there are no more events in the * future event queue. *

    * Note: This method should be called after all the entities have been setup and added. - * + * * @return the last clock time * @throws NullPointerException This happens when creating this entity before initialising * CloudSim package or this entity name is null or empty. @@ -206,7 +206,7 @@ public static double startSimulation() throws NullPointerException { * Stops Cloud Simulation (based on {@link Simulation#runStop()}). This should be only called if * any of the user defined entities explicitly want to terminate simulation during * execution. - * + * * @throws NullPointerException This happens when creating this entity before initialising * CloudSim package or this entity name is null or empty * @see gridsim.CloudSim#init(int, Calendar, boolean) @@ -225,7 +225,7 @@ public static void stopSimulation() throws NullPointerException { /** * This method is called if one wants to terminate the simulation. - * + * * @return true, if successful; false otherwise. */ public static boolean terminateSimulation() { @@ -236,7 +236,7 @@ public static boolean terminateSimulation() { /** * This method is called if one wants to terminate the simulation at a given time. - * + * * @param time the time at which the simulation has to be terminated * @return true, if successful otherwise. */ @@ -249,9 +249,9 @@ public static boolean terminateSimulation(double time) { return true; } - + /** - * Returns the minimum time between events. Events within shorter periods after the last event are discarded. + * Returns the minimum time between events. Events within shorter periods after the last event are discarded. * @return the minimum time between events. */ public static double getMinTimeBetweenEvents() { @@ -260,7 +260,7 @@ public static double getMinTimeBetweenEvents() { /** * Gets a new copy of initial simulation Calendar. - * + * * @return a new copy of Calendar object or if CloudSim hasn't been initialized * @see gridsim.CloudSim#init(int, Calendar, boolean, String[], String[], String) * @see gridsim.CloudSim#init(int, Calendar, boolean) @@ -279,7 +279,7 @@ public static Calendar getSimulationCalendar() { /** * Gets the entity ID of CloudInformationService. - * + * * @return the Entity ID or if it is not found * @pre $none * @post $result >= -1 @@ -291,7 +291,7 @@ public static int getCloudInfoServiceEntityId() { /** * Sends a request to Cloud Information Service (CIS) entity to get the list of all Cloud * hostList. - * + * * @return A List containing CloudResource ID (as an Integer object) or if a CIS entity hasn't * been created before * @pre $none @@ -316,7 +316,7 @@ public static List getCloudResourceList() { /** The deferred event queue. */ protected static DeferredQueue deferred; - /** + /** * The current simulation clock. */ private static double clock; @@ -367,7 +367,7 @@ protected static void initialize() { /** * Get the current simulation time. - * + * * @return the simulation time */ public static double clock() { @@ -376,7 +376,7 @@ public static double clock() { /** * Get the current number of entities in the simulation. - * + * * @return The number of entities */ public static int getNumEntities() { @@ -385,7 +385,7 @@ public static int getNumEntities() { /** * Get the entity with a given id. - * + * * @param id the entity's unique id number * @return The entity, or if it could not be found */ @@ -395,7 +395,7 @@ public static SimEntity getEntity(int id) { /** * Get the entity with a given name. - * + * * @param name The entity's name * @return The entity */ @@ -405,7 +405,7 @@ public static SimEntity getEntity(String name) { /** * Get the id of an entity with a given name. - * + * * @param name The entity's name * @return The entity's unique id number */ @@ -420,7 +420,7 @@ public static int getEntityId(String name) { /** * Gets name of the entity given its entity ID. - * + * * @param entityID the entity ID * @return the Entity name or if this object does not have one * @pre entityID > 0 @@ -438,7 +438,7 @@ public static String getEntityName(int entityID) { /** * Gets name of the entity given its entity ID. - * + * * @param entityID the entity ID * @return the Entity name or if this object does not have one * @pre entityID > 0 @@ -453,15 +453,13 @@ public static String getEntityName(Integer entityID) { /** * Returns a list of entities created for the simulation. - * + * * @return the entity iterator */ public static List getEntityList() { // create a new list to prevent the user from changing // the list of entities used by Simulation - List list = new LinkedList(); - list.addAll(entities); - return list; + return new LinkedList<>(entities); } // Public update methods @@ -469,7 +467,7 @@ public static List getEntityList() { /** * Add a new entity to the simulation. This is present for compatibility with existing * simulations since entities are automatically added to the simulation upon instantiation. - * + * * @param e The new entity */ public static void addEntity(SimEntity e) { @@ -490,7 +488,7 @@ public static void addEntity(SimEntity e) { /** * Internal method used to add a new entity to the simulation when the simulation is running. It * should not be called from user simulations. - * + * * @param e The new entity */ protected static void addEntityDynamically(SimEntity e) { @@ -505,7 +503,7 @@ protected static void addEntityDynamically(SimEntity e) { /** * Internal method used to run one tick of the simulation. This method should not be * called in simulations. - * + * * @return true, if successful otherwise * @todo If the method shouldn't be called by the user, * it should be protected in any way, such as changing @@ -514,19 +512,17 @@ protected static void addEntityDynamically(SimEntity e) { public static boolean runClockTick() { SimEntity ent; boolean queue_empty; - - int entities_size = entities.size(); - for (int i = 0; i < entities_size; i++) { - ent = entities.get(i); + for (SimEntity entity : entities) { + ent = entity; if (ent.getState() == SimEntity.RUNNABLE) { ent.run(); } } - + // If there are more future events then deal with them if (future.size() > 0) { - List toRemove = new ArrayList(); + List toRemove = new ArrayList<>(); Iterator fit = future.iterator(); queue_empty = false; SimEvent first = fit.next(); @@ -568,7 +564,7 @@ public static void runStop() { /** * Used to hold an entity for some time. - * + * * @param src the src * @param delay the delay */ @@ -580,7 +576,7 @@ public static void hold(int src, long delay) { /** * Used to pause an entity for some time. - * + * * @param src the src * @param delay the delay */ @@ -592,7 +588,7 @@ public static void pause(int src, double delay) { /** * Used to send an event from one entity to another. - * + * * @param src the src * @param dest the dest * @param delay the delay @@ -613,7 +609,7 @@ public static void send(int src, int dest, double delay, int tag, Object data) { /** * Used to send an event from one entity to another, with priority in the queue. - * + * * @param src the src * @param dest the dest * @param delay the delay @@ -633,7 +629,7 @@ public static void sendFirst(int src, int dest, double delay, int tag, Object da * Sets an entity's state to be waiting. The predicate used to wait for an event is now passed * to Sim_system. Only events that satisfy the predicate will be passed to the entity. This is * done to avoid unnecessary context switches. - * + * * @param src the src * @param p the p */ @@ -647,7 +643,7 @@ public static void wait(int src, Predicate p) { /** * Checks if events for a specific entity are present in the deferred event queue. - * + * * @param d the d * @param p the p * @return the int @@ -667,7 +663,7 @@ public static int waiting(int d, Predicate p) { /** * Selects an event matching a predicate. - * + * * @param src the src * @param p the p * @return the sim event @@ -687,7 +683,7 @@ public static SimEvent select(int src, Predicate p) { /** * Find first deferred event matching a predicate. - * + * * @param src the src * @param p the p * @return the sim event @@ -706,7 +702,7 @@ public static SimEvent findFirstDeferred(int src, Predicate p) { /** * Removes an event from the event queue. - * + * * @param src the src * @param p the p * @return the sim event @@ -728,7 +724,7 @@ public static SimEvent cancel(int src, Predicate p) { /** * Removes all events that match a given predicate from the future event queue returns true if * at least one event has been cancelled; false otherwise. - * + * * @param src the src * @param p the p * @return true, if successful @@ -752,7 +748,7 @@ public static boolean cancelAll(int src, Predicate p) { /** * Processes an event. - * + * * @param e the e */ private static void processEvent(SimEvent e) { @@ -829,7 +825,7 @@ public static void runStart() { /** * Check if the simulation is still running. This method should be used by entities to check if * they should continue executing. - * + * * @return if the simulation is still running, otherwise */ public static boolean running() { @@ -838,7 +834,7 @@ public static boolean running() { /** * This method is called if one wants to pause the simulation. - * + * * @return true, if successful otherwise. */ public static boolean pauseSimulation() { @@ -848,7 +844,7 @@ public static boolean pauseSimulation() { /** * This method is called if one wants to pause the simulation at a given time. - * + * * @param time the time at which the simulation has to be paused * @return true, if successful otherwise. */ @@ -863,7 +859,7 @@ public static boolean pauseSimulation(long time) { /** * This method is called if one wants to resume the simulation that has previously been paused. - * + * * @return if the simulation has been restarted or or otherwise. */ public static boolean resumeSimulation() { @@ -879,7 +875,7 @@ public static boolean resumeSimulation() { /** * Start the simulation running. This should be called after all the entities have been setup * and added, and their ports linked. - * + * * @return the last clock value */ public static double run() { @@ -964,7 +960,7 @@ public static void abruptallyTerminate() { /** * Prints a message about the progress of the simulation. - * + * * @param message the message */ private static void printMessage(String message) { @@ -973,7 +969,7 @@ private static void printMessage(String message) { /** * Checks if is paused. - * + * * @return true, if is paused */ public static boolean isPaused() {