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,11 +34,50 @@ import java.util.Set;
*/
public class Links {
private LinkedHashSet<Link> links = Sets.newLinkedHashSet();
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromLinks(this);
}
public static class Builder {
private Set<Link> links = Sets.newLinkedHashSet();
/**
* @see Links#getLinks
*/
public Builder links(Set<Link> links) {
this.links = Sets.newLinkedHashSet(checkNotNull(links, "links"));
return this;
}
public Builder addLink(Link link) {
links.add(checkNotNull(link,"link"));
return this;
}
public Links build() {
return new Links(links);
}
public Builder fromLinks(Links in) {
return links(in.getLinks());
}
}
@XmlElement(name = "Link")
public void setLink(Link link) {
this.links.add(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() {

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));
}
}