<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:georss="http://www.georss.org/georss" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Nesterovsky bros - Java</title>
    <link>http://www.nesterovsky-bros.com/weblog/</link>
    <description />
    <language>en-us</language>
    <copyright>Nesterovsky bros</copyright>
    <lastBuildDate>Thu, 08 Mar 2012 21:56:19 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>contact@nesterovsky-bros.com</managingEditor>
    <webMaster>contact@nesterovsky-bros.com</webMaster>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=8383cfc7-10ac-4f2a-a249-4cc37ddbac70</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=8383cfc7-10ac-4f2a-a249-4cc37ddbac70</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For a long time we were developing web applications with ASP.NET and JSF. At present
we prefer rich clients and a server with page templates and RESTful web services.
</p>
        <p>
This transition brings technical questions. Consider this one.
</p>
        <p>
Browsers allow to store session state entirely on the client, so should we maintain
a session on the server?
</p>
        <p>
Since the server is just a set of web services, so we may supply all required arguments
on each call. 
</p>
        <p>
At first glance we can assume that no session is required on the server. However,
looking further we see that we should deal with data validation (security) on the
server. 
</p>
        <p>
Think about a classic ASP.NET application, where a user can select a value from a
dropdown. Either ASP.NET itself or your program (against a list from a session) verifies
that the value received is valid for the user. That list of values and might be other
parameters constitute a user profile, which we stored in session. The user profile
played important role (often indirectly) in the validation of input data.
</p>
        <p>
When the server is just a set of web services then we have to validate all parameters
manually. There are two sources that we can rely to: (a) <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.session.aspx"> a
session</a>, (b) <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.user.aspx"> a
user principal</a>.
</p>
        <p>
The case (a) is very similar to classic ASP.NET application except that with <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fydy4x04a.aspx">EnableEventValidation="true"</a> runtime
did it for us most of the time.<br />
The case (b) requires reconstruction of the user profile for a user principal and
then we proceed with validation of parameters. 
</p>
        <p>
We may cache user profile in session, in which case we reduce (b) to (a); on the other
hand we may cache user profile in <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.caching.cache.aspx"> Cache</a>,
which is also similar to (a) but which might be lighter than (at least not heavier
than) the solution with the session.
</p>
        <p>
What we see is that the client session does not free us from server session (or its
alternative).
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70" />
      </body>
      <title>Shifting to client side web applications</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/03/08/ShiftingToClientSideWebApplications.aspx</link>
      <pubDate>Thu, 08 Mar 2012 21:56:19 GMT</pubDate>
      <description>  &lt;p&gt;
For a long time we were developing web applications with ASP.NET and JSF. At present
we prefer rich clients and a server with page templates and RESTful web services.
&lt;/p&gt;
&lt;p&gt;
This transition brings technical questions. Consider this one.
&lt;/p&gt;
&lt;p&gt;
Browsers allow to store session state entirely on the client, so should we maintain
a session on the server?
&lt;/p&gt;
&lt;p&gt;
Since the server is just a set of web services, so we may supply all required arguments
on each call. 
&lt;/p&gt;
&lt;p&gt;
At first glance we can assume that no session is required on the server. However,
looking further we see that we should deal with data validation (security) on the
server. 
&lt;/p&gt;
&lt;p&gt;
Think about a classic ASP.NET application, where a user can select a value from a
dropdown. Either ASP.NET itself or your program (against a list from a session) verifies
that the value received is valid for the user. That list of values and might be other
parameters constitute a user profile, which we stored in session. The user profile
played important role (often indirectly) in the validation of input data.
&lt;/p&gt;
&lt;p&gt;
When the server is just a set of web services then we have to validate all parameters
manually. There are two sources that we can rely to: (a) &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.session.aspx"&gt; a
session&lt;/a&gt;, (b) &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.services.webservice.user.aspx"&gt; a
user principal&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The case (a) is very similar to classic ASP.NET application except that with &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fydy4x04a.aspx"&gt;EnableEventValidation="true"&lt;/a&gt; runtime
did it for us most of the time.&lt;br /&gt;
The case (b) requires reconstruction of the user profile for a user principal and
then we proceed with validation of parameters. 
&lt;/p&gt;
&lt;p&gt;
We may cache user profile in session, in which case we reduce (b) to (a); on the other
hand we may cache user profile in &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fsystem.web.caching.cache.aspx"&gt; Cache&lt;/a&gt;,
which is also similar to (a) but which might be lighter than (at least not heavier
than) the solution with the session.
&lt;/p&gt;
&lt;p&gt;
What we see is that the client session does not free us from server session (or its
alternative).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8383cfc7-10ac-4f2a-a249-4cc37ddbac70" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8383cfc7-10ac-4f2a-a249-4cc37ddbac70.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>Java</category>
      <category>JSF and Facelets</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=7a2cda28-6e38-48d8-a233-302ba6e1d75e</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=7a2cda28-6e38-48d8-a233-302ba6e1d75e</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A bit history: the <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;url=http%3a%2f%2fwww.bphx.com%2fen%2fNewsEvents%2fPressReleases%2fPages%2f072902.aspx" target="_blank">first
release</a> of this solution was about 9.5 years ago...
</p>
        <p>
Today we've run into a strange situation. One of our clients ask us about automatic
conversion of data from mainframe (that were defined as COBOL copybooks) into XML
or Java/.NET objects. On our suggestion to use <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml">eXperanto</a>,
which is well known to him, he stated that he wouldn't like to use a tool of a company
that is no more exists...
</p>
        <p>
The situation, in our opinion, become more strange when you consider the following:
</p>
        <ul>
          <li>
            <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml">eXperanto</a> (the
design-time tool and run-time libraries for Java and .NET) were developed, well tested,
and delivered by us to production already several years ago.</li>
          <li>
the client bought this set (the tool and libraries).</li>
          <li>
the set is in production yet already in another big company, and is used time to time
by our company in different migration projects.</li>
          <li>
the client talks with developers of this tool and run-time libraries, and he knows
about this fact.</li>
          <li>
the client uses widely open source solutions even without dedicated vendors or support
warranties.</li>
        </ul>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e" />
      </body>
      <title>eXperanto project actual yet...</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/11/10/eXperantoProjectActualYet.aspx</link>
      <pubDate>Thu, 10 Nov 2011 22:14:09 GMT</pubDate>
      <description>&lt;p&gt;
A bit history: the &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;amp;url=http%3a%2f%2fwww.bphx.com%2fen%2fNewsEvents%2fPressReleases%2fPages%2f072902.aspx" target="_blank"&gt;first
release&lt;/a&gt; of this solution was about 9.5 years ago...
&lt;/p&gt;
&lt;p&gt;
Today we've run into a strange situation. One of our clients ask us about automatic
conversion of data from mainframe (that were defined as COBOL copybooks) into XML
or Java/.NET objects. On our suggestion to use &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml"&gt;eXperanto&lt;/a&gt;,
which is well known to him, he stated that he wouldn't like to use a tool of a company
that is no more exists...
&lt;/p&gt;
&lt;p&gt;
The situation, in our opinion, become more strange when you consider the following:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdata%2fprojects.xml"&gt;eXperanto&lt;/a&gt; (the
design-time tool and run-time libraries for Java and .NET) were developed, well tested,
and delivered by us to production already several years ago.&lt;/li&gt;
&lt;li&gt;
the client bought this set (the tool and libraries).&lt;/li&gt;
&lt;li&gt;
the set is in production yet already in another big company, and is used time to time
by our company in different migration projects.&lt;/li&gt;
&lt;li&gt;
the client talks with developers of this tool and run-time libraries, and he knows
about this fact.&lt;/li&gt;
&lt;li&gt;
the client uses widely open source solutions even without dedicated vendors or support
warranties.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=7a2cda28-6e38-48d8-a233-302ba6e1d75e" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,7a2cda28-6e38-48d8-a233-302ba6e1d75e.aspx</comments>
      <category>.NET</category>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=582c91ec-61fc-4bf4-a65c-bf410715a420</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,582c91ec-61fc-4bf4-a65c-bf410715a420.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,582c91ec-61fc-4bf4-a65c-bf410715a420.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=582c91ec-61fc-4bf4-a65c-bf410715a420</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Incidentally, we have found one new implementation of yield return in java that is
in the development stage. Sources can be found at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=https%3a%2f%2fgithub.com%2fpeichhorn%2flombok-pg%2fzipball%2fmaster"> https://github.com/peichhorn/lombok-pg/zipball/master</a>.
Just to be sure we have copied those sources at other place <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fplagiarism%2fpeichhorn-lombok-pg-0.10.0-39-g384fb7b.zip"> peichhorn-lombok-pg-0.10.0-39-g384fb7b.zip</a> (you
may search "yield" in the archive).
</p>
        <p>
It's broken according to source tracker, but the funny thing is that sources, however
different, still resemble our yield return implementation (<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar">Yield.jar</a>, <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.3.7.jar">Yield.3.7.jar</a> -
Indigo, <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip">Yield.zip</a> -
sources) very much: variable names, error messages, algorithmic structure.
</p>
        <p>
Those programmers probably have forgotten good manners: to reference a base work,
at least.
</p>
        <p>
Well, we generously forgive them this blunder.
</p>
        <p>
P.S. our implementation, in contrast, works without bugs.
</p>
        <p>
P.P.S. misunderstanding is resolved. See comments.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420" />
      </body>
      <title>Funny, about yield return in java</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,582c91ec-61fc-4bf4-a65c-bf410715a420.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/10/16/FunnyAboutYieldReturnInJava.aspx</link>
      <pubDate>Sun, 16 Oct 2011 13:49:33 GMT</pubDate>
      <description>  &lt;p&gt;
Incidentally, we have found one new implementation of yield return in java that is
in the development stage. Sources can be found at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;amp;url=https%3a%2f%2fgithub.com%2fpeichhorn%2flombok-pg%2fzipball%2fmaster"&gt; https://github.com/peichhorn/lombok-pg/zipball/master&lt;/a&gt;.
Just to be sure we have copied those sources at other place &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fplagiarism%2fpeichhorn-lombok-pg-0.10.0-39-g384fb7b.zip"&gt; peichhorn-lombok-pg-0.10.0-39-g384fb7b.zip&lt;/a&gt; (you
may search "yield" in the archive).
&lt;/p&gt;
&lt;p&gt;
It's broken according to source tracker, but the funny thing is that sources, however
different, still resemble our yield return implementation (&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar"&gt;Yield.jar&lt;/a&gt;, &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.3.7.jar"&gt;Yield.3.7.jar&lt;/a&gt; -
Indigo, &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"&gt;Yield.zip&lt;/a&gt; -
sources) very much: variable names, error messages, algorithmic structure.
&lt;/p&gt;
&lt;p&gt;
Those programmers probably have forgotten good manners: to reference a base work,
at least.
&lt;/p&gt;
&lt;p&gt;
Well, we generously forgive them this blunder.
&lt;/p&gt;
&lt;p&gt;
P.S. our implementation, in contrast, works without bugs.
&lt;/p&gt;
&lt;p&gt;
P.P.S. misunderstanding is resolved. See comments.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=582c91ec-61fc-4bf4-a65c-bf410715a420" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,582c91ec-61fc-4bf4-a65c-bf410715a420.aspx</comments>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=a2345b18-13e4-4f60-b798-4b28587a18cb</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a2345b18-13e4-4f60-b798-4b28587a18cb.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,a2345b18-13e4-4f60-b798-4b28587a18cb.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=a2345b18-13e4-4f60-b798-4b28587a18cb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Recently one of users of <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx">java
yield return annotation</a> has kindly informed us about some problem that happened
in his environment (see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f02%2f05%2fJavasYieldReturnAnnotationUpdate.aspx"> Java's
@Yield return annotation update</a>).
</p>
        <p>
Incidentally we have never noticed the problem earlier. Along with this issue we have
found that eclipse compiler has changed in the Indigo in a way that we had to recompile
the source. Well, that's a price you have to pay when you access internal API.
</p>
        <p>
Updated sources can be found at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip">Yield.zip</a>,
and compiled jars at <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar">Yield.jar</a> (pre-Indigo),
and <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.3.7.jar"> Yield.3.7.jar</a> (Indigo
and probably higher).
</p>
        <p>
See also: 
</p>
        <p>
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx"> Yield
return feature in java</a>
          <br />
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f27%2fWhyYieldIteratorShouldBeCloseable.aspx"> Why
@Yield iterator should be Closeable</a>
          <br />
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f09%2f05%2fWhatYouCanDoWithJxom.aspx"> What
you can do with jxom.</a>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb" />
      </body>
      <title>Yield return update</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a2345b18-13e4-4f60-b798-4b28587a18cb.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/08/28/YieldReturnUpdate.aspx</link>
      <pubDate>Sun, 28 Aug 2011 19:11:45 GMT</pubDate>
      <description>&lt;p&gt;
Recently one of users of &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx"&gt;java
yield return annotation&lt;/a&gt; has kindly informed us about some problem that happened
in his environment (see &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f02%2f05%2fJavasYieldReturnAnnotationUpdate.aspx"&gt; Java&amp;#39;s
@Yield return annotation update&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
Incidentally we have never noticed the problem earlier. Along with this issue we have
found that eclipse compiler has changed in the Indigo in a way that we had to recompile
the source. Well, that&amp;#39;s a price you have to pay when you access internal API.
&lt;/p&gt;
&lt;p&gt;
Updated sources can be found at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"&gt;Yield.zip&lt;/a&gt;,
and compiled jars at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar"&gt;Yield.jar&lt;/a&gt; (pre-Indigo),
and &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.3.7.jar"&gt; Yield.3.7.jar&lt;/a&gt; (Indigo
and probably higher).
&lt;/p&gt;
&lt;p&gt;
See also: 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx"&gt; Yield
return feature in java&lt;/a&gt;
&lt;br /&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f27%2fWhyYieldIteratorShouldBeCloseable.aspx"&gt; Why
@Yield iterator should be Closeable&lt;/a&gt;
&lt;br /&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f09%2f05%2fWhatYouCanDoWithJxom.aspx"&gt; What
you can do with jxom.&lt;/a&gt; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a2345b18-13e4-4f60-b798-4b28587a18cb" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,a2345b18-13e4-4f60-b798-4b28587a18cb.aspx</comments>
      <category>Announce</category>
      <category>Java</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,c28b2736-46cd-43c4-b69a-a9c6dc0eb43d.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,c28b2736-46cd-43c4-b69a-a9c6dc0eb43d.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
There is a problem with XML serialization of <code>BigDecimal</code> values, as we've
written in one of our previous articles <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2010%2f09%2f05%2fBigDecimalJAXBPotentialInteroperabilityProblems.aspx">"BigDecimal
+ JAXB =&gt; potential interoperability problems"</a>. And now we ran into issue with
serialization of <code>double</code> / <code>Double</code> values. All such values,
except zero, serialize in scientific format, even a value contains only integer part.
For example, <b>12</b> will be serialized as <b>1.2E+1</b>. Actually this is not contradicts
with <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;url=http%3a%2f%2fwww.w3.org%2fTR%2f2001%2fREC-xmlschema-2-20010502%2f%23double" target="_blank">XML
schema definitions</a>.
</p>
        <p>
But what could be done, if you want to send/receive double and/or decimal values in
plain format. For example you want serialize a <code>double</code> / <code>BigDecimal</code> value <b>314.15926</b> in
XML as is. In this case you ought to use <code><a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;url=http%3a%2f%2fdownload.oracle.com%2fjavase%2f6%2fdocs%2fapi%2fjavax%2fxml%2fbind%2fannotation%2fadapters%2fXmlAdapter.html" target="_blank">javax.xml.bind.annotation.adapters.XmlAdapter</a></code>. 
</p>
        <p>
In order to solve this task we've created two descendants of XmlAdapter (the first
for <code>double</code> / <code>Double</code> and the second for <code>BigDecimal</code>), <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fNumericAdapters.zip" target="_blank">click
here to download the sources</a>.
</p>
        <p>
Applying these classes on properties or package level you may manage XML serialization
of numeric fields in your classes.
</p>
        <p>
See <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;url=http%3a%2f%2fstackoverflow.com%2fquestions%2f4148192%2fjaxb-how-to-customize-xml-serialization-of-double-fields" traget="_blank">this
article</a> for tips how to use custom XML serialization. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d" />
      </body>
      <title>XML serialization of numbers in Java.</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,c28b2736-46cd-43c4-b69a-a9c6dc0eb43d.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/06/16/XMLSerializationOfNumbersInJava.aspx</link>
      <pubDate>Thu, 16 Jun 2011 22:14:36 GMT</pubDate>
      <description>&lt;p&gt;
There is a problem with XML serialization of &lt;code&gt;BigDecimal&lt;/code&gt; values, as we've
written in one of our previous articles &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2010%2f09%2f05%2fBigDecimalJAXBPotentialInteroperabilityProblems.aspx"&gt;"BigDecimal
+ JAXB =&gt; potential interoperability problems"&lt;/a&gt;. And now we ran into issue with
serialization of &lt;code&gt;double&lt;/code&gt; / &lt;code&gt;Double&lt;/code&gt; values. All such values,
except zero, serialize in scientific format, even a value contains only integer part.
For example, &lt;b&gt;12&lt;/b&gt; will be serialized as &lt;b&gt;1.2E+1&lt;/b&gt;. Actually this is not contradicts
with &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;amp;url=http%3a%2f%2fwww.w3.org%2fTR%2f2001%2fREC-xmlschema-2-20010502%2f%23double" target="_blank"&gt;XML
schema definitions&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
But what could be done, if you want to send/receive double and/or decimal values in
plain format. For example you want serialize a &lt;code&gt;double&lt;/code&gt; / &lt;code&gt;BigDecimal&lt;/code&gt; value &lt;b&gt;314.15926&lt;/b&gt; in
XML as is. In this case you ought to use &lt;code&gt;&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;amp;url=http%3a%2f%2fdownload.oracle.com%2fjavase%2f6%2fdocs%2fapi%2fjavax%2fxml%2fbind%2fannotation%2fadapters%2fXmlAdapter.html" target="_blank"&gt;javax.xml.bind.annotation.adapters.XmlAdapter&lt;/a&gt;&lt;/code&gt;. 
&lt;/p&gt;
&lt;p&gt;
In order to solve this task we've created two descendants of XmlAdapter (the first
for &lt;code&gt;double&lt;/code&gt; / &lt;code&gt;Double&lt;/code&gt; and the second for &lt;code&gt;BigDecimal&lt;/code&gt;), &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fNumericAdapters.zip" target="_blank"&gt;click
here to download the sources&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Applying these classes on properties or package level you may manage XML serialization
of numeric fields in your classes.
&lt;/p&gt;
&lt;p&gt;
See &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d&amp;amp;url=http%3a%2f%2fstackoverflow.com%2fquestions%2f4148192%2fjaxb-how-to-customize-xml-serialization-of-double-fields" traget="_blank"&gt;this
article&lt;/a&gt; for tips how to use custom XML serialization. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=c28b2736-46cd-43c4-b69a-a9c6dc0eb43d" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,c28b2736-46cd-43c4-b69a-a9c6dc0eb43d.aspx</comments>
      <category>Java</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=c8b061fa-7471-4601-8b19-2c5bf55ce9ea</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,c8b061fa-7471-4601-8b19-2c5bf55ce9ea.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,c8b061fa-7471-4601-8b19-2c5bf55ce9ea.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=c8b061fa-7471-4601-8b19-2c5bf55ce9ea</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We did not update <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"> languages-xom</a> already
for many monthes but now we have found a severe bug in the jxom's algorithm for eliminating
unreachable code. The marked line were considered as unreachable:
</p>
        <p style="padding-left: 1em">
          <code>check:<br />
if (condition)<br />
{<br />
break check;<br />
}<br />
else<br />
{<br />
return;<br />
}<br /><br />
// due to bug the following was considered unreachable<br /><span style="color: red; font-weight: bold">expression;</span></code>
        </p>
        <p>
Bug is fixed.
</p>
        <p>
Current update contains other cosmetic fixes. 
</p>
        <p>
Please download xslt sources from <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip">languages-xom.zip</a>.
</p>
        <p>
Summary
</p>
        <p>
Languages XOM is a set of xml schemas and xslt stylesheets that allows:
</p>
        <ul>
          <li>
to define programs in xml form;</li>
          <li>
to perform transformations over code in xml form;</li>
          <li>
to generate sources.</li>
        </ul>
        <p>
Languages XOM includes:
</p>
        <ul>
          <li>
jxom - Java Xml Object model;</li>
          <li>
csharpxom - C# Xml Object Model;</li>
          <li>
cobolxom - COBOL Xml Object Model;</li>
          <li>
sqlxom - SQL Xml Object Model (including several sql dialects);</li>
          <li>
aspx - ASP.NET Object Model;</li>
        </ul>
        <p>
A proprietary part of languages XOM also includes XML Object Model for a language
named Cool:GEN. In fact the original purpose for this API was a generation of java/C#/COBOL
from Cool:GEN. For more details about Cool:GEN conversion please see <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea&amp;url=http%3a%2f%2fwww.bphx.com%2fen%2fSolutions%2fApplicationModernization%2fPages%2fCooLGen.aspx"> here</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea" />
      </body>
      <title>Languages-XOM update</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,c8b061fa-7471-4601-8b19-2c5bf55ce9ea.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/05/26/LanguagesXOMUpdate.aspx</link>
      <pubDate>Thu, 26 May 2011 05:15:11 GMT</pubDate>
      <description>&lt;p&gt;
We did not update &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"&gt; languages-xom&lt;/a&gt; already
for many monthes but now we have found a severe bug in the jxom's algorithm for eliminating
unreachable code. The marked line were considered as unreachable:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;check:&lt;br /&gt;
if (condition)&lt;br /&gt;
{&lt;br /&gt;
break check;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
return;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// due to bug the following was considered unreachable&lt;br /&gt;
&lt;span style="color: red; font-weight: bold"&gt;expression;&lt;/span&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
Bug is fixed.
&lt;/p&gt;
&lt;p&gt;
Current update contains other cosmetic fixes. 
&lt;/p&gt;
&lt;p&gt;
Please download xslt sources from &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"&gt;languages-xom.zip&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Summary
&lt;/p&gt;
&lt;p&gt;
Languages XOM is a set of xml schemas and xslt stylesheets that allows:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
to define programs in xml form;&lt;/li&gt;
&lt;li&gt;
to perform transformations over code in xml form;&lt;/li&gt;
&lt;li&gt;
to generate sources.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Languages XOM includes:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
jxom - Java Xml Object model;&lt;/li&gt;
&lt;li&gt;
csharpxom - C# Xml Object Model;&lt;/li&gt;
&lt;li&gt;
cobolxom - COBOL Xml Object Model;&lt;/li&gt;
&lt;li&gt;
sqlxom - SQL Xml Object Model (including several sql dialects);&lt;/li&gt;
&lt;li&gt;
aspx - ASP.NET Object Model;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
A proprietary part of languages XOM also includes XML Object Model for a language
named Cool:GEN. In fact the original purpose for this API was a generation of java/C#/COBOL
from Cool:GEN. For more details about Cool:GEN conversion please see &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea&amp;amp;url=http%3a%2f%2fwww.bphx.com%2fen%2fSolutions%2fApplicationModernization%2fPages%2fCooLGen.aspx"&gt; here&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=c8b061fa-7471-4601-8b19-2c5bf55ce9ea" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,c8b061fa-7471-4601-8b19-2c5bf55ce9ea.aspx</comments>
      <category>Announce</category>
      <category>Java</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=b94eae0f-f7dc-4234-92cd-7ad127a20963</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b94eae0f-f7dc-4234-92cd-7ad127a20963.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,b94eae0f-f7dc-4234-92cd-7ad127a20963.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=b94eae0f-f7dc-4234-92cd-7ad127a20963</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As you may know, JAX-WS uses <code>javax.xml.datatype.XMLGregorianCalendar</code> abstract
class in order to present date/time data type fields. We have used this class rather
long time in happy ignorance without of any problem. Suddenly, few days ago, we ran
into a weird bug of its Sun’s implementation (<code>com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl</code>).
The bug appears whenever we try to convert an <code>XMLGregorianCalendar</code> instance
to a <code>java.util.GregorianCalendar</code> using <code>toGregorianCalendar()</code> method.
I’ve written a simple JUnit test in order to demonstrate this bug:
</p>
        <pre>
  @Test
  public void testXMLGregorianCalendar()
    throws Exception
  {    
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    XMLGregorianCalendar calendar = 
      javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar();

    calendar.setDay(1);
    calendar.setMonth(1);
    calendar.setYear(1);
    
    System.out.println("1: " + calendar.toString());

    System.out.println("2: " +
      formatter.format(calendar.toGregorianCalendar().getTime()));
    
    GregorianCalendar cal = new GregorianCalendar(
      calendar.getYear(), 
      calendar.getMonth() - 1, 
      calendar.getDay());
    
    cal.clear(Calendar.AM_PM);
    cal.clear(Calendar.HOUR_OF_DAY);
    cal.clear(Calendar.HOUR);
    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MILLISECOND);

    System.out.println("3: " + formatter.format(cal.getTime()));
    
    /*
     * Output:
     * 
     * 1: 0001-01-01
     * 2: 0001-01-03 00:00:00
     * 3: 0001-01-01 00:00:00
     */
  }
</pre>
        <p>
As you see, the date <b>0001-01-01</b> is transformed to <b>0001-01-03</b> after call
of <code>toGregorianCalendar()</code> method (see output 2). 
</p>
        <p>
Moreover, if we’ll serialize this <code>XMLGregorianCalendar</code> instance to XML
we’ll see it as <b>0001-01-01+02:00</b> which is rather weird and could be potential
problem for interoperability between Java and other platforms. 
</p>
        <p>
          <b>Conclusion:</b> in order to convert <code>XMLGregorianCalendar</code> value to <code>GregorianCalendar</code> do
the following. Create a new instance of <code>GregorianCalendar</code> and just set
the corresponding fields with values from <code>XMLGregorianCalendar</code> instance. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b94eae0f-f7dc-4234-92cd-7ad127a20963" />
      </body>
      <title>A bug in XMLGregorianCalendar implementation.</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,b94eae0f-f7dc-4234-92cd-7ad127a20963.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/05/04/ABugInXMLGregorianCalendarImplementation.aspx</link>
      <pubDate>Wed, 04 May 2011 09:19:52 GMT</pubDate>
      <description>&lt;p&gt;
As you may know, JAX-WS uses &lt;code&gt;javax.xml.datatype.XMLGregorianCalendar&lt;/code&gt; abstract
class in order to present date/time data type fields. We have used this class rather
long time in happy ignorance without of any problem. Suddenly, few days ago, we ran
into a weird bug of its Sun’s implementation (&lt;code&gt;com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl&lt;/code&gt;).
The bug appears whenever we try to convert an &lt;code&gt;XMLGregorianCalendar&lt;/code&gt; instance
to a &lt;code&gt;java.util.GregorianCalendar&lt;/code&gt; using &lt;code&gt;toGregorianCalendar()&lt;/code&gt; method.
I’ve written a simple JUnit test in order to demonstrate this bug:
&lt;/p&gt;
&lt;pre&gt;
  @Test
  public void testXMLGregorianCalendar()
    throws Exception
  {    
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    XMLGregorianCalendar calendar = 
      javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar();

    calendar.setDay(1);
    calendar.setMonth(1);
    calendar.setYear(1);
    
    System.out.println("1: " + calendar.toString());

    System.out.println("2: " +
      formatter.format(calendar.toGregorianCalendar().getTime()));
    
    GregorianCalendar cal = new GregorianCalendar(
      calendar.getYear(), 
      calendar.getMonth() - 1, 
      calendar.getDay());
    
    cal.clear(Calendar.AM_PM);
    cal.clear(Calendar.HOUR_OF_DAY);
    cal.clear(Calendar.HOUR);
    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MILLISECOND);

    System.out.println("3: " + formatter.format(cal.getTime()));
    
    /*
     * Output:
     * 
     * 1: 0001-01-01
     * 2: 0001-01-03 00:00:00
     * 3: 0001-01-01 00:00:00
     */
  }
&lt;/pre&gt;
&lt;p&gt;
As you see, the date &lt;b&gt;0001-01-01&lt;/b&gt; is transformed to &lt;b&gt;0001-01-03&lt;/b&gt; after call
of &lt;code&gt;toGregorianCalendar()&lt;/code&gt; method (see output 2). 
&lt;/p&gt;
&lt;p&gt;
Moreover, if we’ll serialize this &lt;code&gt;XMLGregorianCalendar&lt;/code&gt; instance to XML
we’ll see it as &lt;b&gt;0001-01-01+02:00&lt;/b&gt; which is rather weird and could be potential
problem for interoperability between Java and other platforms. 
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Conclusion:&lt;/b&gt; in order to convert &lt;code&gt;XMLGregorianCalendar&lt;/code&gt; value to &lt;code&gt;GregorianCalendar&lt;/code&gt; do
the following. Create a new instance of &lt;code&gt;GregorianCalendar&lt;/code&gt; and just set
the corresponding fields with values from &lt;code&gt;XMLGregorianCalendar&lt;/code&gt; instance. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=b94eae0f-f7dc-4234-92cd-7ad127a20963" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,b94eae0f-f7dc-4234-92cd-7ad127a20963.aspx</comments>
      <category>Java</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=0c95c9de-19dd-4ba3-8923-639928f30895</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,0c95c9de-19dd-4ba3-8923-639928f30895.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,0c95c9de-19dd-4ba3-8923-639928f30895.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=0c95c9de-19dd-4ba3-8923-639928f30895</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A search "java web service session object" has reached our site.
</p>
        <p>
Unfortunately, we cannot help to the original searcher but a next one might find this
info usefull.
</p>
        <p>
To get http session in the web service you should add a field to your class that will
be populated with request context.
</p>
        <p style="padding-left: 1em">
          <code> @WebService<br />
public class MyService<br />
{<br />
  @WebMethod<br />
  public int method(String value)<br />
  {<br />
    MessageContext messageContext = context.getMessageContext();<br />
    HttpServletRequest request = 
<br />
      (HttpServletRequest)messageContext.get(MessageContext.SERVLET_REQUEST);<br />
    HttpSession session = request.getSession();<br /><br />
    // go ahead.<br />
  }<br /><br />
  // A web service context.<br />
  <b>@Resource<br />
  private WebServiceContext context;</b><br />
}</code>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=0c95c9de-19dd-4ba3-8923-639928f30895" />
      </body>
      <title>java web service session object (www.bing.com)</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,0c95c9de-19dd-4ba3-8923-639928f30895.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/02/23/javaWebServiceSessionObjectWwwbingcom.aspx</link>
      <pubDate>Wed, 23 Feb 2011 11:33:37 GMT</pubDate>
      <description>&lt;p&gt;
A search &amp;quot;java web service session object&amp;quot; has reached our site.
&lt;/p&gt;
&lt;p&gt;
Unfortunately, we cannot help to the original searcher but a next one might find this
info usefull.
&lt;/p&gt;
&lt;p&gt;
To get http session in the web service you should add a field to your class that will
be populated with request context.
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; @WebService&lt;br /&gt;
public class MyService&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; @WebMethod&lt;br /&gt;
&amp;nbsp; public int method(String value)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; MessageContext messageContext = context.getMessageContext();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpServletRequest request = 
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (HttpServletRequest)messageContext.get(MessageContext.SERVLET_REQUEST);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpSession session = request.getSession();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // go ahead.&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; // A web service context.&lt;br /&gt;
&amp;nbsp; &lt;b&gt;@Resource&lt;br /&gt;
&amp;nbsp; private WebServiceContext context;&lt;/b&gt;
&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=0c95c9de-19dd-4ba3-8923-639928f30895" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,0c95c9de-19dd-4ba3-8923-639928f30895.aspx</comments>
      <category>Java</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=d56b2e61-6762-4f44-8058-12d5625658d8</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,d56b2e61-6762-4f44-8058-12d5625658d8.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,d56b2e61-6762-4f44-8058-12d5625658d8.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=d56b2e61-6762-4f44-8058-12d5625658d8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last few days we were testing Java web-applications that expose web-services. During
these tests we've found few interesting features.
</p>
        <p>
The first feature allows to retrieve info about all endpoints supported by the web-application
on GET request. The feature works at least for <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=d56b2e61-6762-4f44-8058-12d5625658d8&amp;url=http%3a%2f%2fjax-ws.java.net%2f2.1.7%2f" target="_blank">Metro
that implements JAX-WS API v2.x</a>. In order to get such info, a client sends any
endpoint's URL to the server. The result is an HTML page with a table. Each row of
such table contains an endpoint's data for each supported web-service method. This
feature may be used as a web-services discovery mechanism.
</p>
        <p>
The second feature is bad rather than good. JAX-WS API supposes that a developer annotates
classes and methods that he/she wants to expose as web-services. Then, an implementation
generates additional layer-bridge between developer's code and API that does all routine
work behind the scene. May be that was a good idea, but Metro's implementation is
imperfect. Metro dynamically generates such classes at run-time when a web-application
starts. Moreover, Metro does such generation for all classes at once. So, in our case,
when the generated web-based application contains dozens or even hundreds of web-services,
the application's startup takes a lot of time.
</p>
        <p>
Probably, Metro developers didn't want to deal with implementation of lazy algorithms,
when a web-service is generated and cached on demand. We hope this issue will be solved
in next releases.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=d56b2e61-6762-4f44-8058-12d5625658d8" />
      </body>
      <title>Good and bad things in JAX-WS implementation.</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,d56b2e61-6762-4f44-8058-12d5625658d8.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/02/20/GoodAndBadThingsInJAXWSImplementation.aspx</link>
      <pubDate>Sun, 20 Feb 2011 10:20:12 GMT</pubDate>
      <description>&lt;p&gt;
Last few days we were testing Java web-applications that expose web-services. During
these tests we've found few interesting features.
&lt;/p&gt;
&lt;p&gt;
The first feature allows to retrieve info about all endpoints supported by the web-application
on GET request. The feature works at least for &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=d56b2e61-6762-4f44-8058-12d5625658d8&amp;amp;url=http%3a%2f%2fjax-ws.java.net%2f2.1.7%2f" target=_blank&gt;Metro
that implements JAX-WS API v2.x&lt;/a&gt;. In order to get such info, a client sends any
endpoint's URL to the server. The result is an HTML page with a table. Each row of
such table contains an endpoint's data for each supported web-service method. This
feature may be used as a web-services discovery mechanism.
&lt;/p&gt;
&lt;p&gt;
The second feature is bad rather than good. JAX-WS API supposes that a developer annotates
classes and methods that he/she wants to expose as web-services. Then, an implementation
generates additional layer-bridge between developer's code and API that does all routine
work behind the scene. May be that was a good idea, but Metro's implementation is
imperfect. Metro dynamically generates such classes at run-time when a web-application
starts. Moreover, Metro does such generation for all classes at once. So, in our case,
when the generated web-based application contains dozens or even hundreds of web-services,
the application's startup takes a lot of time.
&lt;/p&gt;
&lt;p&gt;
Probably, Metro developers didn't want to deal with implementation of lazy algorithms,
when a web-service is generated and cached on demand. We hope this issue will be solved
in next releases.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=d56b2e61-6762-4f44-8058-12d5625658d8" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,d56b2e61-6762-4f44-8058-12d5625658d8.aspx</comments>
      <category>Java</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A while ago we have created <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f12%2f04%2fASimpleCacheForJavaApplication.aspx"> a
simple cache for Java application</a>. It was modelled like a <code>Map&lt;K, V&gt;</code>:
it cached values for keys.
</p>
        <p>
Use cases were:
</p>
        <p style="padding-left: 1em">
          <code>Cache&lt;String, Object&gt; cache = new Cache&lt;String, Object&gt;(); 
<br />
...<br />
instance = cache.get("key");<br />
cache.put("key", instance);</code>
        </p>
        <p>
But now we thought of different implementation like a <code>WeakReference&lt;V&gt;</code> and
with map access as additional utility methods.
</p>
        <p>
 Consider an examples:
</p>
        <p>
1. Free standing <code>CachedReference&lt;V&gt;</code> instance.
</p>
        <p style="padding-left: 1em">
          <code> CachedReference&lt;Data&gt; ref = new CachedReference&lt;Data&gt;(1000, true);<br />
...<br />
ref.set(data);<br />
...<br />
data = ref.get();</code>
        </p>
        <p>
2. Map of <code>CachedReference&lt;V&gt;</code> instances.
</p>
        <p style="padding-left: 1em">
          <code> ConcurrentHashMap&lt;String, CachedReference&lt;Data&gt;&gt; cache = 
<br />
  new ConcurrentHashMap&lt;String, CachedReference&lt;Data&gt;&gt;();<br /><br />
CachedReference.put(cache, "key", data, 1000, true);<br />
...<br />
data = CachedReference.get(cache, "key");</code>
        </p>
        <p>
The first case is faster than original <code>Cache&lt;K, V&gt;</code> as it does not
use any hash map at all. The later case provides the same performance as <code>Cache&lt;K,
V&gt;</code> but gives a better control over the storage. Incidentally, <code>CachedReference&lt;V&gt;</code> is
more compact than <code>Cache&lt;K, V&gt;</code>.
</p>
        <p>
The new implementation is <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fCachedReference.java.txt">CachedReference.java</a>,
the old one <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fCache.java.txt">Cache.java</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96" />
      </body>
      <title>A simple cache for Java application #2</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/02/08/ASimpleCacheForJavaApplication2.aspx</link>
      <pubDate>Tue, 08 Feb 2011 15:20:29 GMT</pubDate>
      <description>&lt;p&gt;
A while ago we have created &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f12%2f04%2fASimpleCacheForJavaApplication.aspx"&gt; a
simple cache for Java application&lt;/a&gt;. It was modelled like a &lt;code&gt;Map&amp;lt;K, V&amp;gt;&lt;/code&gt;:
it cached values for keys.
&lt;/p&gt;
&lt;p&gt;
Use cases were:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;Cache&amp;lt;String, Object&amp;gt; cache = new Cache&amp;lt;String, Object&amp;gt;(); 
&lt;br /&gt;
...&lt;br /&gt;
instance = cache.get(&amp;quot;key&amp;quot;);&lt;br /&gt;
cache.put(&amp;quot;key&amp;quot;, instance);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
But now we thought of different implementation like a &lt;code&gt;WeakReference&amp;lt;V&amp;gt;&lt;/code&gt; and
with map access as additional utility methods.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;Consider an examples:
&lt;/p&gt;
&lt;p&gt;
1. Free standing &lt;code&gt;CachedReference&amp;lt;V&amp;gt;&lt;/code&gt; instance.
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; CachedReference&amp;lt;Data&amp;gt; ref = new CachedReference&amp;lt;Data&amp;gt;(1000, true);&lt;br /&gt;
...&lt;br /&gt;
ref.set(data);&lt;br /&gt;
...&lt;br /&gt;
data = ref.get();&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
2. Map of &lt;code&gt;CachedReference&amp;lt;V&amp;gt;&lt;/code&gt; instances.
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; ConcurrentHashMap&amp;lt;String, CachedReference&amp;lt;Data&amp;gt;&amp;gt; cache = 
&lt;br /&gt;
&amp;nbsp; new ConcurrentHashMap&amp;lt;String, CachedReference&amp;lt;Data&amp;gt;&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
CachedReference.put(cache, &amp;quot;key&amp;quot;, data, 1000, true);&lt;br /&gt;
...&lt;br /&gt;
data = CachedReference.get(cache, &amp;quot;key&amp;quot;);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The first case is faster than original &lt;code&gt;Cache&amp;lt;K, V&amp;gt;&lt;/code&gt; as it does not
use any hash map at all. The later case provides the same performance as &lt;code&gt;Cache&amp;lt;K,
V&amp;gt;&lt;/code&gt; but gives a better control over the storage. Incidentally, &lt;code&gt;CachedReference&amp;lt;V&amp;gt;&lt;/code&gt; is
more compact than &lt;code&gt;Cache&amp;lt;K, V&amp;gt;&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
The new implementation is &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fCachedReference.java.txt"&gt;CachedReference.java&lt;/a&gt;,
the old one &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fCache.java.txt"&gt;Cache.java&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,cdf6ed65-8c21-49ed-92ac-93d3d6a3ea96.aspx</comments>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,5ed4531a-dbbb-48b5-89d2-b58d1ed78df8.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,5ed4531a-dbbb-48b5-89d2-b58d1ed78df8.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We have updated <code>@Yield</code> annotation processor to support better debug info.
</p>
        <p>
Annotation processor can be downloaded from <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip">Yield.zip</a> or <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar">Yield.jar</a>.
</p>
        <p>
We also decided to consider jxom's state machine refactoring as obsolete as <code>@Yield</code> annotation
allows to achieve the same effect but with more clear code.
</p>
        <p>
JXOM can be downloaded from <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"> languages-xom.zip</a></p>
        <p>
See also: 
</p>
        <p>
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx">Yield
return feature in java</a>
          <br />
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f27%2fWhyYieldIteratorShouldBeCloseable.aspx">Why
@Yield iterator should be Closeable</a>
          <br />
          <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f09%2f05%2fWhatYouCanDoWithJxom.aspx">What
you can do with jxom.</a>
        </p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8" />
      </body>
      <title>Java's @Yield return annotation update</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,5ed4531a-dbbb-48b5-89d2-b58d1ed78df8.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/02/05/JavasYieldReturnAnnotationUpdate.aspx</link>
      <pubDate>Sat, 05 Feb 2011 21:12:05 GMT</pubDate>
      <description>&lt;p&gt;
We have updated &lt;code&gt;@Yield&lt;/code&gt; annotation processor to support better debug info.
&lt;/p&gt;
&lt;p&gt;
Annotation processor can be downloaded from &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"&gt;Yield.zip&lt;/a&gt; or &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar"&gt;Yield.jar&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
We also decided to consider jxom&amp;#39;s state machine refactoring as obsolete as &lt;code&gt;@Yield&lt;/code&gt; annotation
allows to achieve the same effect but with more clear code.
&lt;/p&gt;
&lt;p&gt;
JXOM can be downloaded from &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"&gt; languages-xom.zip&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
See also: 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx"&gt;Yield
return feature in java&lt;/a&gt;
&lt;br /&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f27%2fWhyYieldIteratorShouldBeCloseable.aspx"&gt;Why
@Yield iterator should be Closeable&lt;/a&gt;
&lt;br /&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f09%2f05%2fWhatYouCanDoWithJxom.aspx"&gt;What
you can do with jxom.&lt;/a&gt; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=5ed4531a-dbbb-48b5-89d2-b58d1ed78df8" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,5ed4531a-dbbb-48b5-89d2-b58d1ed78df8.aspx</comments>
      <category>Announce</category>
      <category>Java</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,1197c4e4-fb86-4007-8bf3-9d5bf7e79a52.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,1197c4e4-fb86-4007-8bf3-9d5bf7e79a52.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A method pattern we have suggested to use along with <code>@Yield</code> annotation
brought funny questions like: "why should I mark my method with <code>@Yield</code> annotation
at all?"
</p>
        <p>
Well, in many cases you may live with <code>ArrayList</code> populated with data,
and then to perform iteration. But in some cases this approach is not practical either
due to amount of data or due to the time required to get first item.
</p>
        <p>
In later case you usually want to build an iterator that calculates items on demand.
The <code>@Yield</code> annotation is designed as a marker of such methods. They are
refactored into state machines at compilation time, where each addition to a result
list is transformed into a new item yielded by the iterator.
</p>
        <p>
So, if you have decided to use <code>@Yield</code> annotation then at some point you
will ask yourself what happens with resources acquired during iteration. Will resources
be released if iteration is interrupted in the middle due to exception or a break
statement? 
</p>
        <p>
To address the problem yield iterator implements <code>Closeable</code> interface.
</p>
        <p>
This way when you call <code>close()</code> before iteration reached the end, the
state machine works as if break statement of the method body is injected after the
yield point. Thus all finally blocks of the original method are executed and resources
are released.
</p>
        <p>
Consider an example of data iterator:
</p>
        <p style="padding-left: 1em">
          <code> @Yield<br />
public Iterable&lt;Data&gt; getData(final Connection connection)<br />
throws Exception<br />
{<br />
ArrayList&lt;Data&gt; result = new ArrayList&lt;Data&gt;();<br /><br />
PreparedStatement statement = 
<br />
connection.prepareStatement("select key, value from table");<br /><br />
try<br />
{<br />
ResultSet resultSet = statement.executeQuery();<br /><br />
try<br />
{<br />
while(resultSet.next())<br />
{<br />
Data data = new Data();<br /><br />
data.key = resultSet.getInt(1);<br />
data.value = resultSet.getString(2);<br /><br />
result.add(data); // yield point<br />
}<br />
}<br />
finally<br />
{<br />
resultSet.close();<br />
}<br />
}<br />
finally<br />
{<br />
statement.close();<br />
}<br /><br />
return result;<br />
}<br /><br />
private static void close(Object value)<br />
throws IOException<br />
{<br />
if (value instanceof Closeable)<br />
{<br />
Closeable closeable = (Closeable)value;<br /><br />
closeable.close();<br />
}<br />
}<br /><br />
public void daoAction(Connection connection)<br />
throws Exception<br />
{<br />
Iterable&lt;Data&gt; items = getData(connection);<br /><br />
try<br />
{<br />
for(Data data: items)<br />
{<br />
// do something that potentially throws exception.<br />
}<br />
}<br />
finally<br />
{<br />
close(items);<br />
}<br />
} </code>
        </p>
        <p>
          <code>getData()</code> iterates over sql data. During the lifecycle it creates and
releases <code>PreparedStatement</code> and <code>ResultSet</code>.
</p>
        <p>
          <code>daoAction()</code> iterates over results provided by <code>getData()</code> and
performs some actions that potentially throw an exception. The goal of <code>close()</code> is
to release opened sql resources in case of such an exception.
</p>
        <p>
Here you can inspect how state machine is implemented for such a method:
</p>
        <p style="padding-left: 1em">
          <code>@Yield()<br />
public static Iterable&lt;Data&gt; getData(final Connection connection)<br />
throws Exception 
<br />
{<br />
assert (java.util.ArrayList&lt;Data&gt;)(ArrayList&lt;Data&gt;)null == null;<br /><br />
class $state implements java.lang.Iterable&lt;Data&gt;, java.util.Iterator&lt;Data&gt;,
java.io.Closeable 
<br />
{<br />
public java.util.Iterator&lt;Data&gt; iterator() {<br />
if ($state$id == 0) {<br />
$state$id = 1;<br /><br />
return this;<br />
} else return new $state();<br />
}<br /><br />
public boolean hasNext() {<br />
if (!$state$nextDefined) {<br />
$state$hasNext = $state$next();<br />
$state$nextDefined = true;<br />
}<br /><br />
return $state$hasNext;<br />
}<br /><br />
public Data next() {<br />
if (!hasNext()) throw new java.util.NoSuchElementException();<br /><br />
$state$nextDefined = false;<br /><br />
return $state$next;<br />
}<br /><br />
public void remove() {<br />
throw new java.lang.UnsupportedOperationException();<br />
}<br /><br />
public void close() {<br />
do switch ($state$id) {<br />
case 3: 
<br />
$state$id2 = 8;<br />
$state$id = 5;<br /><br />
continue;<br />
default: 
<br />
$state$id = 8;<br /><br />
continue;<br />
} while ($state$next());<br />
}<br /><br />
private boolean $state$next() {<br />
java.lang.Throwable $state$exception;<br /><br />
while (true) {<br />
try {<br />
switch ($state$id) {<br />
case 0: 
<br />
$state$id = 1;<br />
case 1: 
<br />
statement = connection.prepareStatement("select key, value from table");<br />
$state$exception1 = null;<br />
$state$id1 = 8;<br />
$state$id = 2;<br />
case 2: 
<br />
resultSet = statement.executeQuery();<br />
$state$exception2 = null;<br />
$state$id2 = 6;<br />
$state$id = 3;<br />
case 3: 
<br />
if (!resultSet.next()) {<br />
$state$id = 4;<br /><br />
continue;<br />
}<br /><br />
data = new Data();<br />
data.key = resultSet.getInt(1);<br />
data.value = resultSet.getString(2);<br />
$state$next = data;<br />
$state$id = 3;<br /><br />
return true;<br />
case 4: 
<br />
$state$id = 5;<br />
case 5: 
<br />
{<br />
resultSet.close();<br />
}<br /><br />
if ($state$exception2 != null) {<br />
$state$exception = $state$exception2;<br /><br />
break;<br />
}<br /><br />
if ($state$id2 &gt; 7) {<br />
$state$id1 = $state$id2;<br />
$state$id = 7;<br />
} else $state$id = $state$id2;<br /><br />
continue;<br />
case 6: 
<br />
$state$id = 7;<br />
case 7: 
<br />
{<br />
statement.close();<br />
}<br /><br />
if ($state$exception1 != null) {<br />
$state$exception = $state$exception1;<br /><br />
break;<br />
}<br /><br />
$state$id = $state$id1;<br /><br />
continue;<br />
case 8: 
<br />
default: 
<br />
return false;<br />
}<br />
} catch (java.lang.Throwable e) {<br />
$state$exception = e;<br />
}<br /><br />
switch ($state$id) {<br />
case 3: 
<br />
case 4: 
<br />
$state$exception2 = $state$exception;<br />
$state$id = 5;<br /><br />
continue;<br />
case 2: 
<br />
case 5: 
<br />
case 6: 
<br />
$state$exception1 = $state$exception;<br />
$state$id = 7;<br /><br />
continue;<br />
default: 
<br />
$state$id = 8;<br /><br />
java.util.ConcurrentModificationException ce = new java.util.ConcurrentModificationException();<br /><br />
ce.initCause($state$exception);<br /><br />
throw ce;<br />
}<br />
}<br />
}<br /><br />
private PreparedStatement statement;<br />
private ResultSet resultSet;<br />
private Data data;<br />
private int $state$id;<br />
private boolean $state$hasNext;<br />
private boolean $state$nextDefined;<br />
private Data $state$next;<br />
private java.lang.Throwable $state$exception1;<br />
private int $state$id1;<br />
private java.lang.Throwable $state$exception2;<br />
private int $state$id2;<br />
}<br /><br />
return new $state();<br />
} </code>
        </p>
        <p>
Now, you can estimate for what it worth to write an algorithm as a sound state machine
comparing to the conventional implementation.
</p>
        <p>
Yield annotation processor can be downloaded from <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip">Yield.zip</a> or <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar">Yield.jar</a></p>
        <p>
See also <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx">Yield
return feature in java</a>.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52" />
      </body>
      <title>Why @Yield iterator should be Closeable</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,1197c4e4-fb86-4007-8bf3-9d5bf7e79a52.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/01/27/WhyYieldIteratorShouldBeCloseable.aspx</link>
      <pubDate>Thu, 27 Jan 2011 10:33:54 GMT</pubDate>
      <description>  &lt;p&gt;
A method pattern we have suggested to use along with &lt;code&gt;@Yield&lt;/code&gt; annotation
brought funny questions like: &amp;quot;why should I mark my method with &lt;code&gt;@Yield&lt;/code&gt; annotation
at all?&amp;quot;
&lt;/p&gt;
&lt;p&gt;
Well, in many cases you may live with &lt;code&gt;ArrayList&lt;/code&gt; populated with data,
and then to perform iteration. But in some cases this approach is not practical either
due to amount of data or due to the time required to get first item.
&lt;/p&gt;
&lt;p&gt;
In later case you usually want to build an iterator that calculates items on demand.
The &lt;code&gt;@Yield&lt;/code&gt; annotation is designed as a marker of such methods. They are
refactored into state machines at compilation time, where each addition to a result
list is transformed into a new item yielded by the iterator.
&lt;/p&gt;
&lt;p&gt;
So, if you have decided to use &lt;code&gt;@Yield&lt;/code&gt; annotation then at some point you
will ask yourself what happens with resources acquired during iteration. Will resources
be released if iteration is interrupted in the middle due to exception or a break
statement? 
&lt;/p&gt;
&lt;p&gt;
To address the problem yield iterator implements &lt;code&gt;Closeable&lt;/code&gt; interface.
&lt;/p&gt;
&lt;p&gt;
This way when you call &lt;code&gt;close()&lt;/code&gt; before iteration reached the end, the
state machine works as if break statement of the method body is injected after the
yield point. Thus all finally blocks of the original method are executed and resources
are released.
&lt;/p&gt;
&lt;p&gt;
Consider an example of data iterator:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; @Yield&lt;br /&gt;
public Iterable&amp;lt;Data&amp;gt; getData(final Connection connection)&lt;br /&gt;
throws Exception&lt;br /&gt;
{&lt;br /&gt;
ArrayList&amp;lt;Data&amp;gt; result = new ArrayList&amp;lt;Data&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
PreparedStatement statement = 
&lt;br /&gt;
connection.prepareStatement("select key, value from table");&lt;br /&gt;
&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
ResultSet resultSet = statement.executeQuery();&lt;br /&gt;
&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
while(resultSet.next())&lt;br /&gt;
{&lt;br /&gt;
Data data = new Data();&lt;br /&gt;
&lt;br /&gt;
data.key = resultSet.getInt(1);&lt;br /&gt;
data.value = resultSet.getString(2);&lt;br /&gt;
&lt;br /&gt;
result.add(data); // yield point&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
resultSet.close();&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
statement.close();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return result;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private static void close(Object value)&lt;br /&gt;
throws IOException&lt;br /&gt;
{&lt;br /&gt;
if (value instanceof Closeable)&lt;br /&gt;
{&lt;br /&gt;
Closeable closeable = (Closeable)value;&lt;br /&gt;
&lt;br /&gt;
closeable.close();&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void daoAction(Connection connection)&lt;br /&gt;
throws Exception&lt;br /&gt;
{&lt;br /&gt;
Iterable&amp;lt;Data&amp;gt; items = getData(connection);&lt;br /&gt;
&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
for(Data data: items)&lt;br /&gt;
{&lt;br /&gt;
// do something that potentially throws exception.&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
close(items);&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;getData()&lt;/code&gt; iterates over sql data. During the lifecycle it creates and
releases &lt;code&gt;PreparedStatement&lt;/code&gt; and &lt;code&gt;ResultSet&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;daoAction()&lt;/code&gt; iterates over results provided by &lt;code&gt;getData()&lt;/code&gt; and
performs some actions that potentially throw an exception. The goal of &lt;code&gt;close()&lt;/code&gt; is
to release opened sql resources in case of such an exception.
&lt;/p&gt;
&lt;p&gt;
Here you can inspect how state machine is implemented for such a method:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;@Yield()&lt;br /&gt;
public static Iterable&amp;lt;Data&amp;gt; getData(final Connection connection)&lt;br /&gt;
throws Exception 
&lt;br /&gt;
{&lt;br /&gt;
assert (java.util.ArrayList&amp;lt;Data&amp;gt;)(ArrayList&amp;lt;Data&amp;gt;)null == null;&lt;br /&gt;
&lt;br /&gt;
class $state implements java.lang.Iterable&amp;lt;Data&amp;gt;, java.util.Iterator&amp;lt;Data&amp;gt;,
java.io.Closeable 
&lt;br /&gt;
{&lt;br /&gt;
public java.util.Iterator&amp;lt;Data&amp;gt; iterator() {&lt;br /&gt;
if ($state$id == 0) {&lt;br /&gt;
$state$id = 1;&lt;br /&gt;
&lt;br /&gt;
return this;&lt;br /&gt;
} else return new $state();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public boolean hasNext() {&lt;br /&gt;
if (!$state$nextDefined) {&lt;br /&gt;
$state$hasNext = $state$next();&lt;br /&gt;
$state$nextDefined = true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return $state$hasNext;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public Data next() {&lt;br /&gt;
if (!hasNext()) throw new java.util.NoSuchElementException();&lt;br /&gt;
&lt;br /&gt;
$state$nextDefined = false;&lt;br /&gt;
&lt;br /&gt;
return $state$next;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void remove() {&lt;br /&gt;
throw new java.lang.UnsupportedOperationException();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void close() {&lt;br /&gt;
do switch ($state$id) {&lt;br /&gt;
case 3: 
&lt;br /&gt;
$state$id2 = 8;&lt;br /&gt;
$state$id = 5;&lt;br /&gt;
&lt;br /&gt;
continue;&lt;br /&gt;
default: 
&lt;br /&gt;
$state$id = 8;&lt;br /&gt;
&lt;br /&gt;
continue;&lt;br /&gt;
} while ($state$next());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private boolean $state$next() {&lt;br /&gt;
java.lang.Throwable $state$exception;&lt;br /&gt;
&lt;br /&gt;
while (true) {&lt;br /&gt;
try {&lt;br /&gt;
switch ($state$id) {&lt;br /&gt;
case 0: 
&lt;br /&gt;
$state$id = 1;&lt;br /&gt;
case 1: 
&lt;br /&gt;
statement = connection.prepareStatement("select key, value from table");&lt;br /&gt;
$state$exception1 = null;&lt;br /&gt;
$state$id1 = 8;&lt;br /&gt;
$state$id = 2;&lt;br /&gt;
case 2: 
&lt;br /&gt;
resultSet = statement.executeQuery();&lt;br /&gt;
$state$exception2 = null;&lt;br /&gt;
$state$id2 = 6;&lt;br /&gt;
$state$id = 3;&lt;br /&gt;
case 3: 
&lt;br /&gt;
if (!resultSet.next()) {&lt;br /&gt;
$state$id = 4;&lt;br /&gt;
&lt;br /&gt;
continue;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
data = new Data();&lt;br /&gt;
data.key = resultSet.getInt(1);&lt;br /&gt;
data.value = resultSet.getString(2);&lt;br /&gt;
$state$next = data;&lt;br /&gt;
$state$id = 3;&lt;br /&gt;
&lt;br /&gt;
return true;&lt;br /&gt;
case 4: 
&lt;br /&gt;
$state$id = 5;&lt;br /&gt;
case 5: 
&lt;br /&gt;
{&lt;br /&gt;
resultSet.close();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ($state$exception2 != null) {&lt;br /&gt;
$state$exception = $state$exception2;&lt;br /&gt;
&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ($state$id2 &amp;gt; 7) {&lt;br /&gt;
$state$id1 = $state$id2;&lt;br /&gt;
$state$id = 7;&lt;br /&gt;
} else $state$id = $state$id2;&lt;br /&gt;
&lt;br /&gt;
continue;&lt;br /&gt;
case 6: 
&lt;br /&gt;
$state$id = 7;&lt;br /&gt;
case 7: 
&lt;br /&gt;
{&lt;br /&gt;
statement.close();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ($state$exception1 != null) {&lt;br /&gt;
$state$exception = $state$exception1;&lt;br /&gt;
&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$state$id = $state$id1;&lt;br /&gt;
&lt;br /&gt;
continue;&lt;br /&gt;
case 8: 
&lt;br /&gt;
default: 
&lt;br /&gt;
return false;&lt;br /&gt;
}&lt;br /&gt;
} catch (java.lang.Throwable e) {&lt;br /&gt;
$state$exception = e;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
switch ($state$id) {&lt;br /&gt;
case 3: 
&lt;br /&gt;
case 4: 
&lt;br /&gt;
$state$exception2 = $state$exception;&lt;br /&gt;
$state$id = 5;&lt;br /&gt;
&lt;br /&gt;
continue;&lt;br /&gt;
case 2: 
&lt;br /&gt;
case 5: 
&lt;br /&gt;
case 6: 
&lt;br /&gt;
$state$exception1 = $state$exception;&lt;br /&gt;
$state$id = 7;&lt;br /&gt;
&lt;br /&gt;
continue;&lt;br /&gt;
default: 
&lt;br /&gt;
$state$id = 8;&lt;br /&gt;
&lt;br /&gt;
java.util.ConcurrentModificationException ce = new java.util.ConcurrentModificationException();&lt;br /&gt;
&lt;br /&gt;
ce.initCause($state$exception);&lt;br /&gt;
&lt;br /&gt;
throw ce;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private PreparedStatement statement;&lt;br /&gt;
private ResultSet resultSet;&lt;br /&gt;
private Data data;&lt;br /&gt;
private int $state$id;&lt;br /&gt;
private boolean $state$hasNext;&lt;br /&gt;
private boolean $state$nextDefined;&lt;br /&gt;
private Data $state$next;&lt;br /&gt;
private java.lang.Throwable $state$exception1;&lt;br /&gt;
private int $state$id1;&lt;br /&gt;
private java.lang.Throwable $state$exception2;&lt;br /&gt;
private int $state$id2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return new $state();&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Now, you can estimate for what it worth to write an algorithm as a sound state machine
comparing to the conventional implementation.
&lt;/p&gt;
&lt;p&gt;
Yield annotation processor can be downloaded from &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"&gt;Yield.zip&lt;/a&gt; or &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar"&gt;Yield.jar&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
See also &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f24%2fYieldReturnFeatureInJava.aspx"&gt;Yield
return feature in java&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=1197c4e4-fb86-4007-8bf3-9d5bf7e79a52" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,1197c4e4-fb86-4007-8bf3-9d5bf7e79a52.aspx</comments>
      <category>Java</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=0525e835-90bf-4889-8706-6b0432624e57</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,0525e835-90bf-4889-8706-6b0432624e57.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,0525e835-90bf-4889-8706-6b0432624e57.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=0525e835-90bf-4889-8706-6b0432624e57</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We're happy to announce that we have implemented <code>@Yield</code> annotation
both in javac and in eclipse compilers.
</p>
        <p>
This way you get built-in IDE support for the feature!
</p>
        <p>
To download yield annotation processor please use the following link: <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip">Yield.zip</a></p>
        <p>
It contains both yield annotation processor, and a test project.
</p>
        <p>
If you do not want to compile the sources, you can download <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar">Yield.jar</a></p>
        <p>
        </p>
        <p>
We would like to reiterate on how <code>@Yield</code> annotation works:
</p>
        <ol>
          <li>
A developer defines a method that returns either <code>Iterator&lt;T&gt;</code> or <code>Iterable&lt;T&gt;</code> instance
and marks it with <code>@Yield</code> annotation.</li>
          <li>
A developer implements iteration logic following the pattern:<br /><ul><li>
declare a variable to accumulate results:<br /><code>ArrayList&lt;T&gt; items = new ArrayList&lt;T&gt;();</code></li><li>
use the following statement to add item to result:<br /><code>items.add(...);</code></li><li>
use<br /><code>return items;</code><br />
or<br /><code>return items.iterator();<br /></code>to return result;</li><li>
mark method's params, if any, as final.</li></ul></li>
          <li>
A devoloper ensures that yield annotation processor is available during compilation
(see details below).</li>
          <li>
            <code>YieldProcessor</code> rewrites method into a state machine at compilation time.</li>
        </ol>
        <p>
The following is an example of such a method:
</p>
        <p style="PADDING-LEFT: 1em">
          <code>@Yield<br />
public static Iterable&lt;Integer&gt; generate(final int from, final int to)<br />
{<br />
ArrayList&lt;Integer&gt; items = new ArrayList&lt;Integer&gt;();<br /><br />
for(int i = from; i &lt; to; ++i)<br />
{<br />
items.add(i);<br />
}<br /><br />
return items;<br />
}</code>
        </p>
        <p>
The use is like this:
</p>
        <p style="PADDING-LEFT: 1em">
          <code>for(int value: generate(7, 20))<br />
{<br />
System.out.println("generator: " + value);<br />
} </code>
        </p>
        <p>
Notice that method's implementation still will be correct in absence of <code> YieldProcessor</code>.
</p>
        <p>
Other important feature is that the state machine returned after the yield processor
is closeable.
</p>
        <p>
This means that if you're breaking the iteration before the end is reached you
can release resources acquired during the iteration.
</p>
        <p>
Consider the example where break exits iteration:
</p>
        <p style="PADDING-LEFT: 1em">
          <code>@Yield<br />
public static Iterable&lt;String&gt; resourceIteration()<br />
{<br />
ArrayList&lt;String&gt; items = new ArrayList&lt;String&gt;();<br /><br />
acquire();<br /><br />
try<br />
{<br />
for(int i = 0; i &lt; 100; ++i)<br />
{<br />
items.add(String.valueOf(i));<br />
}<br />
}<br />
finally<br />
{<br />
release();<br />
}<br /><br />
return items;<br />
}</code>
        </p>
        <p>
and the use
</p>
        <p style="PADDING-LEFT: 1em">
          <code>int i = 0;<br />
Iterable&lt;String&gt; iterator = resourceIteration();<br /><br />
try<br />
{<br />
for(String item: iterator)<br />
{<br />
System.out.println("item " + i + ":" + item);<br /><br />
if (i++ &gt; 30)<br />
{<br /><b>break</b>;<br />
}<br />
}<br />
}<br />
finally<br />
{<br />
close(iterator);<br />
}<br /><br />
...<br /><br />
private static &lt;T&gt; void close(T value)<br />
throws IOException<br />
{<br />
if (value instanceof Closeable)<br />
{<br />
Closeable closeable = (Closeable)value;<br /><br />
closeable.close();<br />
}<br />
} </code>
        </p>
        <p>
Close will execute all required finally blocks. This way resources will be released.
</p>
        <p>
        </p>
        <p>
To configure yield processor a developer needs to refer Yield.jar in build path, as
it contains <code>@Yield</code> annotation. For javac it's enough, as compiler
will find annotation processor automatically.
</p>
        <p>
Eclipse users need to open project properties and:
</p>
        <ul>
          <li>
go to the "Java Compiler"/"Annotation Processing" 
</li>
          <li>
mark "Enable project specific settings"</li>
          <li>
select "Java Compiler"/"Annotation Processing"/"Factory Path"</li>
          <li>
mark "Enable project specific settings"</li>
          <li>
add Yield.jar to the list of "plug-ins and JARs that contain annotation processors".</li>
        </ul>
        <p>
        </p>
        <p>
At the end we want to point that <code>@Yield</code> annotation is a syntactic suggar,
but it's important the way the foreach statement is important, as it helps to
write concise and an error free code.
</p>
        <p>
See also<br /><a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f11%2fYieldFeatureInJavaImplemented.aspx">Yield
feature in java implemented!</a><br /><a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2010%2f12%2f20%2fYieldFeatureInJava.aspx">Yield
feature in java</a></p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=0525e835-90bf-4889-8706-6b0432624e57" />
      </body>
      <title>Yield return feature in java</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,0525e835-90bf-4889-8706-6b0432624e57.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/01/24/YieldReturnFeatureInJava.aspx</link>
      <pubDate>Mon, 24 Jan 2011 10:23:53 GMT</pubDate>
      <description>&lt;p&gt;
We&amp;#39;re happy to announce that we have implemented &lt;code&gt;@Yield&lt;/code&gt; annotation
both in javac and in eclipse compilers.
&lt;/p&gt;
&lt;p&gt;
This way you get built-in IDE support for the feature!
&lt;/p&gt;
&lt;p&gt;
To download yield annotation processor please use the following link: &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"&gt;Yield.zip&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
It contains both yield annotation processor, and a test project.
&lt;/p&gt;
&lt;p&gt;
If you do not want to compile the sources, you can download &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.jar"&gt;Yield.jar&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
We would like to reiterate on how &lt;code&gt;@Yield&lt;/code&gt; annotation works:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
A developer defines a method that returns either &lt;code&gt;Iterator&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;Iterable&amp;lt;T&amp;gt;&lt;/code&gt; instance
and marks it with &lt;code&gt;@Yield&lt;/code&gt; annotation.&lt;/li&gt;
&lt;li&gt;
A developer implements iteration logic following the pattern:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;
declare a variable to accumulate results:&lt;br /&gt;
&lt;code&gt;ArrayList&amp;lt;T&amp;gt; items = new ArrayList&amp;lt;T&amp;gt;();&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
use the following statement to add item to result:&lt;br /&gt;
&lt;code&gt;items.add(...);&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
use&lt;br /&gt;
&lt;code&gt;return items;&lt;/code&gt; 
&lt;br /&gt;
or&lt;br /&gt;
&lt;code&gt;return items.iterator();&lt;br /&gt;
&lt;/code&gt;to return result;&lt;/li&gt;
&lt;li&gt;
mark method&amp;#39;s params, if any, as final.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
A devoloper ensures that yield annotation processor is available during compilation
(see details below).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;YieldProcessor&lt;/code&gt; rewrites method into a state machine at compilation time.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The following is an example of such a method:
&lt;/p&gt;
&lt;p style="PADDING-LEFT: 1em"&gt;
&lt;code&gt;@Yield&lt;br /&gt;
public static Iterable&amp;lt;Integer&amp;gt; generate(final int from, final int to)&lt;br /&gt;
{&lt;br /&gt;
ArrayList&amp;lt;Integer&amp;gt; items = new ArrayList&amp;lt;Integer&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
for(int i = from; i &amp;lt; to; ++i)&lt;br /&gt;
{&lt;br /&gt;
items.add(i);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return items;&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
The use is like this:
&lt;/p&gt;
&lt;p style="PADDING-LEFT: 1em"&gt;
&lt;code&gt;for(int value: generate(7, 20))&lt;br /&gt;
{&lt;br /&gt;
System.out.println(&amp;quot;generator: &amp;quot; + value);&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
Notice that method&amp;#39;s implementation still will be correct in absence of &lt;code&gt; YieldProcessor&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
Other important feature is that the state machine returned after the yield processor
is closeable.
&lt;/p&gt;
&lt;p&gt;
This means that if you&amp;#39;re breaking the iteration before the end is reached you
can release resources acquired during the iteration.
&lt;/p&gt;
&lt;p&gt;
Consider the example where break exits iteration:
&lt;/p&gt;
&lt;p style="PADDING-LEFT: 1em"&gt;
&lt;code&gt;@Yield&lt;br /&gt;
public static Iterable&amp;lt;String&amp;gt; resourceIteration()&lt;br /&gt;
{&lt;br /&gt;
ArrayList&amp;lt;String&amp;gt; items = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
acquire();&lt;br /&gt;
&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
for(int i = 0; i &amp;lt; 100; ++i)&lt;br /&gt;
{&lt;br /&gt;
items.add(String.valueOf(i));&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
release();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return items;&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
and the use
&lt;/p&gt;
&lt;p style="PADDING-LEFT: 1em"&gt;
&lt;code&gt;int i = 0;&lt;br /&gt;
Iterable&amp;lt;String&amp;gt; iterator = resourceIteration();&lt;br /&gt;
&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
for(String item: iterator)&lt;br /&gt;
{&lt;br /&gt;
System.out.println(&amp;quot;item &amp;quot; + i + &amp;quot;:&amp;quot; + item);&lt;br /&gt;
&lt;br /&gt;
if (i++ &amp;gt; 30)&lt;br /&gt;
{&lt;br /&gt;
&lt;b&gt;break&lt;/b&gt;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
finally&lt;br /&gt;
{&lt;br /&gt;
close(iterator);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
private static &amp;lt;T&amp;gt; void close(T value)&lt;br /&gt;
throws IOException&lt;br /&gt;
{&lt;br /&gt;
if (value instanceof Closeable)&lt;br /&gt;
{&lt;br /&gt;
Closeable closeable = (Closeable)value;&lt;br /&gt;
&lt;br /&gt;
closeable.close();&lt;br /&gt;
}&lt;br /&gt;
} &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
Close will execute all required finally blocks. This way resources will be released.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
To configure yield processor a developer needs to refer Yield.jar in build path, as
it contains &lt;code&gt;@Yield&lt;/code&gt; annotation. For javac it&amp;#39;s enough, as compiler
will find annotation processor automatically.
&lt;/p&gt;
&lt;p&gt;
Eclipse users need to open project properties and:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
go to the &amp;quot;Java Compiler&amp;quot;/&amp;quot;Annotation Processing&amp;quot; 
&lt;/li&gt;
&lt;li&gt;
mark &amp;quot;Enable project specific settings&amp;quot;&lt;/li&gt;
&lt;li&gt;
select &amp;quot;Java Compiler&amp;quot;/&amp;quot;Annotation Processing&amp;quot;/&amp;quot;Factory Path&amp;quot;&lt;/li&gt;
&lt;li&gt;
mark &amp;quot;Enable project specific settings&amp;quot;&lt;/li&gt;
&lt;li&gt;
add Yield.jar to the list of &amp;quot;plug-ins and JARs that contain annotation processors&amp;quot;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
At the end we want to point that &lt;code&gt;@Yield&lt;/code&gt; annotation is a syntactic suggar,
but it&amp;#39;s important the way the foreach statement is important, as it helps to
write concise and an error free code.
&lt;/p&gt;
&lt;p&gt;
See also&lt;br /&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2011%2f01%2f11%2fYieldFeatureInJavaImplemented.aspx"&gt;Yield
feature in java implemented!&lt;/a&gt;
&lt;br /&gt;
&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=0525e835-90bf-4889-8706-6b0432624e57&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2010%2f12%2f20%2fYieldFeatureInJava.aspx"&gt;Yield
feature in java&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=0525e835-90bf-4889-8706-6b0432624e57" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,0525e835-90bf-4889-8706-6b0432624e57.aspx</comments>
      <category>Announce</category>
      <category>Java</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=280d6e0d-1389-4566-9e0f-badef8530578</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,280d6e0d-1389-4566-9e0f-badef8530578.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,280d6e0d-1389-4566-9e0f-badef8530578.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=280d6e0d-1389-4566-9e0f-badef8530578</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For some reason we never knew about instance initializer in java; on the other hand
static initializer is well known.
</p>
        <p style="padding-left: 1em;">
          <code> class A<br />
{<br />
int x;<br />
static int y;<br /><br />
// This is an instance initializer.<br />
{<br />
x = 1;<br />
}<br /><br />
// This is a static initializer.<br />
static<br />
{<br />
y = 2;<br />
}<br />
}</code>
        </p>
        <p>
Worse, we have missed it in the java grammar when we were building jxom. This way
jxom was missing the feature.
</p>
        <p>
Today we fix the miss and introduce a schema element:
</p>
        <p style="padding-left: 1em">
          <code>&lt;class-initializer static="boolean"&gt;<br />
&lt;block&gt;<br />
...<br />
&lt;/block&gt;<br />
&lt;/class-initializer&gt;</code>
        </p>
        <p>
It superseeds:
</p>
        <p style="padding-left: 1em">
          <code>&lt;static&gt;<br />
&lt;block&gt;<br />
...<br />
&lt;/block&gt;<br />
&lt;/static&gt;</code>
        </p>
        <p>
that supported static initializers alone.
</p>
        <p>
Please update <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=280d6e0d-1389-4566-9e0f-badef8530578&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"> languages-xom
xslt stylesheets</a>.
</p>
        <p>
P.S. Out of curiosity, did you ever see any use of instance initializers?
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=280d6e0d-1389-4566-9e0f-badef8530578" />
      </body>
      <title>Java XML Object Model Update</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,280d6e0d-1389-4566-9e0f-badef8530578.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/01/14/JavaXMLObjectModelUpdate.aspx</link>
      <pubDate>Fri, 14 Jan 2011 21:29:04 GMT</pubDate>
      <description>&lt;p&gt;
For some reason we never knew about instance initializer in java; on the other hand
static initializer is well known.
&lt;/p&gt;
&lt;p style="padding-left: 1em;"&gt;
&lt;code&gt; class A&lt;br /&gt;
{&lt;br /&gt;
int x;&lt;br /&gt;
static int y;&lt;br /&gt;
&lt;br /&gt;
// This is an instance initializer.&lt;br /&gt;
{&lt;br /&gt;
x = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is a static initializer.&lt;br /&gt;
static&lt;br /&gt;
{&lt;br /&gt;
y = 2;&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Worse, we have missed it in the java grammar when we were building jxom. This way
jxom was missing the feature.
&lt;/p&gt;
&lt;p&gt;
Today we fix the miss and introduce a schema element:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;lt;class-initializer static=&amp;quot;boolean&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;block&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/block&amp;gt;&lt;br /&gt;
&amp;lt;/class-initializer&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
It superseeds:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;&amp;lt;static&amp;gt;&lt;br /&gt;
&amp;lt;block&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/block&amp;gt;&lt;br /&gt;
&amp;lt;/static&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
that supported static initializers alone.
&lt;/p&gt;
&lt;p&gt;
Please update &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=280d6e0d-1389-4566-9e0f-badef8530578&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2flanguages-xom.zip"&gt; languages-xom
xslt stylesheets&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
P.S. Out of curiosity, did you ever see any use of instance initializers?
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=280d6e0d-1389-4566-9e0f-badef8530578" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,280d6e0d-1389-4566-9e0f-badef8530578.aspx</comments>
      <category>Announce</category>
      <category>Java</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=ebd5185f-fc37-4e82-b2bb-006387635512</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,ebd5185f-fc37-4e82-b2bb-006387635512.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,ebd5185f-fc37-4e82-b2bb-006387635512.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=ebd5185f-fc37-4e82-b2bb-006387635512</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We could not stand the temptation to implement the <code>@Yield</code> annotation
that we described <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2010%2f12%2f20%2fYieldFeatureInJava.aspx">earlier</a>.
</p>
        <p>
Idea is rather clear but people are saying that it's not an easy task to update
the sources.
</p>
        <p>
They were right!
</p>
        <p>
Implementation has its price, as we were forced to access JDK's classes of javac
compiler. As result, at present, we don't support other compilers such as EclipseCompiler.
We shall look later what can be done in this area.
</p>
        <p>
At present, annotation processor works perfectly when you run javac either from the
command line, from ant, or from other build tool.
</p>
        <p>
Here is an example of how method is refactored:
</p>
        <p style="padding-left: 1em">
          <code>@Yield<br />
public static Iterable&lt;Long&gt; fibonachi()<br />
{<br />
ArrayList&lt;Long&gt; items = new ArrayList&lt;Long&gt;();<br /><br />
long Ti = 0;<br />
long Ti1 = 1;<br /><br />
while(true)<br />
{<br />
items.add(Ti);<br /><br />
long value = Ti + Ti1;<br /><br />
Ti = Ti1;<br />
Ti1 = value;<br />
}<br />
}</code>
        </p>
        <p>
And that's how we transform it:
</p>
        <p style="padding-left: 1em">
          <code>@Yield()<br />
public static Iterable&lt;Long&gt; fibonachi() {<br />
assert (java.util.ArrayList&lt;Long&gt;)(ArrayList&lt;Long&gt;)null == null : null;<br /><br />
class $state$ implements java.lang.Iterable&lt;Long&gt;, java.util.Iterator&lt;Long&gt;,
java.io.Closeable {<br /><br />
public java.util.Iterator&lt;Long&gt; iterator() {<br />
if ($state$id == 0) {<br />
$state$id = 1;<br />
return this;<br />
} else return new $state$();<br />
}<br /><br />
public boolean hasNext() {<br />
if (!$state$nextDefined) {<br />
$state$hasNext = $state$next();<br />
$state$nextDefined = true;<br />
}<br /><br />
return $state$hasNext;<br />
}<br /><br />
public Long next() {<br />
if (!hasNext()) throw new java.util.NoSuchElementException();<br /><br />
$state$nextDefined = false;<br /><br />
return $state$next;<br />
}<br /><br />
public void remove() {<br />
throw new java.lang.UnsupportedOperationException();<br />
}<br /><br />
public void close() {<br />
$state$id = 5;<br />
}<br /><br />
private boolean $state$next() {<br />
while (true) switch ($state$id) {<br />
case 0:<br />
$state$id = 1;<br />
case 1:<br />
Ti = 0;<br />
Ti1 = 1;<br />
case 2: 
<br />
if (!true) {<br />
$state$id = 4;<br />
break;<br />
}<br /><br />
$state$next = Ti;<br />
$state$id = 3;<br /><br />
return true;<br />
case 3: 
<br />
value = Ti + Ti1;<br />
Ti = Ti1;<br />
Ti1 = value;<br />
$state$id = 2;<br /><br />
break;<br />
case 4: 
<br />
case 5: 
<br />
default: 
<br />
$state$id = 5;<br /><br />
return false;<br />
}<br />
}<br /><br />
private long Ti;<br />
private long Ti1;<br />
private long value;<br />
private int $state$id;<br />
private boolean $state$hasNext;<br />
private boolean $state$nextDefined;<br />
private Long $state$next;<br />
}<br /><br />
return new $state$();<br />
}</code>
        </p>
        <p>
Formatting is automatic, sorry, but anyway it's for diagnostics only. You will
never see this code.
</p>
        <p>
It's iteresting to say that this implementation is very precisely mimics <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f09%2f05%2fWhatYouCanDoWithJxom.aspx"> xslt
state machine implementation</a> we have done back in 2008.
</p>
        <p>
You can <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512&amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"> download
YieldProcessor here</a>. We hope that someone will find our solution very interesting.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512" />
      </body>
      <title>Yield feature in java implemented!</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,ebd5185f-fc37-4e82-b2bb-006387635512.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2011/01/11/YieldFeatureInJavaImplemented.aspx</link>
      <pubDate>Tue, 11 Jan 2011 16:08:41 GMT</pubDate>
      <description>&lt;p&gt;
We could not stand the temptation to implement the &lt;code&gt;@Yield&lt;/code&gt; annotation
that we described &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2010%2f12%2f20%2fYieldFeatureInJava.aspx"&gt;earlier&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Idea is rather clear but people are saying that it&amp;#39;s not an easy task to update
the sources.
&lt;/p&gt;
&lt;p&gt;
They were right!
&lt;/p&gt;
&lt;p&gt;
Implementation has its price, as we were forced to access JDK&amp;#39;s classes of javac
compiler. As result, at present, we don&amp;#39;t support other compilers such as EclipseCompiler.
We shall look later what can be done in this area.
&lt;/p&gt;
&lt;p&gt;
At present, annotation processor works perfectly when you run javac either from the
command line, from ant, or from other build tool.
&lt;/p&gt;
&lt;p&gt;
Here is an example of how method is refactored:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;@Yield&lt;br /&gt;
public static Iterable&amp;lt;Long&amp;gt; fibonachi()&lt;br /&gt;
{&lt;br /&gt;
ArrayList&amp;lt;Long&amp;gt; items = new ArrayList&amp;lt;Long&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
long Ti = 0;&lt;br /&gt;
long Ti1 = 1;&lt;br /&gt;
&lt;br /&gt;
while(true)&lt;br /&gt;
{&lt;br /&gt;
items.add(Ti);&lt;br /&gt;
&lt;br /&gt;
long value = Ti + Ti1;&lt;br /&gt;
&lt;br /&gt;
Ti = Ti1;&lt;br /&gt;
Ti1 = value;&lt;br /&gt;
}&lt;br /&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
And that&amp;#39;s how we transform it:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;@Yield()&lt;br /&gt;
public static Iterable&amp;lt;Long&amp;gt; fibonachi() {&lt;br /&gt;
assert (java.util.ArrayList&amp;lt;Long&amp;gt;)(ArrayList&amp;lt;Long&amp;gt;)null == null : null;&lt;br /&gt;
&lt;br /&gt;
class $state$ implements java.lang.Iterable&amp;lt;Long&amp;gt;, java.util.Iterator&amp;lt;Long&amp;gt;,
java.io.Closeable {&lt;br /&gt;
&lt;br /&gt;
public java.util.Iterator&amp;lt;Long&amp;gt; iterator() {&lt;br /&gt;
if ($state$id == 0) {&lt;br /&gt;
$state$id = 1;&lt;br /&gt;
return this;&lt;br /&gt;
} else return new $state$();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public boolean hasNext() {&lt;br /&gt;
if (!$state$nextDefined) {&lt;br /&gt;
$state$hasNext = $state$next();&lt;br /&gt;
$state$nextDefined = true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return $state$hasNext;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public Long next() {&lt;br /&gt;
if (!hasNext()) throw new java.util.NoSuchElementException();&lt;br /&gt;
&lt;br /&gt;
$state$nextDefined = false;&lt;br /&gt;
&lt;br /&gt;
return $state$next;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void remove() {&lt;br /&gt;
throw new java.lang.UnsupportedOperationException();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void close() {&lt;br /&gt;
$state$id = 5;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private boolean $state$next() {&lt;br /&gt;
while (true) switch ($state$id) {&lt;br /&gt;
case 0:&lt;br /&gt;
$state$id = 1;&lt;br /&gt;
case 1:&lt;br /&gt;
Ti = 0;&lt;br /&gt;
Ti1 = 1;&lt;br /&gt;
case 2: 
&lt;br /&gt;
if (!true) {&lt;br /&gt;
$state$id = 4;&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$state$next = Ti;&lt;br /&gt;
$state$id = 3;&lt;br /&gt;
&lt;br /&gt;
return true;&lt;br /&gt;
case 3: 
&lt;br /&gt;
value = Ti + Ti1;&lt;br /&gt;
Ti = Ti1;&lt;br /&gt;
Ti1 = value;&lt;br /&gt;
$state$id = 2;&lt;br /&gt;
&lt;br /&gt;
break;&lt;br /&gt;
case 4: 
&lt;br /&gt;
case 5: 
&lt;br /&gt;
default: 
&lt;br /&gt;
$state$id = 5;&lt;br /&gt;
&lt;br /&gt;
return false;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
private long Ti;&lt;br /&gt;
private long Ti1;&lt;br /&gt;
private long value;&lt;br /&gt;
private int $state$id;&lt;br /&gt;
private boolean $state$hasNext;&lt;br /&gt;
private boolean $state$nextDefined;&lt;br /&gt;
private Long $state$next;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return new $state$();&lt;br /&gt;
}&lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
Formatting is automatic, sorry, but anyway it&amp;#39;s for diagnostics only. You will
never see this code.
&lt;/p&gt;
&lt;p&gt;
It&amp;#39;s iteresting to say that this implementation is very precisely mimics &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f09%2f05%2fWhatYouCanDoWithJxom.aspx"&gt; xslt
state machine implementation&lt;/a&gt; we have done back in 2008.
&lt;/p&gt;
&lt;p&gt;
You can &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"&gt; download
YieldProcessor here&lt;/a&gt;. We hope that someone will find our solution very interesting.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=ebd5185f-fc37-4e82-b2bb-006387635512" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,ebd5185f-fc37-4e82-b2bb-006387635512.aspx</comments>
      <category>Announce</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
      <category>Java</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=adb797fe-a781-423c-8dea-ea827d86241b</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,adb797fe-a781-423c-8dea-ea827d86241b.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,adb797fe-a781-423c-8dea-ea827d86241b.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=adb797fe-a781-423c-8dea-ea827d86241b</wfw:commentRss>
      <title>Yield feature in java</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,adb797fe-a781-423c-8dea-ea827d86241b.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2010/12/20/YieldFeatureInJava.aspx</link>
      <pubDate>Mon, 20 Dec 2010 16:28:35 GMT</pubDate>
      <description>&lt;p&gt;
Several times we have already wished to see &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=adb797fe-a781-423c-8dea-ea827d86241b&amp;amp;url=http%3a%2f%2fstackoverflow.com%2fquestions%2f1980953%2fis-there-a-java-equivalent-to-cs-yield-keyword"&gt; &lt;code&gt;yield&lt;/code&gt; feature
in java&lt;/a&gt; and all the time came to the same implementation: &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=adb797fe-a781-423c-8dea-ea827d86241b&amp;amp;url=http%3a%2f%2fcode.google.com%2fp%2finfomancers-collections%2f"&gt;infomancers-collections&lt;/a&gt;.
And every time with dissatisfaction turned away, and continued with regular iterators.
&lt;/p&gt;
&lt;p&gt;
Why? Well, in spite of the fact it's the best implementation of the feature we have
seen, it's still too heavy, as it's playing with java byte code at run-time.
&lt;/p&gt;
&lt;p&gt;
We never grasped the idea why it's done this way, while there is &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=adb797fe-a781-423c-8dea-ea827d86241b&amp;amp;url=http%3a%2f%2fdownload.oracle.com%2fjavase%2f1.5.0%2fdocs%2fguide%2fapt%2fGettingStarted.html"&gt;post-compile
time annotation processing in java&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
If we would implemented the yeild feature in java we would created a &lt;code&gt;@Yield&lt;/code&gt; annotation
and would demanded to implement some well defined code pattern like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; @Yield&lt;br /&gt;
Iteratable&amp;lt;String&gt; iterator()&lt;br /&gt;
{ 
&lt;br /&gt;
// This is part of pattern.&lt;br /&gt;
ArrayList&amp;lt;String&gt; list = new ArrayList&amp;lt;String&gt;();&lt;br /&gt;
&lt;br /&gt;&lt; 10; ++i)&lt;br /&gt;
for(int i = 0; i {&lt;br /&gt;
// list.add() plays the role of yield return.&lt;br /&gt;
list.add(String.valueOf(i));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is part of pattern.&lt;br /&gt;
return list;&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
or
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt; @Yield&lt;br /&gt;
Iterator&amp;lt;String&gt; iterator()&lt;br /&gt;
{&lt;br /&gt;
// This is part of pattern.&lt;br /&gt;
ArrayList&amp;lt;String&gt; list = new ArrayList&amp;lt;String&gt;();&lt;br /&gt;
&lt;br /&gt;&lt; 10; ++i)&lt;br /&gt;
for(int i = 0; i {&lt;br /&gt;
// list.add() plays the role of yield return.&lt;br /&gt;
list.add(String.valueOf(i));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is part of pattern.&lt;br /&gt;
return list.iterator();&lt;br /&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Note that the code will work correctly even, if by mischance, post-compile-time processing
will not take place.
&lt;/p&gt;
&lt;p&gt;
At post comile time we would do all required refactoring to turn these implementations
into a state machines thus runtime would not contain any third party components.
&lt;/p&gt;
&lt;p&gt;
It's iteresting to recall that we have also implemented similar refactoring in pure
xslt.
&lt;/p&gt;
&lt;p&gt;
See &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=adb797fe-a781-423c-8dea-ea827d86241b&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fweblog%2f2008%2f09%2f05%2fWhatYouCanDoWithJxom.aspx" rel="bookmark"&gt;What
you can do with jxom&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Update:&lt;/b&gt; implementation can be found at &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=adb797fe-a781-423c-8dea-ea827d86241b&amp;amp;url=http%3a%2f%2fwww.nesterovsky-bros.com%2fdownload%2fjava%2fYield.zip"&gt;Yield.zip&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=adb797fe-a781-423c-8dea-ea827d86241b" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,adb797fe-a781-423c-8dea-ea827d86241b.aspx</comments>
      <category>Java</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
      <category>xslt</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=83454782-f6aa-4aca-97d1-ae989fda46d9</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,83454782-f6aa-4aca-97d1-ae989fda46d9.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,83454782-f6aa-4aca-97d1-ae989fda46d9.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=83454782-f6aa-4aca-97d1-ae989fda46d9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We have a class <code>Beans</code> used to serialize a list of generic objects into
an xml. This is done like this:
</p>
        <p style="padding-left: 1em;">
          <code> public class Call<br />
{<br />
  public Beans input;<br />
  public Beans output;<br />
  ...<br />
}<br /><br />
@XmlJavaTypeAdapter(value = BeanAdapter.class) 
<br />
public class Beans 
<br />
{ 
<br />
  public List&lt;Object&gt; bean;<br />
} </code>
        </p>
        <p>
Thanks to <code>@XmlJavaTypeAdapter</code>, we're able to write xml in whatever form
we want.
</p>
        <p>
When we're serializing a <code>Call</code> instance:
</p>
        <p style="padding-left: 1em;">
          <code> Call call = ... 
<br />
Beans beans = ...;<br /><br />
call.setInput(beans); 
<br /><br />
JAXBContext context = ...;<br />
Marshaller marshaler = context.createMarshaller();<br />
ObjectFactory factory = ...;<br /><br />
marshaler.marshal(factory.createCall(call), result);</code>
        </p>
        <p>
things work as expected, meaning that <code>BeanAdapter</code> is used during xml
serialization. But if it's happened that you want to serialize a <code>Beans</code> instance
itself, you start getting problems with the serialization of unknown objects. That's
because JAXB does not use <code>BeanAdapter</code>.
</p>
        <p>
We have found a similar case "<a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=83454782-f6aa-4aca-97d1-ae989fda46d9&amp;url=http%3a%2f%2fmarkmail.org%2fmessage%2fiuje47wsfjmz6dp2">How
to assign an adapter to the root element?</a>", unfortunately with no satisfactory
explanation.
</p>
        <p>
That is strange. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=83454782-f6aa-4aca-97d1-ae989fda46d9" />
      </body>
      <title>Strange JAXB behaviour</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,83454782-f6aa-4aca-97d1-ae989fda46d9.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2010/12/11/StrangeJAXBBehaviour.aspx</link>
      <pubDate>Sat, 11 Dec 2010 08:48:00 GMT</pubDate>
      <description>&lt;p&gt;
We have a class &lt;code&gt;Beans&lt;/code&gt; used to serialize a list of generic objects into
an xml. This is done like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em;"&gt;
&lt;code&gt; public class Call&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; public Beans input;&lt;br&gt;
&amp;nbsp; public Beans output;&lt;br&gt;
&amp;nbsp; ...&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
@XmlJavaTypeAdapter(value = BeanAdapter.class) 
&lt;br&gt;
public class Beans 
&lt;br&gt;
{ 
&lt;br&gt;
&amp;nbsp; public List&amp;lt;Object&gt; bean;&lt;br&gt;
} &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Thanks to &lt;code&gt;@XmlJavaTypeAdapter&lt;/code&gt;, we're able to write xml in whatever form
we want.
&lt;/p&gt;
&lt;p&gt;
When we're serializing a &lt;code&gt;Call&lt;/code&gt; instance:
&lt;/p&gt;
&lt;p style="padding-left: 1em;"&gt;
&lt;code&gt; Call call = ... 
&lt;br&gt;
Beans beans = ...;&lt;br&gt;
&lt;br&gt;
call.setInput(beans); 
&lt;br&gt;
&lt;br&gt;
JAXBContext context = ...;&lt;br&gt;
Marshaller marshaler = context.createMarshaller();&lt;br&gt;
ObjectFactory factory = ...;&lt;br&gt;
&lt;br&gt;
marshaler.marshal(factory.createCall(call), result);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
things work as expected, meaning that &lt;code&gt;BeanAdapter&lt;/code&gt; is used during xml
serialization. But if it's happened that you want to serialize a &lt;code&gt;Beans&lt;/code&gt; instance
itself, you start getting problems with the serialization of unknown objects. That's
because JAXB does not use &lt;code&gt;BeanAdapter&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
We have found a similar case "&lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=83454782-f6aa-4aca-97d1-ae989fda46d9&amp;amp;url=http%3a%2f%2fmarkmail.org%2fmessage%2fiuje47wsfjmz6dp2"&gt;How
to assign an adapter to the root element?&lt;/a&gt;", unfortunately with no satisfactory
explanation.
&lt;/p&gt;
&lt;p&gt;
That is strange. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=83454782-f6aa-4aca-97d1-ae989fda46d9" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,83454782-f6aa-4aca-97d1-ae989fda46d9.aspx</comments>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=11ecf481-38d4-4a14-b22d-460e430285c3</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,11ecf481-38d4-4a14-b22d-460e430285c3.aspx</pingback:target>
      <dc:creator>Arthur Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,11ecf481-38d4-4a14-b22d-460e430285c3.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=11ecf481-38d4-4a14-b22d-460e430285c3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of our latest tasks was a conversion of data received from mainframe as an EBCDIC
flat file into an XML file in UTF-8 encoding for further processing. 
</p>
        <p>
The solution was rather straightforward: 
</p>
        <ul>
          <li>
read the source flat file, record-by-record; 
</li>
          <li>
serialize each record as an element into target XML file using JAXB.</li>
        </ul>
        <p>
For reading data from EBCDIC encoded flat file, a good old tool named eXperanto was
used. It allows to define C# and/or Java classes that suit for records in the source
flat file. Thus we were able to read and convert records from EBCDIC to UTF-8. 
</p>
        <p>
The next sub-task was to serialize a Java bean to an XML element. JAXB marshaller
was used for this. 
</p>
        <p>
Everything was ok, until we had started to test the implementation on real data. 
</p>
        <p>
We've realized that some decimal values (BigDecimal fields in Java classes) were serialized
in scientific exponential notation. For example: 0.000000365 was serialized as 3.65E-7
and so on. 
</p>
        <p>
On the other hand, the target XML was used by another (non Java) application, which
expected to receive decimal data, as it was defined in XSD schema (the field types
were specified as <code>xs:decimal</code>). 
</p>
        <p>
According with <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=11ecf481-38d4-4a14-b22d-460e430285c3&amp;url=http%3a%2f%2fwww.w3.org%2fTR%2f2004%2fREC-xmlschema-2-20041028%2fdatatypes.html%23decimal" target="_blank">W3C
datatypes specification</a>: 
</p>
        <p>
          <i>"...decimal has a lexical representation consisting of a finite-length sequence
of decimal digits (#x30-#x39) separated by a period as a decimal indicator. An optional
leading sign is allowed. If the sign is omitted, "+" is assumed. Leading and trailing
zeroes are optional. If the fractional part is zero, the period and following zero(es)
can be omitted. For example: -1.23, 12678967.543233, 100000.00, 210..." </i>
        </p>
        <p>
So, the result was predictable, the consumer application fails. 
</p>
        <p>
Google search reveals that we deal with a well-known bug: <a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=11ecf481-38d4-4a14-b22d-460e430285c3&amp;url=http%3a%2f%2fbugs.sun.com%2fbugdatabase%2fview_bug.do%3fbug_id%3d6846140" target="_blank">"JAXB
marshaller returns BigDecimal with scientific notation in JDK 6"</a>. It remains open
already an year and half since May 2009, marked as "Fix in progress". We've tested
our application with Java version 1.6.0_21-b07, JAXB 2.1. 
</p>
        <p>
Although this is rather critical bug that may affect on interoperability of Java applications
(e.g. Java web services etc.), its priority was set just as "4-Low". 
</p>
        <p>
          <b>P.S.</b> as a temporary workaround for this case only(!) we've replaced <code>xs:decimal</code> on <code>xs:double</code> in
XSD schema for the target application. 
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=11ecf481-38d4-4a14-b22d-460e430285c3" />
      </body>
      <title>BigDecimal + JAXB =&gt; potential interoperability problems</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,11ecf481-38d4-4a14-b22d-460e430285c3.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2010/09/05/BigDecimalJAXBPotentialInteroperabilityProblems.aspx</link>
      <pubDate>Sun, 05 Sep 2010 12:58:23 GMT</pubDate>
      <description>&lt;p&gt;
One of our latest tasks was a conversion of data received from mainframe as an EBCDIC
flat file into an XML file in UTF-8 encoding for further processing. 
&lt;/p&gt;
&lt;p&gt;
The solution was rather straightforward: 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
read the source flat file, record-by-record; 
&lt;li&gt;
serialize each record as an element into target XML file using JAXB.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
For reading data from EBCDIC encoded flat file, a good old tool named eXperanto was
used. It allows to define C# and/or Java classes that suit for records in the source
flat file. Thus we were able to read and convert records from EBCDIC to UTF-8. 
&lt;/p&gt;
&lt;p&gt;
The next sub-task was to serialize a Java bean to an XML element. JAXB marshaller
was used for this. 
&lt;/p&gt;
&lt;p&gt;
Everything was ok, until we had started to test the implementation on real data. 
&lt;/p&gt;
&lt;p&gt;
We've realized that some decimal values (BigDecimal fields in Java classes) were serialized
in scientific exponential notation. For example: 0.000000365 was serialized as 3.65E-7
and so on. 
&lt;/p&gt;
&lt;p&gt;
On the other hand, the target XML was used by another (non Java) application, which
expected to receive decimal data, as it was defined in XSD schema (the field types
were specified as &lt;code&gt;xs:decimal&lt;/code&gt;). 
&lt;/p&gt;
&lt;p&gt;
According with &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=11ecf481-38d4-4a14-b22d-460e430285c3&amp;amp;url=http%3a%2f%2fwww.w3.org%2fTR%2f2004%2fREC-xmlschema-2-20041028%2fdatatypes.html%23decimal" target=_blank&gt;W3C
datatypes specification&lt;/a&gt;: 
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;"...decimal has a lexical representation consisting of a finite-length sequence
of decimal digits (#x30-#x39) separated by a period as a decimal indicator. An optional
leading sign is allowed. If the sign is omitted, "+" is assumed. Leading and trailing
zeroes are optional. If the fractional part is zero, the period and following zero(es)
can be omitted. For example: -1.23, 12678967.543233, 100000.00, 210..." &lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
So, the result was predictable, the consumer application fails. 
&lt;/p&gt;
&lt;p&gt;
Google search reveals that we deal with a well-known&amp;nbsp;bug: &lt;a href="http://www.nesterovsky-bros.com/weblog/ct.ashx?id=11ecf481-38d4-4a14-b22d-460e430285c3&amp;amp;url=http%3a%2f%2fbugs.sun.com%2fbugdatabase%2fview_bug.do%3fbug_id%3d6846140" target=_blank&gt;"JAXB
marshaller returns BigDecimal with scientific notation in JDK 6"&lt;/a&gt;. It remains open
already an year and half since May 2009, marked as "Fix in progress". We've tested
our application with Java version 1.6.0_21-b07, JAXB 2.1. 
&lt;/p&gt;
&lt;p&gt;
Although this is rather critical bug that may affect on interoperability of Java applications
(e.g. Java web services etc.), its priority was set just as "4-Low". 
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;P.S.&lt;/b&gt; as a temporary workaround for this case only(!) we've replaced &lt;code&gt;xs:decimal&lt;/code&gt; on &lt;code&gt;xs:double&lt;/code&gt; in
XSD schema for the target application. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=11ecf481-38d4-4a14-b22d-460e430285c3" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,11ecf481-38d4-4a14-b22d-460e430285c3.aspx</comments>
      <category>Java</category>
      <category>Tips and tricks</category>
    </item>
  </channel>
</rss>