New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[OSJC-169] changed KubernetesResource#equals #hashCode to constant behav #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,14 +31,14 @@ | |
* | ||
* @author Jeff Cantrill | ||
*/ | ||
public class KubernetesResource implements IResource, ResourcePropertyKeys{ | ||
public abstract class KubernetesResource implements IResource, ResourcePropertyKeys { | ||
|
||
private ModelNode node; | ||
private IClient client; | ||
private Map<Class<? extends ICapability>, ICapability> capabilities = new HashMap<Class<? extends ICapability>, ICapability>(); | ||
private Map<String, String []> propertyKeys; | ||
|
||
public KubernetesResource(ModelNode node, IClient client, Map<String, String []> propertyKeys){ | ||
protected KubernetesResource(ModelNode node, IClient client, Map<String, String []> propertyKeys){ | ||
this.node = node; | ||
this.client = client; | ||
this.propertyKeys = propertyKeys; | ||
|
@@ -105,11 +105,11 @@ public void refresh(){ | |
this.node = ModelNode.fromJSONString(client.get(getKind(), getName(), getNamespace()).toString()); | ||
} | ||
|
||
// TODO Pretty certain this should be protected | ||
@Override | ||
public ResourceKind getKind(){ | ||
if(node.has("kind")){ | ||
return ResourceKind.valueOf(node.get("kind").asString()); | ||
ModelNode kindNode = get(ResourcePropertyKeys.KIND); | ||
if(kindNode.isDefined()){ | ||
return ResourceKind.valueOf(kindNode.asString()); | ||
} | ||
return null; | ||
} | ||
|
@@ -128,7 +128,6 @@ public String getName(){ | |
return asString(NAME); | ||
} | ||
|
||
@Override | ||
public void setName(String name) { | ||
set(NAME, name); | ||
} | ||
|
@@ -142,14 +141,13 @@ public String getNamespace(){ | |
return node.asString(); | ||
} | ||
|
||
@Override | ||
public void setNamespace(String namespace){ | ||
set(NAMESPACE, namespace); | ||
} | ||
|
||
@Override | ||
public void addLabel(String key, String value) { | ||
ModelNode labels = node.get("labels"); | ||
ModelNode labels = node.get(ResourcePropertyKeys.LABELS); | ||
labels.get(key).set(value); | ||
} | ||
|
||
|
@@ -161,20 +159,25 @@ public Map<String, String> getLabels() { | |
|
||
/*---------- utility methods ------*/ | ||
protected ModelNode get(String key){ | ||
String [] property = propertyKeys.get(key); | ||
return node.get(property); | ||
return node.get(getPath(key)); | ||
} | ||
|
||
protected void set(String key, int value) { | ||
String [] property = propertyKeys.get(key); | ||
node.get(property).set(value); | ||
node.get(getPath(key)).set(value); | ||
} | ||
|
||
protected void set(String key, String value){ | ||
String [] property = propertyKeys.get(key); | ||
node.get(property).set(value); | ||
node.get(getPath(key)).set(value); | ||
} | ||
|
||
private String[] getPath(String key) { | ||
String [] property = propertyKeys.get(key); | ||
if (property == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adietish we should consider throwing here/providing an indication that the path doesnt exist. If the path doesnt exist for a given property then it needs to be added to the map IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jcantrill isnt the property map a mapping so that if a special mapping is missing it indicates that the key is to be used as is? What is the purpose of these property-keys? |
||
throw new IllegalArgumentException(String.format("key %s is not known to the resource %s", key, getName().isEmpty()? getClass().getSimpleName() : getName())); | ||
} | ||
return property; | ||
} | ||
|
||
protected Map<String, String> asMap(String property){ | ||
return JBossDmrExtentions.asMap(this.node, propertyKeys, property); | ||
} | ||
|
@@ -203,26 +206,52 @@ public String toPrettyString(){ | |
|
||
@Override | ||
public int hashCode() { | ||
String namespace = getNamespace(); | ||
String name = getName(); | ||
ResourceKind kind = getKind(); | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((node == null) ? 0 : node.hashCode()); | ||
return result; | ||
return prime * (namespace.hashCode() + name.hashCode() + kind.hashCode()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
else if (obj == null) | ||
return false; | ||
KubernetesResource other = (KubernetesResource) obj; | ||
if (node == null) { | ||
if (other.node != null) | ||
return false; | ||
} else if (!node.equals(other.node)) | ||
else if (getClass() != obj.getClass()) | ||
return false; | ||
else { | ||
KubernetesResource other = (KubernetesResource) obj; | ||
if (getKind() != null){ | ||
if (getKind() != other.getKind()) { | ||
return false; | ||
} | ||
} else { | ||
if (other.getKind() != null) { | ||
return false; | ||
} | ||
} | ||
if (getNamespace() != null) { | ||
if(!getNamespace().equals(other.getNamespace())) { | ||
return false; | ||
} | ||
} else { | ||
if (other.getNamespace() != null) { | ||
return false; | ||
} | ||
} | ||
if (getName() != null) { | ||
if(!getName().equals(other.getName())) { | ||
return false; | ||
} | ||
} else { | ||
if (other.getName() != null) { | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
return true; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will throw for unknown/new kinds that we are not aware of. We should either get away from enums or provide a lenient #valueOf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adietish this was one of my thoughts to KubernetesResource not being abstract for the cases of when we do have kinds we dont understand about and we decide to move away from enum