Issue 695: Created builder for links and fixed order of constructor args

This commit is contained in:
Jason King 2011-11-21 16:44:28 +00:00
parent ca69fbf0f7
commit e2d7b66b0c
4 changed files with 126 additions and 28 deletions

View File

@ -123,7 +123,7 @@ public class Link extends BaseNamedResource<Link> {
@Override
public Link build() {
return new Link(href, name, type, rel);
return new Link(href, type, name, rel);
}
public Builder fromLink(Link in) {

View File

@ -25,6 +25,8 @@ import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Wraps individual Link elements.
* Needed because parsing is done with JAXB and it does not handle Generic collections
@ -32,36 +34,75 @@ import java.util.Set;
*/
public class Links {
private LinkedHashSet<Link> links = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
@XmlElement(name = "Link")
public void setLink(Link link) {
this.links.add(link);
}
public Builder toBuilder() {
return new Builder().fromLinks(this);
}
public Set<Link> getLinks() {
return Collections.unmodifiableSet(links);
}
public static class Builder {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
private Set<Link> links = Sets.newLinkedHashSet();
Links links1 = (Links) o;
/**
* @see Links#getLinks
*/
public Builder links(Set<Link> links) {
this.links = Sets.newLinkedHashSet(checkNotNull(links, "links"));
return this;
}
if (!links.equals(links1.links)) return false;
public Builder addLink(Link link) {
links.add(checkNotNull(link,"link"));
return this;
}
return true;
}
public Links build() {
return new Links(links);
}
@Override
public int hashCode() {
return links.hashCode();
}
public Builder fromLinks(Links in) {
return links(in.getLinks());
}
}
public String toString() {
return "["+ links.toString()+"]";
}
@XmlElement(name = "Link")
private LinkedHashSet<Link> links = Sets.newLinkedHashSet();
private Links() {
//For JAXB
}
private Links(Set<Link> links) {
this.links = Sets.newLinkedHashSet(links);
}
public Set<Link> getLinks() {
return Collections.unmodifiableSet(links);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Links links1 = (Links) o;
if (!links.equals(links1.links)) return false;
return true;
}
@Override
public int hashCode() {
return links.hashCode();
}
public String toString() {
return "["+ links.toString()+"]";
}
}

View File

@ -62,7 +62,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
public static class Builder extends BaseNamedResource.Builder<VirtualMachine> {
//TODO There are some more fields
private Links links = new Links();
private Links links = Links.builder().build();
private Actions actions = Actions.builder().build();
private Tasks tasks = new Tasks();
private String description;
@ -81,8 +81,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
* @see VirtualMachine#getLinks
*/
public Builder links(Set<Link> links) {
checkNotNull(links,"links");
for(Link link:links) this.links.setLink(link);
this.links = Links.builder().links(checkNotNull(links,"links")).build();
return this;
}
@ -269,7 +268,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
}
@XmlElement(name = "Links", required = true)
private Links links = new Links();
private Links links = Links.builder().build();
@XmlElement(name = "Tasks", required = true)
private Tasks tasks = new Tasks();

View File

@ -0,0 +1,58 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.tmrk.enterprisecloud.domain;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;
import static org.jclouds.tmrk.enterprisecloud.domain.Link.Relationship;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @author Jason King
*/
@Test(groups = "unit", testName = "LinksTest")
public class LinksTest {
private Link link;
private Links links;
@BeforeMethod()
public void setUp() throws URISyntaxException {
link = Link.builder().href(new URI("/1")).name("my link").type("test link").rel(Relationship.FIRST).build();
links = Links.builder().addLink(link).build();
}
@Test
public void testAddAction() throws URISyntaxException {
Link link2 = Link.builder().href(new URI("/2")).name("my link 2").type("test link").rel(Relationship.LAST).build();
Links twoLinks = links.toBuilder().addLink(link2).build();
Set<Link> linkSet = twoLinks.getLinks();
assertEquals(2, linkSet.size());
assertTrue(linkSet.contains(link));
assertTrue(linkSet.contains(link2));
}
}