Skip to content

Commit

Permalink
Replace String.format(str, args) to str.formatted(args)
Browse files Browse the repository at this point in the history
Signed-off-by: Manoel Campos <manoelcampos@gmail.com>
  • Loading branch information
manoelcampos committed Nov 4, 2022
1 parent 9fb86a0 commit f4551b8
Show file tree
Hide file tree
Showing 51 changed files with 145 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void hostSearchRetry() {
"as soon as possible";

final boolean singleDc = dcList.size() == 1;
final var targetDcName = singleDc || getDatacenter().equals(targetMigrationDc) ? "" : String.format("on %s ", targetMigrationDc);
final var targetDcName = singleDc || getDatacenter().equals(targetMigrationDc) ? "" : "on %s ".formatted(targetMigrationDc);
LOGGER.warn(
"{}: {}: An under or overload situation was detected on {}, however there aren't suitable Hosts {}to manage that. Trying again {}.",
getDatacenter().getSimulation().clock(), getClass().getSimpleName(), getDatacenter(), targetDcName, msg);
Expand Down Expand Up @@ -253,9 +253,9 @@ private void printOverUtilizedHosts(final Set<Host> overloadedHosts) {
}

private String overloadedHostToString(final Host host) {
return String.format(
" Host %d (upper CPU threshold %.2f, current utilization: %.2f)",
host.getId(), getOverUtilizationThreshold(host), host.getCpuPercentUtilization());
return
" Host %d (upper CPU threshold %.2f, current utilization: %.2f)"
.formatted(host.getId(), getOverUtilizationThreshold(host), host.getCpuPercentUtilization());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ private void updateHostProcessing(final Cloudlet cloudlet) {
}

private void logCloudletStatusChange(final Cloudlet cloudlet, final String status) {
final String msg = cloudlet.getJobId() > 0 ? String.format("(job %d) ", cloudlet.getJobId()) : "";
final String msg = cloudlet.getJobId() > 0 ? "(job %d) ".formatted(cloudlet.getJobId()) : "";
LOGGER.info("{}: {}: Request to {} {} {}received.", getSimulation().clockStr(), getName(), status, cloudlet, msg);
}

Expand Down Expand Up @@ -1011,19 +1011,21 @@ private void logPostponingCloudletExecution(final Cloudlet cloudlet) {
final Vm vm = cloudlet.getVm();
final String vmMsg = Vm.NULL.equals(vm) ?
"it couldn't be mapped to any VM" :
String.format("bind Vm %d is not available", vm.getId());
"bind Vm %d is not available".formatted(vm.getId());

final String msg = String.format(
"%s: %s: Postponing execution of Cloudlet %d because {}.",
getSimulation().clockStr(), getName(), cloudlet.getId());
final String msg =
"%s: %s: Postponing execution of Cloudlet %d because {}."
.formatted(getSimulation().clockStr(), getName(), cloudlet.getId());

if (vm.getSubmissionDelay() <= 0) {
LOGGER.warn(msg, vmMsg);
return;
}

final String secs = vm.getSubmissionDelay() > 1 ? "seconds" : "second";
final var reason = String.format("bind Vm %d was requested to be created with %.2f %s delay", vm.getId(), vm.getSubmissionDelay(), secs);
final var reason =
"bind Vm %d was requested to be created with %.2f %s delay"
.formatted(vm.getId(), vm.getSubmissionDelay(), secs);
LOGGER.info(msg, reason);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void setTimeSlice(final double timeSlice) {

@Override
public String toString() {
return String.format("Cloudlet %d", cloudlet.getId());
return "Cloudlet %d".formatted(cloudlet.getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public CloudletSimple(final long id, final long length, final long pesNumber) {

@Override
public String toString() {
return String.format("Cloudlet %d", getId());
return "Cloudlet %d".formatted(getId());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/cloudbus/cloudsim/core/CloudSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private void notifyEndOfSimulationToEntities() {
}

private void printSimulationFinished() {
final String msg1 = String.format("Simulation finished at time %.2f", clock);
final String msg1 = "Simulation finished at time %.2f".formatted(clock);
final String extra = future.isEmpty() ? "" : ", before completing,";
final String msg2 = isTimeToTerminateSimulationUnderRequest()
? extra + " in reason of an explicit request to terminate() or terminateAt()"
Expand Down Expand Up @@ -416,7 +416,7 @@ public double clock() {

@Override
public String clockStr() {
return String.format("%.2f", clock);
return "%.2f".formatted(clock);
}

@Override
Expand Down Expand Up @@ -470,7 +470,7 @@ public void addEntity(final CloudSimEntity entity) {
protected void removeFinishedEntity(final CloudSimEntity entity){
if(entity.isAlive()){
final var msg = "Alive entity %s cannot be removed from the simulation entity list.";
throw new IllegalStateException(String.format(msg, entity));
throw new IllegalStateException(msg.formatted(entity));
}

entities.remove(entity);
Expand Down Expand Up @@ -677,7 +677,7 @@ private Stream<SimEvent> filterEvents(final EventQueue queue, final Predicate<Si
private void processEvent(final SimEvent evt) {
if (evt.getTime() < clock) {
final var msg = "Past event detected. Event time: %.2f Simulation clock: %.2f";
throw new IllegalArgumentException(String.format(msg, evt.getTime(), clock));
throw new IllegalArgumentException(msg.formatted(evt.getTime(), clock));
}

setClock(evt.getTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ protected final void setId(final int id) {
*/
private void setAutomaticName() {
final long id = this.id >= 0 ? this.id : this.simulation.getNumEntities();
this.name = String.format("%s%d", getClass().getSimpleName(), id);
this.name = "%s%d".formatted(getClass().getSimpleName(), id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,9 @@ protected boolean processVmDestroy(final SimEvent evt, final boolean ack) {
}

final String warningMsg = generateNotFinishedCloudletsWarning(vm);
final String msg = String.format(
"%s: %s: %s destroyed on %s. %s",
getSimulation().clockStr(), getClass().getSimpleName(), vm, vm.getHost(), warningMsg);
final String msg =
"%s: %s: %s destroyed on %s. %s"
.formatted(getSimulation().clockStr(), getClass().getSimpleName(), vm, vm.getHost(), warningMsg);
if(warningMsg.isEmpty() || getSimulation().isTerminationTimeSet())
LOGGER.info(msg);
else LOGGER.warn(msg);
Expand All @@ -599,12 +599,15 @@ private String generateNotFinishedCloudletsWarning(final Vm vm) {
return "";
}

return String.format(
"It had a total of %d cloudlets (running + waiting). %s", cloudletsNoFinished,
"Some events may have been missed. You can try: " +
"(a) decreasing CloudSim's minTimeBetweenEvents and/or Datacenter's schedulingInterval attribute; " +
"(b) increasing broker's Vm destruction delay for idle VMs if you set it to zero; " +
"(c) defining Cloudlets with smaller length (your Datacenter's scheduling interval may be smaller than the time to finish some Cloudlets).");
final var options =
"""
Some events may have been missed. You can try:
(a) decreasing CloudSim's minTimeBetweenEvents and/or Datacenter's schedulingInterval attribute;
(b) increasing broker's Vm destruction delay for idle VMs if you set it to zero;
(c) defining Cloudlets with smaller length (your Datacenter's scheduling interval may be smaller than the time to finish some Cloudlets).
""";

return "It had a total of %d cloudlets (running + waiting). %s".formatted(cloudletsNoFinished, options);
}

/**
Expand Down Expand Up @@ -832,12 +835,12 @@ public void requestVmMigration(final Vm sourceVm, Host targetHost) {
final double delay = timeToMigrateVm(sourceVm, targetHost);
final String msg1 =
Host.NULL.equals(sourceHost) ?
String.format("%s to %s", sourceVm, targetHost) :
String.format("%s from %s to %s", sourceVm, sourceHost, targetHost);
"%s to %s".formatted(sourceVm, targetHost) :
"%s from %s to %s".formatted(sourceVm, sourceHost, targetHost);

final String currentTime = getSimulation().clockStr();
final var fmt = "It's expected to finish in %.2f seconds, considering the %.0f%% of bandwidth allowed for migration and the VM RAM size.";
final String msg2 = String.format(fmt, delay, getBandwidthPercentForMigration()*100);
final String msg2 = fmt.formatted(delay, getBandwidthPercentForMigration()*100);
LOGGER.info("{}: {}: Migration of {} is started. {}", currentTime, getName(), msg1, msg2);

if(targetHost.addMigratingInVm(sourceVm)) {
Expand Down Expand Up @@ -1037,7 +1040,7 @@ public <T extends Host> Datacenter removeHost(final T host) {

@Override
public String toString() {
return String.format("Datacenter %d", getId());
return "Datacenter %d".formatted(getId());
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/cloudbus/cloudsim/datacenters/TimeZoned.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public interface TimeZoned {
default double validateTimeZone(final double timeZone) {
if(timeZone < MIN_TIME_ZONE_OFFSET || timeZone > MAX_TIME_ZONE_OFFSET){
final var msg = "Timezone offset must be between [%d and %d].";
throw new IllegalArgumentException(String.format(msg, MIN_TIME_ZONE_OFFSET, MAX_TIME_ZONE_OFFSET));
throw new IllegalArgumentException(msg.formatted(MIN_TIME_ZONE_OFFSET, MAX_TIME_ZONE_OFFSET));
}

return timeZone;
Expand Down Expand Up @@ -120,10 +120,10 @@ default double distance(final TimeZoned other) {

static String format(final double timeZone){
final double decimals = timeZone - (int) timeZone;
final String formatted = decimals == 0 ?
String.format("GMT%+.0f", timeZone) :
String.format("GMT%+d:%2.0f", (int)timeZone, hoursToMinutes(decimals));
return String.format("%-8s", formatted);
final String timezoneStr = decimals == 0 ?
"GMT%+.0f".formatted(timeZone) :
"GMT%+d:%2.0f".formatted((int)timeZone, hoursToMinutes(decimals));
return "%-8s".formatted(timezoneStr);
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/cloudbus/cloudsim/hosts/HostSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public final Host setRamProvisioner(final ResourceProvisioner ramProvisioner) {
private void checkSimulationIsRunningAndAttemptedToChangeHost(final String resourceName) {
if(simulation.isRunning()){
final var msg = "It is not allowed to change a Host's %s after the simulation started.";
throw new IllegalStateException(String.format(msg, resourceName));
throw new IllegalStateException(msg.formatted(resourceName));
}
}

Expand Down Expand Up @@ -1125,8 +1125,8 @@ public final void setDatacenter(final Datacenter datacenter) {
public String toString() {
final String dc =
datacenter == null || Datacenter.NULL.equals(datacenter) ? "" :
String.format("/DC %d", datacenter.getId());
return String.format("Host %d%s", getId(), dc);
"/DC %d".formatted(datacenter.getId());
return "Host %d%s".formatted(getId(), dc);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ public boolean active() {
@Override
public String toString() {
final var msg = "Time: %6.1f | Requested: %10.0f MIPS | Allocated: %10.0f MIPS | Used: %3.0f%% Host Active: %s%n";
return String.format(msg, time, requestedMips, allocatedMips, percentUsage() * 100, active);
return msg.formatted(time, requestedMips, allocatedMips, percentUsage() * 100, active);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void calculateShortestPath() {
@Override
public String toString() {
final var builder = new StringBuilder(100);
builder.append(String.format("DelayMatrix: %n"));
builder.append("DelayMatrix: %n".formatted());

for (int column = 0; column < mTotalNodeNum; ++column) {
builder.append('\t').append(column);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/cloudbus/cloudsim/network/IcmpPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public String toString() {
final int SIZE = 1000; // number of chars
final StringBuilder sb = new StringBuilder(SIZE);
sb.append("Ping information for ").append(name)
.append(String.format("%nEntity Name\tEntry TiOme\tExit Time\t Bandwidth%n"))
.append("%nEntity Name\tEntry Time\tExit Time\t Bandwidth%n".formatted())
.append("----------------------------------------------------------")
.append(System.lineSeparator());

Expand All @@ -171,13 +171,13 @@ public String toString() {
final String bandwidth = getData(baudRateList, i);

sb.append("Entity ").append(resID).append("\t\t")
.append(String.format("%s%s%s%s%s%s%s%n", entry, tab, tab, exit, tab, tab, bandwidth));
.append("%s%s%s%s%s%s%s%n".formatted(entry, tab, tab, exit, tab, tab, bandwidth));
}

sb.append(System.lineSeparator())
.append("Round Trip Time : ")
.append(num.format(getTotalResponseTime()))
.append(String.format(" seconds%nNumber of Hops : "))
.append(" seconds%nNumber of Hops : ".formatted())
.append(getNumberOfHops())
.append(System.lineSeparator())
.append("Bottleneck Bandwidth : ")
Expand Down Expand Up @@ -452,7 +452,7 @@ public void setTag(final CloudSimTag tag) {
}
else {
final var fmt = "Tag must be between %s and %s";
final var msg = String.format(fmt, CloudSimTag.ICMP_PKT_SUBMIT, CloudSimTag.ICMP_PKT_RETURN);
final var msg = fmt.formatted(CloudSimTag.ICMP_PKT_SUBMIT, CloudSimTag.ICMP_PKT_RETURN);
throw new IllegalArgumentException(msg);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public Point2D(){

@Override
public String toString() {
return String.format("x: %d y: %d", x, y);
return "x: %d y: %d".formatted(x, y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,15 @@ public String toString() {
builder.append("topological-node-information: ").append(System.lineSeparator());

for (final TopologicalNode node : nodeList) {
builder.append(
String.format("%d | %s%n",
node.getId(), node.getWorldCoordinates()));
builder.append("%d | %s%n".formatted(node.getId(), node.getWorldCoordinates()));
}

builder.append(String.format("%n%n node-link-information:%n"));
builder.append("%n%n node-link-information:%n".formatted());

for (final TopologicalLink link : linksList) {
builder.append(
String.format("from: %d to: %d delay: %.2f%n",
link.getSrcNodeID(), link.getDestNodeID(), link.getLinkDelay()));
"from: %d to: %d delay: %.2f%n"
.formatted(link.getSrcNodeID(), link.getDestNodeID(), link.getLinkDelay()));
}

return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public PowerModelHostSpec(final List<Double> powerSpec) {
}

final var msg =
String.format("powerSpec has to contain at least %d elements (representing utilization at 0%% and 100%% load, respectively)",
MIN_POWER_CONSUMPTION_DATA_SIZE);
"powerSpec has to contain at least %d elements (representing utilization at 0%% and 100%% load, respectively)"
.formatted(MIN_POWER_CONSUMPTION_DATA_SIZE);
throw new IllegalArgumentException(msg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public PeProvisioner getPeProvisioner() {

@Override
public String toString() {
return String.format("%s %d: %s", getClass().getSimpleName(), id, status);
return "%s %d: %s".formatted(getClass().getSimpleName(), id, status);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ public long deallocateAllResources() {

@Override
public String toString() {
return String.format("%s: used %d of %d", getClass().getSimpleName(), getAllocatedResource(), getCapacity());
return "%s: used %d of %d".formatted(getClass().getSimpleName(), getAllocatedResource(), getCapacity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,7 @@ private void updateVmResourceAbsoluteUtilization(final CloudletExecution cle, fi

final long available = vmResource.getAvailableResource();
if(requested > available){
final String msg1 =
available > 0 ?
String.format("just %d was available", available):
"no amount is available.";
final String msg1 = available > 0 ? "just %d was available".formatted(available): "no amount is available.";
final String msg2 = vmResource.getClass() == Ram.class ? ". Using Virtual Memory," : ",";
LOGGER.warn(
"{}: {}: {} requested {} {} of {} but {}{} which delays Cloudlet processing.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ private void allocatePesListForVm(final Vm vm, final MipsShare mipsShare) {
*/
private void logMipsUnavailable(final Vm vm, final double requestedMipsForVmPe, final double allocatedMipsForVmPe) {
final String msg = allocatedMipsForVmPe > 0 ?
String.format("Only %.0f MIPS were allocated.", allocatedMipsForVmPe)
: "No MIPS were allocated.";
"Only %.0f MIPS were allocated.".formatted(allocatedMipsForVmPe) :
"No MIPS were allocated.";
LOGGER.warn(
"{}: {}: {} is requiring a total of {} MIPS but the PEs of {} currently don't have such an available MIPS amount. {}",
getHost().getSimulation().clockStr(),
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/cloudbus/cloudsim/util/BytesConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ public static double bytesToKiloBytes(final double bytes){
*/
public static String bytesToStr(final double bytes){
if(bytes < KILO) {
return String.format("%.0f bytes", bytes);
return "%.0f bytes".formatted(bytes);
}

if(bytes < MEGA) {
return String.format("%.1f KB", bytesToKiloBytes(bytes));
return "%.1f KB".formatted(bytesToKiloBytes(bytes));
}

if(bytes < GIGA) {
return String.format("%.1f MB", bytesToMegaBytes(bytes));
return "%.1f MB".formatted(bytesToMegaBytes(bytes));
}

return String.format("%.1f GB", bytesToGigaBytes(bytes));
return "%.1f GB".formatted(bytesToGigaBytes(bytes));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public InvalidEventDataTypeException(final SimEvent evt, final String tagName, f
private static String formatMsg(final SimEvent evt, final String tagName, final String requiredClassName) {
final var fmt = "%s: %s event data must be a %s but it was %s";
final var clock = evt.getSimulation().clockStr();
return String.format(fmt, clock, tagName, requiredClassName, evt.getData().getClass().getSimpleName());
return fmt.formatted(clock, tagName, requiredClassName, evt.getData().getClass().getSimpleName());
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cloudbus/cloudsim/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static String secondsToStr(final double seconds) {
*/
private static String convertSeconds(final double seconds, final double conversionFactor, final String unit){
final double convertedTime = seconds/conversionFactor;
return String.format("%.2f %s", convertedTime, convertedTime >= 2 ? unit+"s" : unit);
return "%.2f %s".formatted(convertedTime, convertedTime >= 2 ? unit+"s" : unit);
}

/**
Expand Down

0 comments on commit f4551b8

Please sign in to comment.