<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Daniel Gazineu</title>
	<atom:link href="http://blog.danielgazineu.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.danielgazineu.com</link>
	<description></description>
	<lastBuildDate>Sat, 15 May 2010 03:16:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Test Driven Development and Java ME</title>
		<link>http://blog.danielgazineu.com/2010/05/15/test-driven-development-and-java-me/</link>
		<comments>http://blog.danielgazineu.com/2010/05/15/test-driven-development-and-java-me/#comments</comments>
		<pubDate>Sat, 15 May 2010 03:16:31 +0000</pubDate>
		<dc:creator>Daniel Gazineu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Code Design]]></category>
		<category><![CDATA[Conference]]></category>
		<category><![CDATA[JavaME]]></category>
		<category><![CDATA[JavaOne]]></category>
		<category><![CDATA[JavaOne10]]></category>
		<category><![CDATA[Talk]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blog.danielgazineu.com/?p=88</guid>
		<description><![CDATA[At the very beginning of my career as a software developer, I was hired to work at a pioneer company on the field of wireless software. There I learned Java ME (J2ME at that time) almost at the same time I learned Java itself. As a newbie programmer, I was not aware of the benefits [...]]]></description>
			<content:encoded><![CDATA[<p>At the very beginning of my career as a software developer, I was hired to work at a pioneer company on the field of wireless software. There I learned Java ME (J2ME at that time) almost at the same time I learned Java itself. As a newbie programmer, I was not aware of the benefits of a good code design, neither familiar with methodologies like TDD, DDD, etc.<br />
After sometime, I left the company and moved from wireless devices to server side, where I was presented to Hibernate, Spring, EJB and all Design Patterns and development methodologies surrounding these technologies. A long time passed and now I am again working at a company that breaths wireless market. Although most of the projects I work have mobile phones as target access point, I spend most of the time building the backend to provide content to these clients. Anyway, now and then I find myself doing some maintenance or adding a new feature to a Java ME application and when this happens, I feel like having a déjà vu.<br />
Even the best Java ME developers that I had the pleasure to work with still do not practice Test Driven Development and believe Dependency Injection is only useful for server side environments. Most of the applications I have seen cannot be easily tested due to large dependency on Java ME libraries and external resources. When I try or suggest refactoring I normally listen something like “it’ll complicate things”, “you’re too much server-oriented, in the mobile world we do not need so many interfaces”. I respectfully disagree.<br />
Dependency Injection (DI) is the best friend of Unit Test. Moreover, every time you instantiate an environment-specific object into a domain class, a Ferrari crashes in Italy. Are you willing to give up automated testing, destroy a friendship and a Ferrari? Using DI you can eliminate dependency from Java ME classes, enable your code to be unit tested and save the World (or at least some time).</p>
<p>I will go deeper in this topic at the JavaOne 2010, where I will show some anti-patterns I’ve seen and discuss how to avoid them.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danielgazineu.com%2F2010%2F05%2F15%2Ftest-driven-development-and-java-me%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px;height:100px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.danielgazineu.com/2010/05/15/test-driven-development-and-java-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design Pattern for processing a huge XML file: The Solution</title>
		<link>http://blog.danielgazineu.com/2010/04/27/design-pattern-for-processing-a-huge-xml-file-the-solution/</link>
		<comments>http://blog.danielgazineu.com/2010/04/27/design-pattern-for-processing-a-huge-xml-file-the-solution/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 13:35:48 +0000</pubDate>
		<dc:creator>Daniel Gazineu</dc:creator>
				<category><![CDATA[Discussions]]></category>
		<category><![CDATA[Code Design]]></category>
		<category><![CDATA[Parsers]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.danielgazineu.com/?p=72</guid>
		<description><![CDATA[This post is the sequence of the last one I wrote describing the problem I recently faced when needed to parse and process a big and complex XML file.
After playing around with the conventional solutions, I was not convinced to leave xPath/DOM code legibility for an effective memory consumption result.
To understand my solution, it’s important [...]]]></description>
			<content:encoded><![CDATA[<p>This post is the sequence of <a href="http://blog.danielgazineu.com/2010/04/14/design-pattern-for-processing-a-huge-xml-file-the-problem/">the last one I wrote</a> describing the problem I recently faced when needed to parse and process a big and complex XML file.</p>
<p>After playing around with the conventional solutions, I was not convinced to leave xPath/DOM code legibility for an effective memory consumption result.</p>
<p>To understand my solution, it’s important to analyze the data I’m working with. Although the schema is complex and the file contains lots of data, the root tag represents a list of entities (table records) and there is no dependency between nodes. They can (and really might) be processed in a parallel way.</p>
<p>My solution uses a hybrid producer-consumer implementation, where a reader class loads XML contents to memory and dispatches small segments to a parser responsible for processing each segment as if it was a complete file, but without memory consumption concerns.</p>
<p>There are three main steps in the entire parsing process as follows:</p>
<p><strong>Loading</strong></p>
<p>First, I created a class named Reader, which the only purpose is to load the contents of a given XML file to the memory and dispatch it for processing. This class contains a buffer size based in the number of loaded entities. In other words, if the XML file contains a root tag named Cars, with a list of Car nodes, the buffer will be counting occurrences of &lt;/Car&gt;. When a given number of entities is loaded, data is dispatched for processing and the buffer is reset.</p>
<p><strong>Dispatching</strong></p>
<p>Instead of dispatching data directly to the Parser, the Reader object has an implementation of the ParserDispatcher and uses it for this job. The idea behind this is to abstract the execution environment from the rest of the code. While a valid ParserDispatcher implementation for server-side environment would be posting the data to a JMS Queue, my command line desktop application uses an ExecutorService for the same purpose.</p>
<p><strong>Parsing</strong></p>
<p>Parsing process itself doesn’t have any novelty besides the fact that the huge XML file, after broken in small blocks, can be parsed with xPath/DOM without compromising memory consumption or performance. The Parser class is a common XML parser, unaware of prior stages the data was submitted to, it is able to parse any given InputStream since it points to an XML content compliant with its Schema and small enough to be completely represented by a DOM structure in memory. After each entity is parsed, a list of listeners is notified. These listeners can persist, log, count, create reports, etc.</p>
<p><a href="http://blog.danielgazineu.com/wp-content/uploads/2010/04/new-classdiagram.jpg"><img class="aligncenter size-full wp-image-80" title="classdiagram" src="http://blog.danielgazineu.com/wp-content/uploads/2010/04/new-classdiagram.jpg" alt="" width="399" height="655" /></a></p>
<p>A client application would run by calling the following lines:</p>
<p><code><br />
Parser parser = <strong>new</strong> Parser();<br />
parser.addListener(<strong>new</strong> DebugListener());<br />
Reader xmlReader = <strong>new</strong> Reader(<strong>new</strong> NewThreadDispatcher(parser));<br />
xmlReader.read(<strong>new</strong> FileInputStream("file.xml"));<br />
</code></p>
<p>My friend <a href="http://ladoservidor.com/">Paulo Jeronimo</a>, commented in my last post suggesting me to use StAX. Being a pull parser over stream, StAX tries to bring the best of both worlds but in my opinion, a code using StAX is not as legible as it would be using xPath/DOM, that’s why I decided to create my own design.</p>
<p>Although this is neither the most performatic nor the simplest solution, I believe this brings a good balance between performance, memory consumption and code maintainability. Moreover, this pattern can be extended for other file types.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danielgazineu.com%2F2010%2F04%2F27%2Fdesign-pattern-for-processing-a-huge-xml-file-the-solution%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px;height:100px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.danielgazineu.com/2010/04/27/design-pattern-for-processing-a-huge-xml-file-the-solution/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Design Pattern for processing a huge XML file: The Problem</title>
		<link>http://blog.danielgazineu.com/2010/04/14/design-pattern-for-processing-a-huge-xml-file-the-problem/</link>
		<comments>http://blog.danielgazineu.com/2010/04/14/design-pattern-for-processing-a-huge-xml-file-the-problem/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 02:57:56 +0000</pubDate>
		<dc:creator>Daniel Gazineu</dc:creator>
				<category><![CDATA[Discussions]]></category>
		<category><![CDATA[Code Design]]></category>
		<category><![CDATA[Parsers]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.danielgazineu.com/?p=65</guid>
		<description><![CDATA[Some days ago I started working in a project that requires parsing and storing information contained in huge files of different formats. These files are sent by partners of our client and represent data contained in their databases. Sometimes this data is consistent and useful for our system, other times it’s just crap. As we [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago I started working in a project that requires parsing and storing information contained in huge files of different formats. These files are sent by partners of our client and represent data contained in their databases. Sometimes this data is consistent and useful for our system, other times it’s just crap. As we do not have access to their databases, it’s necessary to parse and store in a database and then query this data in order to understand how consistent and complete it is.</p>
<p>Last week my manager asked me to parse the content of an XML file with more than 500MB. The result of this activity would give us information about the quality of the data that that partner could provide us and then we would be able to decide if the process of parsing and storing such schema would be permanently added to the system or just thrown away.</p>
<p>Although the system runs in a Java EE container, for a single process like this I consider much easier to create a Java SE application that receives a filename as parameter, parses and stores it. On the other hand, if the result shows that the partner’s database is consistent and useful enough, this is not a single process anymore and this code must be added to the project. Given that, it was strongly recommended to implement the code in a way that it could be easily refactored from desktop to server environment.</p>
<p>As I said before, the file size was higher than 500MB and this was just a test file, next ones (if exist) might be bigger one gigabyte. Loading so large content to the heap wouldn’t be possible in production. Once DOM was not an option, XPath was also discarded and SAX became the only option. The problem now was that the schema is very complex and the code necessary to parse it using SAX would easily become too messy to be maintained.</p>
<p>Well, that's enough for today! Now I'll let you think about this problem and in a few days I'll describe my solution here.</p>
<p>UPDATE: Post with my solution for this problem can be found <a href="http://blog.danielgazineu.com/2010/04/27/design-pattern-for-processing-a-huge-xml-file-the-solution/">here</a>.</p>
<p>See you soon!</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danielgazineu.com%2F2010%2F04%2F14%2Fdesign-pattern-for-processing-a-huge-xml-file-the-problem%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px;height:100px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.danielgazineu.com/2010/04/14/design-pattern-for-processing-a-huge-xml-file-the-problem/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Talk accepted at Devoxx09</title>
		<link>http://blog.danielgazineu.com/2009/10/16/talk-accepted-at-devoxx09/</link>
		<comments>http://blog.danielgazineu.com/2009/10/16/talk-accepted-at-devoxx09/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 03:52:59 +0000</pubDate>
		<dc:creator>Daniel Gazineu</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[Conference]]></category>
		<category><![CDATA[Devoxx]]></category>
		<category><![CDATA[Devoxx09]]></category>
		<category><![CDATA[Fluent Interfaces]]></category>
		<category><![CDATA[Fluent Validation Framework]]></category>
		<category><![CDATA[Talk]]></category>

		<guid isPermaLink="false">http://blog.danielgazineu.com/?p=54</guid>
		<description><![CDATA[Continuing the sequence of news, I’m glad to announce that my Quickie Talk “Fluent Validation Framework - a DSL for validations using Fluent Interfaces” was accepted at Devoxx09.
I’m very happy with the opportunity to contribute in such important event.
I’ll have only 15 minutes there (that’s why it’s called “quickie”), but will try to bring as [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing the sequence of news, I’m glad to announce that my Quickie Talk “Fluent Validation Framework - a DSL for validations using Fluent Interfaces” was accepted at Devoxx09.</p>
<p>I’m very happy with the opportunity to contribute in such important event.</p>
<p>I’ll have only 15 minutes there (that’s why it’s called “quickie”), but will try to bring as much content as I can about Fluent Interfaces development and issues. The talk will be based on my framework for validations, which I think, was a nice exercise inside Fluent Interfaces development world.</p>
<p>The abstract can be found <a href="http://www.devoxx.com/display/DV09/Fluent+Validation+Framework%2C+a+DSL+for+validations+using+Fluent+Interfaces" target="_blank">here</a>.</p>
<p>See you next month at Devoxx09!</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danielgazineu.com%2F2009%2F10%2F16%2Ftalk-accepted-at-devoxx09%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px;height:100px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.danielgazineu.com/2009/10/16/talk-accepted-at-devoxx09/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Twitter application to tweet your SVN commits</title>
		<link>http://blog.danielgazineu.com/2009/09/30/a-twitter-application-to-tweet-your-svn-commits/</link>
		<comments>http://blog.danielgazineu.com/2009/09/30/a-twitter-application-to-tweet-your-svn-commits/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 16:10:46 +0000</pubDate>
		<dc:creator>Daniel Gazineu</dc:creator>
				<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://blog.danielgazineu.com/?p=48</guid>
		<description><![CDATA[After a long time without posting anything, it's time to return to the normal routine. By the way, the subject of this post is just about one of the reasons for my recent disappearing. I'm here today to introduce you to my last work: TweetSVN
TweetSVN is a Twitter application where you can associate a Twitter [...]]]></description>
			<content:encoded><![CDATA[<p>After a long time without posting anything, it's time to return to the normal routine. By the way, the subject of this post is just about one of the reasons for my recent disappearing. I'm here today to introduce you to my last work: <a href="http://www.tweetsvn.com/">TweetSVN</a></p>
<p>TweetSVN is a Twitter application where you can associate a Twitter account with an SVN repository. After configuring just a few information, you'll start seeing your SVN commits being tweeted by the provided Twitter account.</p>
<p>It is a Beta version, not a Google-like-full-powered-beta-version, but a limited one, with an small set of features. We're planning to deliver some new features soon, but you can also send your suggestions (and bug reports off course) via Twitter:</p>
<p><a href="http://twitter.com/TweetSVN">@TweetSVN</a> - project account<br />
<a href="http://twitter.com/marlonluz">@marlonluz</a> - a friend who developed the application with me<br />
<a href="http://twitter.com/danielgazineu">@danielgazineu</a> - me</p>
<p>Technicaly speaking, TweetSVN is a Java Web project running over Apache Tomcat. Its interface was made in Flex and the core is powered by Hibernate, Quartz, Twitter4J and SVNKit.</p>
<p>So people, that's all for today, I hope coming with more good news shortly.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danielgazineu.com%2F2009%2F09%2F30%2Fa-twitter-application-to-tweet-your-svn-commits%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px;height:100px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.danielgazineu.com/2009/09/30/a-twitter-application-to-tweet-your-svn-commits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A DSL for validations using fluent interfaces</title>
		<link>http://blog.danielgazineu.com/2009/08/09/a-dsl-for-validations-using-fluent-interfaces/</link>
		<comments>http://blog.danielgazineu.com/2009/08/09/a-dsl-for-validations-using-fluent-interfaces/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 05:24:25 +0000</pubDate>
		<dc:creator>Daniel Gazineu</dc:creator>
				<category><![CDATA[Discussions]]></category>
		<category><![CDATA[Code Design]]></category>
		<category><![CDATA[Fluent Interfaces]]></category>
		<category><![CDATA[Fluent Validation Framework]]></category>

		<guid isPermaLink="false">http://blog.danielgazineu.com/?p=27</guid>
		<description><![CDATA[As a software developer, I’m particularly interested in Domain Driven Design (DDD) and Domain Specific Languages (DSL). In the last few weeks I’ve been flirting with Fluent Interfaces and trying to get used to its techniques in order to apply it to build better DSL for my domains.
During my studies, I decided to build a [...]]]></description>
			<content:encoded><![CDATA[<p>As a software developer, I’m particularly interested in Domain Driven Design (DDD) and Domain Specific Languages (DSL). In the last few weeks I’ve been flirting with Fluent Interfaces and trying to get used to its techniques in order to apply it to build better DSL for my domains.</p>
<p>During my studies, I decided to build a small validation framework with a fluent interface to put in practice what I’m reading. It’s far from being ready for professional usage but its development is being a good exercise with fluent interfaces and DSL.</p>
<p>What I was looking for in the beginning of its creation was something to enable the developer to make validations in a more human friendly way.</p>
<p>Let’s say you have an object called ‘myObject’. How would you ask to a validation framework to ensure it is not null?<br />
Well, I made this question to myself and one of the possible answers was:</p>
<p>“Ensure myObject is not null.”</p>
<p>This part was pretty easy to implement and the first draft of the Fluent Validation Framework (as I call it) looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But at the same time that I would like to build something as simple as:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isEqualsTo</span><span style="color: #009900;">&#40;</span>myOtherObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I also wanted to have more control of the result of the validation. Something like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">otherwise</span><span style="color: #009900;">&#40;</span>“myObject cannot be <span style="color: #000066; font-weight: bold;">null</span>”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Or even better:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">otherwise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">throwThis</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In both examples above we can see a problem inherent to the <a href="http://martinfowler.com/dslwip/MethodChaining.html">Method Chaining</a> technique. It’s not possible to know during the execution of <code>isNotNull()</code> method that there is another method to be executed after it, this way, using this approach, it would be necessary to add an execution command at the end of the chain:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">otherwise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">throwThis</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">now</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This works, but lets our DSL too extensive. A simple null verification would be:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"> ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">now</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Moreover, I didn’t like this <code>now()</code> method in the end of the sentence, it’s not so fluent and intuitive.<br />
This way, I decided to move to the <a href="http://martinfowler.com/dslwip/NestedFunction.html">Nested Function</a> approach. After some rethinking, the API usage for the same situations listed above was:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">is</span><span style="color: #009900;">&#40;</span>notNull<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, otherwise<span style="color: #009900;">&#40;</span>
                                   throwThis<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Also, the short validation would work with few parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">is</span><span style="color: #009900;">&#40;</span>notNull<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This brings a good side benefit: passing the validation as a parameter to <code>is(Condition)</code> method enables the developer to create his own validation conditions and extend the framework to work with his domain. In the other hand, I added more necessary static imports to the code, otherwise, the code would be:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Validator.<span style="color: #006633;">ensure</span><span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">is</span><span style="color: #009900;">&#40;</span>Conditions.<span style="color: #006633;">notNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                              Actions.<span style="color: #006633;">otherwise</span><span style="color: #009900;">&#40;</span>
                                  Actions.<span style="color: #006633;">throwThis</span><span style="color: #009900;">&#40;</span>
                                      <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Too much code for a simple if/else block. Moreover, this verbose code doesn’t seem to be so human oriented.</p>
<p>After thinking for a while, I fingered you that the technology problem I was facing could be solved by a linguistic review of the solution. It’s right that an easy way to ask a framework to validate something is saying:</p>
<p>Ensure myObject is not null, otherwise, throw this exception.</p>
<p>But it can also be said:</p>
<p>Analyze myObject and throw this exception if it’s null.</p>
<p>This way, I achieved the following result:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>or:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">analyse<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">and</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">throwThis</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">ifNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Moreover, this solution uses internally the methods <code>is(Condition)</code>, <code>ifIsNot(Condition)</code> and <code>and(Action)</code>, what enables developer to create his own conditions and actions:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ensure<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">is</span><span style="color: #009900;">&#40;</span>inValidState<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>or:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">analyse<span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">and</span><span style="color: #009900;">&#40;</span>logAnError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">ifIsNot</span><span style="color: #009900;">&#40;</span>inValidState<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Assuming that methods <code>inValidState()</code> and <code>logAnError()</code> were created by the developer and return a <code>Condition</code> and an <code>Action</code> respectively.</p>
<p>My idea is to improve this framework adding specific validations for Strings, Numbers, Arrays and Collections, letting specific domain validations to be built inside their domains.</p>
<p>Soon I’ll be posting and discussing this code here, but now it’s time to sleep!</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danielgazineu.com%2F2009%2F08%2F09%2Fa-dsl-for-validations-using-fluent-interfaces%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px;height:100px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.danielgazineu.com/2009/08/09/a-dsl-for-validations-using-fluent-interfaces/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Exposing bitwise operations in a fluent interface</title>
		<link>http://blog.danielgazineu.com/2009/07/20/exposing-bitwise-operations-in-a-fluent-interface/</link>
		<comments>http://blog.danielgazineu.com/2009/07/20/exposing-bitwise-operations-in-a-fluent-interface/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 04:26:21 +0000</pubDate>
		<dc:creator>Daniel Gazineu</dc:creator>
				<category><![CDATA[Discussions]]></category>
		<category><![CDATA[Code Design]]></category>
		<category><![CDATA[Fluent Interfaces]]></category>

		<guid isPermaLink="false">http://blog.danielgazineu.com/?p=3</guid>
		<description><![CDATA[Some days ago I had to develop a class that would represent an event and should contain a weekly recurrence attribute. First idea that came to mind was to use seven lower representative bits of a byte as days of a week and flagging them on and off using bitwise operators. That’s a pretty common [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago I had to develop a class that would represent an event and should contain a weekly recurrence attribute. First idea that came to mind was to use seven lower representative bits of a byte as days of a week and flagging them on and off using bitwise operators. That’s a pretty common resolution for such kind of problem and what I want to share here is not this solution, but the interface I defined to expose it.<br />
I tried to define a fluent interface for it and right now, that's the way a client uses this class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">WeeklyRecurrence rec <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">repeatOn</span><span style="color: #009900;">&#40;</span>EVERY_DAY<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">but</span><span style="color: #009900;">&#40;</span>TUESDAY,THURSDAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertTrue<span style="color: #009900;">&#40;</span>rec.<span style="color: #006633;">occursOn</span><span style="color: #009900;">&#40;</span>WEDNESDAY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertFalse<span style="color: #009900;">&#40;</span>rec.<span style="color: #006633;">doesNotOccurOn</span><span style="color: #009900;">&#40;</span>FRIDAY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Moreover, I created an <code>escape()</code> method to point days where the recurrence doesn’t occur:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">rec <span style="color: #339933;">=</span> rec.<span style="color: #006633;">escape</span><span style="color: #009900;">&#40;</span>WEDNESDAY<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">but</span><span style="color: #009900;">&#40;</span>TUESDAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertTrue<span style="color: #009900;">&#40;</span>rec.<span style="color: #006633;">doesNotOccurOn</span><span style="color: #009900;">&#40;</span>WEDNESDAY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertTrue<span style="color: #009900;">&#40;</span>rec.<span style="color: #006633;">occursOn</span><span style="color: #009900;">&#40;</span>TUESDAY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, <code>but()</code> method is contextual and works adding or removing days from a recurrence, always in the opposite way of the last operation.<br />
You may also notice that <code>WeeklyRecurrence</code> is immutable, in order to avoid complications with changed states. This way, the following is correct:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">WeeklyRecurrence r1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
WeeklyRecurrence r2 <span style="color: #339933;">=</span> r1.<span style="color: #006633;">repeatOn</span><span style="color: #009900;">&#40;</span>EVERY_DAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertFalse<span style="color: #009900;">&#40;</span>r1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>r2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
r1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">repeatOn</span><span style="color: #009900;">&#40;</span>MONDAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
r2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">repeatOn</span><span style="color: #009900;">&#40;</span>MONDAY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertTrue<span style="color: #009900;">&#40;</span>r1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>r2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The following box contains the complete code for <code>WeeklyRecurrence</code>. The idea of this post is to show that even "low-level" solutions can expose fluent and clean interfaces to the other application layers.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Represents recurrence of something in a week.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WeeklyRecurrence <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// the value</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">byte</span> recurrence<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Utility method to merge n days with an original recurrence value
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">byte</span> merge<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> original, Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Day day <span style="color: #339933;">:</span> days<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			original <span style="color: #339933;">|=</span> day.<span style="color: #006633;">value</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> original<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Utility method to remove n days from an original recurrence
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">byte</span> diff<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> original, Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">byte</span> escape <span style="color: #339933;">=</span> merge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">0</span>, days<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>original <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>Day.<span style="color: #006633;">EVERY_DAY</span>.<span style="color: #006633;">value</span> <span style="color: #339933;">-</span> escape<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Enum that represents days of a week
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> Day <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 10000000</span>
		SUNDAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>,
&nbsp;
		<span style="color: #666666; font-style: italic;">// 10000000</span>
		MONDAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>,
&nbsp;
		<span style="color: #666666; font-style: italic;">// 01000000</span>
		TUESDAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>,
&nbsp;
		<span style="color: #666666; font-style: italic;">// 00010000</span>
		WEDNESDAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span>,
&nbsp;
		<span style="color: #666666; font-style: italic;">// 00001000</span>
		THURSDAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span>,
&nbsp;
		<span style="color: #666666; font-style: italic;">// 00000100</span>
		FRIDAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span>,
&nbsp;
		<span style="color: #666666; font-style: italic;">// 00000010</span>
		SATURDAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span>,
&nbsp;
		<span style="color: #666666; font-style: italic;">// 11111110</span>
		EVERY_DAY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">127</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">byte</span> value<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> Day<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">value</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Just for internal use, initializes a weeklyrecurrence with a byte value
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> recurrence<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">recurrence</span> <span style="color: #339933;">=</span> recurrence<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Initializes a &lt;code&gt;WeeklyRecurrence&lt;/code&gt; with no recurrence.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Discovers if given recurrence is set. It returns &lt;code&gt;true&lt;/code&gt; even
	 * if there are more days in the current recurrence.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isSet<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> otherRecurrence<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>recurrence <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span> otherRecurrence<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> otherRecurrence<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Creates a new &lt;code&gt;WeeklyRecurrence&lt;/code&gt; merging given days with the
	 * one in the current recurrence instance.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> WeeklyRecurrence repeatOn<span style="color: #009900;">&#40;</span>Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">byte</span> newRecurr <span style="color: #339933;">=</span> merge<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">recurrence</span>, days<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span>newRecurr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> WeeklyRecurrence but<span style="color: #009900;">&#40;</span>Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> escape<span style="color: #009900;">&#40;</span>days<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Creates a new &lt;code&gt;WeeklyRecurrence&lt;/code&gt; instance removing given days
	 * from the current recurrence instance.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> WeeklyRecurrence escape<span style="color: #009900;">&#40;</span>Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">byte</span> newRecurr <span style="color: #339933;">=</span> diff<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">recurrence</span>, days<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> WeeklyRecurrence<span style="color: #009900;">&#40;</span>newRecurr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> WeeklyRecurrence but<span style="color: #009900;">&#40;</span>Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> repeatOn<span style="color: #009900;">&#40;</span>days<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Creates a new &lt;code&gt;WeeklyRecurrence&lt;/code&gt; including or removing given
	 * days to the current instance, always in the opposite way of the last
	 * operation. If no recurrence was set, it throws an
	 * &lt;code&gt;IllegalStateException&lt;/code&gt;
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> WeeklyRecurrence but<span style="color: #009900;">&#40;</span>Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No recurrence was set yet&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Informs if none of the given days are in the current recurrence.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> doesNotOccurOn<span style="color: #009900;">&#40;</span>Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Day day <span style="color: #339933;">:</span> days<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isSet<span style="color: #009900;">&#40;</span>day.<span style="color: #006633;">value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Informs if every given days are in the current recurrence.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> occursOn<span style="color: #009900;">&#40;</span>Day... <span style="color: #006633;">days</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">byte</span> rec <span style="color: #339933;">=</span> merge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">0</span>, days<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> isSet<span style="color: #009900;">&#40;</span>rec<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> prime <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		result <span style="color: #339933;">=</span> prime <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> recurrence<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> obj<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> obj.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		WeeklyRecurrence other <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>WeeklyRecurrence<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>recurrence <span style="color: #339933;">!=</span> other.<span style="color: #006633;">recurrence</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.danielgazineu.com%2F2009%2F07%2F20%2Fexposing-bitwise-operations-in-a-fluent-interface%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px;height:100px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.danielgazineu.com/2009/07/20/exposing-bitwise-operations-in-a-fluent-interface/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
