Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a resolveClass method helper

  • Loading branch information...
commit 5bae67d11cb8837b31252c7f1f1e50026347bbcc 1 parent f44914c
Mark Derricutt authored May 25, 2012
33  src/main/java/com/theoryinpractise/halbuilder/impl/resources/BaseResource.java
@@ -20,10 +20,12 @@
20 20
 import com.theoryinpractise.halbuilder.spi.Contract;
21 21
 import com.theoryinpractise.halbuilder.spi.Link;
22 22
 import com.theoryinpractise.halbuilder.spi.ReadableResource;
  23
+import com.theoryinpractise.halbuilder.spi.Renderer;
23 24
 import com.theoryinpractise.halbuilder.spi.Resource;
24 25
 import com.theoryinpractise.halbuilder.spi.ResourceException;
25 26
 
26 27
 import javax.annotation.Nullable;
  28
+import java.io.StringWriter;
27 29
 import java.net.MalformedURLException;
28 30
 import java.net.URL;
29 31
 import java.util.Collection;
@@ -275,6 +277,37 @@ public ImmutableResource toImmutableResource() {
275 277
         return new ImmutableResource(resourceFactory, getNamespaces(), getCanonicalLinks(), getProperties(), getResources(), hasNullProperties);
276 278
     }
277 279
 
  280
+
  281
+    /**
  282
+     * Renders the current Resource as a proxy to the provider interface
  283
+     *
  284
+     * @param anInterface The interface we wish to proxy the resource as
  285
+     * @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
  286
+     */
  287
+    public <T> Optional<T> renderClass(Class<T> anInterface) {
  288
+        if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
  289
+            return InterfaceRenderer.newInterfaceRenderer(anInterface).render(this, null);
  290
+        } else {
  291
+            return Optional.absent();
  292
+        }
  293
+    }
  294
+
  295
+    public String renderContent(String contentType) {
  296
+        Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
  297
+        return renderAsString(renderer);
  298
+    }
  299
+
  300
+    public <T> Optional<T> resolveClass(Function<ReadableResource, Optional<T>> resolver) {
  301
+        return resolver.apply(this);
  302
+    }
  303
+
  304
+    private String renderAsString(final Renderer renderer) {
  305
+        validateNamespaces(this);
  306
+        StringWriter sw = new StringWriter();
  307
+        renderer.render(this, sw);
  308
+        return sw.toString();
  309
+    }
  310
+
278 311
     @Override
279 312
     public int hashCode() {
280 313
         int h = namespaces.hashCode();
27  src/main/java/com/theoryinpractise/halbuilder/impl/resources/ImmutableResource.java
@@ -25,7 +25,7 @@ public ImmutableResource(ResourceFactory resourceFactory,
25 25
         this.resources = resources;
26 26
 
27 27
         this.resourceLink = super.getResourceLink();
28  
-        
  28
+
29 29
         this.hasNullProperties = hasNullProperties;
30 30
     }
31 31
 
@@ -33,30 +33,5 @@ public Link getResourceLink() {
33 33
         return resourceLink;
34 34
     }
35 35
 
36  
-    /**
37  
-     * Renders the current Resource as a proxy to the provider interface
38  
-     *
39  
-     * @param anInterface The interface we wish to proxy the resource as
40  
-     * @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
41  
-     */
42  
-    public <T> Optional<T> renderClass(Class<T> anInterface) {
43  
-        if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
44  
-            return InterfaceRenderer.newInterfaceRenderer(anInterface).render(this, null);
45  
-        } else {
46  
-            return Optional.absent();
47  
-        }
48  
-    }
49  
-
50  
-    public String renderContent(String contentType) {
51  
-        Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
52  
-        return renderAsString(renderer);
53  
-    }
54  
-
55  
-    private String renderAsString(final Renderer renderer) {
56  
-        validateNamespaces(this);
57  
-        StringWriter sw = new StringWriter();
58  
-        renderer.render(this, sw);
59  
-        return sw.toString();
60  
-    }
61 36
 
62 37
 }
25  src/main/java/com/theoryinpractise/halbuilder/impl/resources/MutableResource.java
@@ -199,29 +199,4 @@ public MutableResource withSubresource(String rel, Resource resource) {
199 199
         return this;
200 200
     }
201 201
 
202  
-    /**
203  
-     * Renders the current Resource as a proxy to the provider interface
204  
-     *
205  
-     * @param anInterface The interface we wish to proxy the resource as
206  
-     * @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
207  
-     */
208  
-    public <T> Optional<T> renderClass(Class<T> anInterface) {
209  
-        if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
210  
-            return InterfaceRenderer.newInterfaceRenderer(anInterface).render(toImmutableResource(), null);
211  
-        } else {
212  
-            return Optional.absent();
213  
-        }
214  
-    }
215  
-
216  
-    public String renderContent(String contentType) {
217  
-        Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
218  
-        return renderAsString(renderer);
219  
-    }
220  
-
221  
-    private String renderAsString(final Renderer renderer) {
222  
-        validateNamespaces(this);
223  
-        StringWriter sw = new StringWriter();
224  
-        renderer.render(toImmutableResource(), sw);
225  
-        return sw.toString();
226  
-    }
227 202
 }
9  src/main/java/com/theoryinpractise/halbuilder/spi/ReadableResource.java
@@ -135,4 +135,13 @@
135 135
      * @return A String
136 136
      */
137 137
     String renderContent(String contentType);
  138
+
  139
+    /**
  140
+     * Returns an optional proxy to the given interface mirroring the resource.
  141
+     *
  142
+     * @param anInterface An interface to mirror
  143
+     * @return A Guava Optional Resource Proxy
  144
+     */
  145
+    <T> Optional<T> resolveClass(Function<ReadableResource, Optional<T>> resolver);
  146
+
138 147
 }

0 notes on commit 5bae67d

Please sign in to comment.
Something went wrong with that request. Please try again.