Skip to content

Commit

Permalink
Added a resolveClass method helper
Browse files Browse the repository at this point in the history
  • Loading branch information
talios committed May 25, 2012
1 parent f44914c commit 5bae67d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import com.theoryinpractise.halbuilder.spi.Contract;
import com.theoryinpractise.halbuilder.spi.Link;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
import com.theoryinpractise.halbuilder.spi.Renderer;
import com.theoryinpractise.halbuilder.spi.Resource;
import com.theoryinpractise.halbuilder.spi.ResourceException;

import javax.annotation.Nullable;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
Expand Down Expand Up @@ -275,6 +277,37 @@ public ImmutableResource toImmutableResource() {
return new ImmutableResource(resourceFactory, getNamespaces(), getCanonicalLinks(), getProperties(), getResources(), hasNullProperties);
}


/**
* Renders the current Resource as a proxy to the provider interface
*
* @param anInterface The interface we wish to proxy the resource as
* @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
*/
public <T> Optional<T> renderClass(Class<T> anInterface) {
if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
return InterfaceRenderer.newInterfaceRenderer(anInterface).render(this, null);
} else {
return Optional.absent();
}
}

public String renderContent(String contentType) {
Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
return renderAsString(renderer);
}

public <T> Optional<T> resolveClass(Function<ReadableResource, Optional<T>> resolver) {
return resolver.apply(this);
}

private String renderAsString(final Renderer renderer) {
validateNamespaces(this);
StringWriter sw = new StringWriter();
renderer.render(this, sw);
return sw.toString();
}

@Override
public int hashCode() {
int h = namespaces.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,13 @@ public ImmutableResource(ResourceFactory resourceFactory,
this.resources = resources;

this.resourceLink = super.getResourceLink();

this.hasNullProperties = hasNullProperties;
}

public Link getResourceLink() {
return resourceLink;
}

/**
* Renders the current Resource as a proxy to the provider interface
*
* @param anInterface The interface we wish to proxy the resource as
* @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
*/
public <T> Optional<T> renderClass(Class<T> anInterface) {
if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
return InterfaceRenderer.newInterfaceRenderer(anInterface).render(this, null);
} else {
return Optional.absent();
}
}

public String renderContent(String contentType) {
Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
return renderAsString(renderer);
}

private String renderAsString(final Renderer renderer) {
validateNamespaces(this);
StringWriter sw = new StringWriter();
renderer.render(this, sw);
return sw.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,4 @@ public MutableResource withSubresource(String rel, Resource resource) {
return this;
}

/**
* Renders the current Resource as a proxy to the provider interface
*
* @param anInterface The interface we wish to proxy the resource as
* @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
*/
public <T> Optional<T> renderClass(Class<T> anInterface) {
if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
return InterfaceRenderer.newInterfaceRenderer(anInterface).render(toImmutableResource(), null);
} else {
return Optional.absent();
}
}

public String renderContent(String contentType) {
Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
return renderAsString(renderer);
}

private String renderAsString(final Renderer renderer) {
validateNamespaces(this);
StringWriter sw = new StringWriter();
renderer.render(toImmutableResource(), sw);
return sw.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,13 @@ public interface ReadableResource {
* @return A String
*/
String renderContent(String contentType);

/**
* Returns an optional proxy to the given interface mirroring the resource.
*
* @param anInterface An interface to mirror
* @return A Guava Optional Resource Proxy
*/
<T> Optional<T> resolveClass(Function<ReadableResource, Optional<T>> resolver);

}

0 comments on commit 5bae67d

Please sign in to comment.