tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Tapestry > IoC - coerce
Date Mon, 29 Nov 2010 00:58:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/12/_/styles/combined.css?spaceKey=TAPESTRY&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/TAPESTRY/IoC+-+coerce">IoC
- coerce</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bobharner">Bob
Harner</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Fixed broken image, spelling, formatting, reorganized lead paragraph<br />
    </div>
        <br/>
                         <h4>Changes (15)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{float} <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Tapestry
frequently must coerce objects from one type to another. By coercion, we mean to convert an
object of some type into a new object of a different type with similar content: a common example
is coercing a string into an integer or a double. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">*Type
Coercion* is the conversion of one type of object to a new object of a different type with
similar content. Tapestry frequently must coerce objects from one type to another.  A common
example is the coercion of a string into an integer or a double. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >Although these types of coercions
happens more inside tapestry-core <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">(inlcuding</span>
<span class="diff-added-words"style="background-color: #dfd;">(including</span>
coercions of [component <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">parameters|IoC
- coerce]),</span> <span class="diff-added-words"style="background-color: #dfd;">parameters|Component
Parameters]),</span> this may also happen inside tapestry-ioc, such as when injecting
a value, rather than a service, into a builder method. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Like everything else in
Tapestry, type coercions are extensible. At the root is the [TypeCoercer|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/services/TypeCoercer.html]
service. Its configuration consists of a number of [CoercionTuple|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/services/CoercionTuple.html]s.
Each tuple defines how to coerce from one type to another. The initial set of coercions is
focused primarily on coercions between different numeric types: <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">!type-coercer.png!
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">!images/type-coercer.png!Default
Type Coercions There&#39;s a few special coercions related to null there; Object --&gt;
List wraps a lone object as a singleton list, we then need null --&gt; List to ensure
that null stays null (rather than a singleton list whose lone element is a null). <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h2.
Default Type Coercions <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">There
are a few special coercions related to null there; Object --&gt; List wraps a lone object
as a singleton list, we then need null --&gt; List to ensure that null stays null (rather
than a singleton list whose lone element is a null). <br> <br></td></tr>
            <tr><td class="diff-unchanged" >Tapestry can _interpolate_ necessary
coercions. For example, say it is necessary to coerce a StringBuffer to an Integer; the TypeCoercer
will chain together a series of coercions: <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >The only built-in null coercion is
from null to boolean (which is always false). <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >h2. Contributing <span class="diff-changed-words"><span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">n</span><span
class="diff-added-chars"style="background-color: #dfd;">N</span>ew</span> Coercions
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >TypeCoercer is <span class="diff-changed-words">extensible<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">,</span><span
class="diff-added-chars"style="background-color: #dfd;">;</span></span> you
may add new coercions as desired. For example, let&#39;s say you have a <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>Money<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> type that represents an amount of some currency, and
you want to be able to convert from <span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>BigDecimal<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> to <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>Money<span class="diff-added-chars"style="background-color:
#dfd;">}</span>.</span> Further, let&#39;s assume that <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>Money<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> has a constructor that accepts a <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>BigDecimal<span
class="diff-added-chars"style="background-color: #dfd;">}}</span></span> as
its parameter. We&#39;ll use a little Tapestry IOC configuration jujitsu to inform the
TypeCoercer about this coercion. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code:java} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >   }{code} <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Further,
since TypeCoercer knows how to convert Double to BigDecimal, or even Integer (to Long to Double)
to BigDecimal, all of those coercions would work as well. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Further,
since TypeCoercer knows how to convert {{Double}} to {{BigDecimal}}, or even {{Integer}} (to
{{Long}} to {{Double}}) to {{BigDecimal}}, all of those coercions would work as well. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">When
creating a coercion from null, use Void.class as the source type. For example, the builtin
coercion from null to Boolean is implemented as: <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">When
creating a coercion from {{null}}, use {{Void.class}} as the source type. For example, the
built-in coercion from {{null}} to {{Boolean}} is implemented as: <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code:java} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >                return false; <br>
           } <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">}));<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">{code}</span></span>
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code}
<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="IoC-coerce-TypeCoercion"></a>Type Coercion</h1>

<div class='navmenu' style='float:right; background:white; margin:3px; padding:3px'><div
class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Related Articles</b><br
/><ul>
	<li><a href="/confluence/display/TAPESTRY/Type+Coercion" title="Type Coercion">Parameter
Type Coercion</a></li>
</ul>
</td></tr></table></div></div>

<p><b>Type Coercion</b> is the conversion of one type of object to a new
object of a different type with similar content. Tapestry frequently must coerce objects from
one type to another.  A common example is the coercion of a string into an integer or a double.</p>

<p>Although these types of coercions happens more inside tapestry-core (including coercions
of <a href="/confluence/display/TAPESTRY/Component+Parameters" title="Component Parameters">component
parameters</a>), this may also happen inside tapestry-ioc, such as when injecting a
value, rather than a service, into a builder method.</p>

<p>Like everything else in Tapestry, type coercions are extensible. At the root is the
<a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/services/TypeCoercer.html"
class="external-link" rel="nofollow">TypeCoercer</a> service. Its configuration consists
of a number of <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/services/CoercionTuple.html"
class="external-link" rel="nofollow">CoercionTuple</a>s. Each tuple defines how to
coerce from one type to another. The initial set of coercions is focused primarily on coercions
between different numeric types:</p>

<p><span class="image-wrap" style=""><img src="/confluence/download/attachments/23338478/type-coercer.png?version=1&amp;modificationDate=1290991716094"
style="border: 0px solid black" /></span></p>

<h2><a name="IoC-coerce-DefaultTypeCoercions"></a>Default Type Coercions</h2>

<p>There are a few special coercions related to null there; Object --&gt; List wraps
a lone object as a singleton list, we then need null --&gt; List to ensure that null stays
null (rather than a singleton list whose lone element is a null).</p>

<p>Tapestry can <em>interpolate</em> necessary coercions. For example, say
it is necessary to coerce a StringBuffer to an Integer; the TypeCoercer will chain together
a series of coercions:</p>

<ul>
	<li>Object --&gt; String</li>
	<li>String --&gt; Long</li>
	<li>Long --&gt; Integer</li>
</ul>


<h2><a name="IoC-coerce-Coercingfromnull"></a>Coercing from null</h2>

<p>Coercing from null is special; it is not a spanning search as with the other types.
Either there is a specific coercion from null to the desired type, or no coercion takes places
(and the coerced value is null).</p>

<p>The only built-in null coercion is from null to boolean (which is always false).</p>

<h2><a name="IoC-coerce-ContributingNewCoercions"></a>Contributing New Coercions</h2>

<p>TypeCoercer is extensible; you may add new coercions as desired. For example, let's
say you have a <tt>Money</tt> type that represents an amount of some currency,
and you want to be able to convert from <tt>BigDecimal</tt> to <tt>Money}.
Further, let's assume that {{Money</tt> has a constructor that accepts a <tt>BigDecimal</tt>
as its parameter. We'll use a little Tapestry IOC configuration jujitsu to inform the TypeCoercer
about this coercion.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   <span class="code-keyword">public</span> <span class="code-keyword">static</span>
void contributeTypeCoercer(Configuration&lt;CoercionTuple&gt; configuration)
   {
     Coercion&lt;BigDecimal, Money&gt; coercion = <span class="code-keyword">new</span>
Coercion&lt;BigDecimal, Money&gt;()
     {
       <span class="code-keyword">public</span> Money coerce(BigDecimal input)
       {
         <span class="code-keyword">return</span> <span class="code-keyword">new</span>
Money(input);
       }
     };
     
     configuration.add(<span class="code-keyword">new</span> CoercionTuple&lt;BigDecimal,
Money&gt;(BigDecimal.class, Money.class, coercion));     
   }</pre>
</div></div>

<p>Further, since TypeCoercer knows how to convert <tt>Double</tt> to <tt>BigDecimal</tt>,
or even <tt>Integer</tt> (to <tt>Long</tt> to <tt>Double</tt>)
to <tt>BigDecimal</tt>, all of those coercions would work as well.</p>

<p>When creating a coercion from <tt>null</tt>, use <tt>Void.class</tt>
as the source type. For example, the built-in coercion from <tt>null</tt> to <tt>Boolean</tt>
is implemented as:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  configuration.add(<span class="code-keyword">new</span> CoercionTuple(void.class,
<span class="code-object">Boolean</span>.class,
        <span class="code-keyword">new</span> Coercion&lt;<span class="code-object">Void</span>,
<span class="code-object">Boolean</span>&gt;()
        {
            <span class="code-keyword">public</span> <span class="code-object">Boolean</span>
coerce(<span class="code-object">Void</span> input)
            {
                <span class="code-keyword">return</span> <span class="code-keyword">false</span>;
            }
        }));
</pre>
</div></div>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/TAPESTRY/IoC+-+coerce">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23338478&revisedVersion=5&originalVersion=4">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message