Skip to content

Commit

Permalink
Merge pull request #1629 from martinsawicki/ag
Browse files Browse the repository at this point in the history
Fix for issue 1594: enhancing PowerState extensible enum
  • Loading branch information
Martin Sawicki committed Apr 26, 2017
2 parents 46e97cf + 2305ff3 commit 4f8b533
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
Expand Up @@ -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<String, PowerState> VALUES_BY_NAME = new HashMap<>();

/**
* Static value PowerState/running for PowerState.
*/
Expand All @@ -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<PowerState> 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);
}
}

/**
Expand All @@ -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());
}
}
}
Expand All @@ -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);
}
}
}
}
Expand Up @@ -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<Region> valuesCollection = VALUES_BY_NAME.values();
Expand Down Expand Up @@ -161,7 +160,7 @@ public static Region fromName(String name) {

@Override
public int hashCode() {
return name.hashCode();
return this.name.hashCode();
}

@Override
Expand All @@ -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);
}
}
}

0 comments on commit 4f8b533

Please sign in to comment.