diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/PowerState.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/PowerState.java index 070b7ea030bcc..4c96341ea03b0 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/PowerState.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/PowerState.java @@ -5,12 +5,19 @@ */ package com.microsoft.azure.management.compute; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + import com.fasterxml.jackson.annotation.JsonValue; /** * Possible power states of a virtual machine. */ public class PowerState { + // This needs to be at the beginning for the initialization to happen correctly + private static final Map VALUES_BY_NAME = new HashMap<>(); + /** * Static value PowerState/running for PowerState. */ @@ -36,19 +43,53 @@ public class PowerState { */ public static final PowerState STOPPED = new PowerState("PowerState/stopped"); + /** + * Static value PowerState/stopping for PowerState. + */ + public static final PowerState STOPPING = new PowerState("PowerState/stopping"); + /** * Static value PowerState/unknown for PowerState. */ public static final PowerState UNKNOWN = new PowerState("PowerState/unknown"); - private String value; + private final String value; + + /** + * @return predefined virtual machine power states + */ + public static PowerState[] values() { + Collection valuesCollection = VALUES_BY_NAME.values(); + return valuesCollection.toArray(new PowerState[valuesCollection.size()]); + } /** * Creates a custom value for PowerState. * @param value the custom value */ public PowerState(String value) { + // TODO: This constructor should be private, but keeping as is for now to keep 1.0.0 back compat this.value = value; + VALUES_BY_NAME.put(value.toLowerCase(), this); + } + + /** + * Parses a value into a power state and creates a new PowerState instance if not found among the existing ones. + * + * @param value a power state name + * @return the parsed or created power state + */ + public static PowerState fromString(String value) { + if (value == null) { + return null; + } + + PowerState powerState = VALUES_BY_NAME.get(value.toLowerCase().replace(" ", "")); + if (powerState != null) { + return powerState; + } else { + return new PowerState(value); + } } /** @@ -61,8 +102,8 @@ public PowerState(String value) { public static PowerState fromInstanceView(VirtualMachineInstanceView virtualMachineInstanceView) { if (virtualMachineInstanceView != null && virtualMachineInstanceView.statuses() != null) { for (InstanceViewStatus status : virtualMachineInstanceView.statuses()) { - if (status.code() != null && status.code().startsWith("PowerState")) { - return new PowerState(status.code()); + if (status.code() != null && status.code().toLowerCase().startsWith("powerstate")) { + return PowerState.fromString(status.code()); } } } @@ -84,15 +125,15 @@ public int hashCode() { public boolean equals(Object obj) { if (!(obj instanceof PowerState)) { return false; - } - if (obj == this) { + } else if (obj == this) { return true; - } - PowerState rhs = (PowerState) obj; - if (value == null) { - return rhs.value == null; } else { - return value.equals(rhs.value); + PowerState rhs = (PowerState) obj; + if (value == null) { + return rhs.value == null; + } else { + return value.equals(rhs.value); + } } } } \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/Region.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/Region.java index c62b6f76c5574..9025e602e6fbb 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/Region.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/Region.java @@ -75,8 +75,7 @@ public final class Region { private final String label; /** - * Get an array of pre-defined regions. - * @return an array of pre-defined regions. + * @return predefined Azure regions */ public static Region[] values() { Collection valuesCollection = VALUES_BY_NAME.values(); @@ -161,7 +160,7 @@ public static Region fromName(String name) { @Override public int hashCode() { - return name.hashCode(); + return this.name.hashCode(); } @Override @@ -172,7 +171,7 @@ public boolean equals(Object obj) { return true; } else { Region rhs = (Region) obj; - return name.equalsIgnoreCase(rhs.name); + return this.name.equalsIgnoreCase(rhs.name); } } }