<?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>Everyday Coder &#187; Java</title>
	<atom:link href="http://everydaycoder.com/category/coding/java/feed" rel="self" type="application/rss+xml" />
	<link>http://everydaycoder.com</link>
	<description>The rants and raves of a programmer in the trenches.</description>
	<lastBuildDate>Wed, 24 Sep 2008 04:20:27 +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>Enum as an XML Attribute with XStream</title>
		<link>http://everydaycoder.com/2006/12/08/enum-attribute-xstream.html</link>
		<comments>http://everydaycoder.com/2006/12/08/enum-attribute-xstream.html#comments</comments>
		<pubDate>Fri, 08 Dec 2006 05:56:43 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://everydaycoder.com/2006/12/08/enum-attribute-xstream.html</guid>
		<description><![CDATA[I am the type of developer that uses existing 3rd-party solutions whenever possible. Lately, I've been using XStream for object persistance in a standalone Java application. So far, I must say that I'm extremely impressed with how well this API is put together and also with its simplicity. Exporting an entire object tree is as [...]]]></description>
			<content:encoded><![CDATA[<p>I am the type of developer that uses existing 3rd-party solutions whenever possible. Lately, I've been using <a href="http://xstream.codehaus.org/">XStream</a> for object persistance in a standalone Java application. So far, I must say that I'm extremely impressed with how well this API is put together and also with its simplicity. Exporting an entire object tree is as simple as <code>new XStream().toXML(myObjectTree);</code></p>
<p>One of the great features of XStream is its ability for customization of the XML. I began customizing my XML and ran into a slight snag (not a bug, but a snag). I couldn't figure out how to get an enum field to output as an attribute rather than a child element. Example Java class:</p>
<blockquote class="code"><p>class Card {<br />
    int val;<br />
    Type type;</p>
<p>    enum Type {<br />
        HEARTS, CLUBS, DIAMONDS, SPADES<br />
    }<br />
}</p></blockquote>
<p>Now let's export it:</p>
<blockquote class="code"><p>void export(List&lt;Card&gt; cards) {<br />
    XStream xs = new XStream();<br />
    xs.alias("cards", List.class);<br />
    xs.alias("card", Card.class);<br />
    xs.useAttributeFor("val", int.class);<br />
    xs.useAttributeFor("type", Type.class);</p>
<p>    System.out.print(xs.toXML(cards));<br />
}</p></blockquote>
<p>Executing the above code will produce the following XML:</p>
<blockquote class="code"><p>&lt;cards&gt;<br />
    &lt;card val="1"&gt;<br />
        &lt;type&gt;SPADES&lt;/type&gt;<br />
    &lt;/card&gt;<br />
&lt;/cards&gt;</p></blockquote>
<p>As you can see, the <code>type</code> field is not properly used as an attribute like I instructed: <code>xs.useAttributeFor("type", Type.class);</code> The reason? Well, I wasn't sure, so, being the lazy developer I am, I trolled around Google, et. al. to find if someone else had this problem. Nothing. And after a few minutes of poking around in the XStream documentation, I was at a loss. I sent an email describing my problem to the users' mailing list, expecting to wait a day or so for an answer. Supprisingly, I recieved an answer within a few hours, from the project lead himself.</p>
<p>Jörg informed me that attribute converters must be of type <code>SingleValueConverter</code>. Attributes are single text values and don't need all of that other object (un)ravelling stuff. Jörg also pointed me to <a href="http://jira.codehaus.org/browse/XSTR-367">an issue</a> that had been opened in the project's bug tracking system just a few days ago that also described this.</p>
<p>The solution, at the moment, is to simply create a specific converter for my Enum types that is a <code>SingleValueConverter</code>. The code for this is below:</p>
<blockquote class="code"><p>public class SingleValueEnumConverter extends AbstractSingleValueConverter {</p>
<p>    private final Class enumType;</p>
<p>    public SingleValueEnumConverter(Class type) {<br />
        this.enumType = type;<br />
    }</p>
<p>    public boolean canConvert(Class c) {<br />
        return c.equals(enumType);<br />
    }</p>
<p>    public Object fromString(String value) {<br />
        return Enum.valueOf(enumType, value);<br />
    }<br />
}</p></blockquote>
<p>And to modify the XStream example above:</p>
<blockquote class="code"><p>XStream xs = new XStream();<br />
xs.alias("cards", List.class);<br />
xs.alias("card", Card.class);<br />
xs.useAttributeFor("val", int.class);<br />
xs.useAttributeFor("type", Type.class);</p>
<p>xs.registerConverter(new SingleValueEnumConverter(Card.Type.class));</p>
<p>System.out.print(xs.toXML(cards));</p></blockquote>
<p>And now the XML looks like this:</p>
<blockquote class="code"><p>&lt;cards&gt;<br />
    &lt;card val="1" type="SPADES"/&gt;<br />
&lt;/cards&gt;</p></blockquote>
<p>So as you can see, this was exactly what I needed. I love coding in a world where we have such a large community of people willing to take a few minutes of their time to guide you in the right direction. Great job XStream!</p>
]]></content:encoded>
			<wfw:commentRss>http://everydaycoder.com/2006/12/08/enum-attribute-xstream.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Soft Clipping with Java 2D</title>
		<link>http://everydaycoder.com/2006/10/01/soft-clipping-java-2d.html</link>
		<comments>http://everydaycoder.com/2006/10/01/soft-clipping-java-2d.html#comments</comments>
		<pubDate>Sun, 01 Oct 2006 20:16:19 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://everydaycoder.com/2006/10/01/soft-clipping-java-2d.html</guid>
		<description><![CDATA[The current application that I'm coding uses an interface composed mostly of rounded rectangles. Initially it looked really cool. I later added transparency to some of the panels so that the parent panel's background would show through. On the Mac, it looked really great. I had since ran the application on Windows and a few [...]]]></description>
			<content:encoded><![CDATA[<p>The current application that I'm coding uses an interface composed mostly of rounded rectangles. Initially it looked really cool. I later added transparency to some of the panels so that the parent panel's background would show through. On the Mac, it looked really great. I had since ran the application on Windows and a few of my decisions regarding how I made the rounded rectangle backgrounds came back to haunt me.</p>
<p>With a little more testing, I discovered that the issues lied with anti-aliasing and clipping using <code>Graphics2D</code>. Since this issue was purely asthetic, I moved on to more pressing issues. Then, about a week later I got my monthly Tech Tips from Sun. Talk about reading my mind. The <a href="http://java.sun.com/mailers/techtips/corejava/2006/tt0923.html">Java 2D Trickery - Core Java Technologies Technical Tips</a> contains 2 excellent Java 2D tips. The first one titled <q><a href="http://java.sun.com/mailers/techtips/corejava/2006/tt0923.html#1">Java 2D Soft Clipping</a></q> was the answer I needed.</p>
<p>After about 5 minutes, the problem was resolved and it now looks great on both platforms. Excellent tip! Well done Chris Campbell! I suggest you check it out and subscribe if you haven't already done so.</p>
]]></content:encoded>
			<wfw:commentRss>http://everydaycoder.com/2006/10/01/soft-clipping-java-2d.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Our Old Friend: GridBagLayout</title>
		<link>http://everydaycoder.com/2006/09/28/old-friend-gridbaglayout.html</link>
		<comments>http://everydaycoder.com/2006/09/28/old-friend-gridbaglayout.html#comments</comments>
		<pubDate>Thu, 28 Sep 2006 20:22:23 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://everydaycoder.com/2006/09/28/old-friend-gridbaglayout.html</guid>
		<description><![CDATA[ I stumbled across this article about my old friend the GridBagLayout today. I for one thought I was in a very small minority when it comes to actually using and liking the GridBagLayout. Well, Jeff Friesen proved that he is also on my side when he wrote Harness the Power of Java's GridBagLayout. The [...]]]></description>
			<content:encoded><![CDATA[<p> I stumbled across this article about my old friend the GridBagLayout today. I for one thought I was in a very small minority when it comes to actually using and liking the GridBagLayout. Well, Jeff Friesen proved that he is also on my side when he wrote <a href="http://www.informit.com/articles/article.asp?p=607374">Harness the Power of Java's GridBagLayout</a>. The article is a very good <em>demystifier</em> for those non-believers out there.</p>
<p>Since this is the first article in a series, it really only covers the basics, but it promises even more. Jeff does say that <q cite="http://www.informit.com/articles/article.asp?p=607374">in case you're still not impressed with GridBagLayout, part 3 presents the JGoodies FormLayout alternative, and compares this layout manager with GridBagLayout so you can choose the layout manager that works best for you.</q> I can't wait. People are always telling me to switch to <a href="http://www.jgoodies.com/freeware/forms/">FormLayout</a>, but I've used it in the past and have not looked back. I just did not like dealing with those cryptic configuration strings (<code>"3dlu p d:fill 4f 5u"</code>) all the time. I look forward to the 3<sup>rd</sup> part of the series.</p>
<p>If your scratching your head over which LayoutManager to use for forms, I would recommend the GridBagLayout. Our old friend.</p>
]]></content:encoded>
			<wfw:commentRss>http://everydaycoder.com/2006/09/28/old-friend-gridbaglayout.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Effective Java: A Must Have</title>
		<link>http://everydaycoder.com/2006/08/30/effective-java-a-must-have.html</link>
		<comments>http://everydaycoder.com/2006/08/30/effective-java-a-must-have.html#comments</comments>
		<pubDate>Wed, 30 Aug 2006 15:37:00 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://everydaycoder.themossbunch.com/?p=6</guid>
		<description><![CDATA[Every Java developer must own a copy of Effective Java: Programming Language Guide by Joshua Bloch. If you haven't read this book and you think you're a good Java developer, think again. This book challenges me to be a better coder every day.
The layout of the book is extremely easy to follow and reference. The [...]]]></description>
			<content:encoded><![CDATA[<p>Every Java developer must own a copy of <a href="http://java.sun.com/docs/books/effective/">Effective Java: Programming Language Guide</a> by Joshua Bloch. If you haven't read this book and you think you're a good Java developer, think again. This book challenges me to be a better coder every day.</p>
<p>The layout of the book is extremely easy to follow and reference. The individual concepts are grouped into "Items" and those items are grouped into topics that are the chapters of the book. At the very least, every programmer needs to read the first 18 items. I'll give you a brief example of the power of this book.</p>
<blockquote><p>Item 7: Obey the general contract when overriding <code>equals</code></p>
</blockquote>
<p>Every Java developer tends to think he/she knows when and how to override the <code>Object.equals</code> method. This item explains the core concepts of the method and when and how to implement it. It's over 10 pages of explanation with code examples. I have used the techniques that Josh explained in almost every class I've written since I read this item. Before I read this book almost all of my <code>equals</code> methods failed to follow the <em>general contract</em>.</p>
<p>I could go on all day, but I know this isn't the place for that, so I really do hope that if you're a Java developer, you will own this book if you don't already. The full table of contents <a href="http://java.sun.com/docs/books/effective/toc.html">can be seen</a> on the book's site.</p>
]]></content:encoded>
			<wfw:commentRss>http://everydaycoder.com/2006/08/30/effective-java-a-must-have.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Persistence: An ongoing battle&#8230;</title>
		<link>http://everydaycoder.com/2006/08/24/java-persistence-an-ongoing-battle.html</link>
		<comments>http://everydaycoder.com/2006/08/24/java-persistence-an-ongoing-battle.html#comments</comments>
		<pubDate>Thu, 24 Aug 2006 02:34:00 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://everydaycoder.themossbunch.com/?p=8</guid>
		<description><![CDATA[This is my inaugural post. I guess I should hit the ground running with a post concerning database access from Java applications. I'm in the minority in that I'm currently writing a stand-alone Java application, not a JEE Enterprise Application. I know that this may be a shock to some of you, but let's just [...]]]></description>
			<content:encoded><![CDATA[<p>This is my inaugural post. I guess I should hit the ground running with a post concerning database access from Java applications. I'm in the minority in that I'm currently writing a stand-alone Java application, not a JEE Enterprise Application. I know that this may be a shock to some of you, but let's just keep moving.</p>
<p>I used to be in the Roll Your Own camp when it came to database persistence. I'd create a set of classes roughly separated along table boundaries. Those classes would call the classes in the <code>java.sql</code> package directly. This method seemed to work just fine, but it did take a lot of work to get all the code setup to convert <code>ResultSet</code>s to real objects. I actually used that method several times in the past with great success. My only regret is that my internal object model usually mapped exactly to my tables -- meaning that references were often represeted by id's rather than an actual object reference.</p>
<p>As new projects arose, I found myself searching for something better suited to persisting objects from my data model. My next attempt was to use <a href="http://hibernate.org/">Hibernate</a>. The most attractive feature that Hibernate offered was forced separation of the persistance layer and the Java object model. We eventually got Hibernate up and running and the project was done.</p>
<p>Hibernate left a bad taste in my mouth. It seemed that I spent a <strong>*lot*</strong> of time learning their object querying language as well as fiddling around with the XML mappings. I would much rather have spent the time adding additional features to the software. I know that one of the most attractive reasons that people use Hibernate is that the underlying database can change without too much effort. Sadly, this feature is flat-out usless to me. I have never once had management come to me and force me to change my DB.</p>
<p>I decided to be a little more drastic on my next project and I went with Prevayler. For those of you not familier with <a href="http://prevayler.org/">Prevayler</a>: it's an object persistence layer relying on Java's object serialization with added transaction support. I have to say that I was pretty enthusiastic after reading up on Prevayler. Prevayler turned out to be a good fit for that application. My only complaint is that I had to create a boat load of transaction objects. I ended up with 50+ classes that handle the individual transactions within the object model. So basically, if I wanted to change a user's first name, I would create a <code>ModifyUserTransaction</code> passing in the new field values and that would perform the modification. Seemed like a lot of work.</p>
<p>Prevayler also left a sour taste in my mouth. I just hated having a package full of classes that were basically one line-ers. Anyway, it did work and given my previous battle with Hibernate, I was happy to see that no XML configuration files were needed. Also, the objects were all stored as serialized java objects in a snapshot file that could easily have been read by another Java application.</p>
<p>Then came my current project. This time, I'm forced to use an SQL server backend. Mostly due to the fact that we will want to take advantage of the stability and host it in a centralized location. I know that a lot of you would have immediatly jumped for the 3-tier JEE architecture. I, myself, chose to do an old-fashioned client-server application. This application will ultimatly communicate via JDBC to the native protocol for the database.</p>
<p>For this attempt, I've deviced to use the <a href="http://springframework.org/">SpringFramework</a>'s JDBC API. After much reading this seems like a good choice. I'll let ya'll know how it turns out.</p>
<p>After reading back over this post, I realize that it may have rambled a bit, but I'll leave it as-is. After all, I'm just a lazy programmer.</p>
]]></content:encoded>
			<wfw:commentRss>http://everydaycoder.com/2006/08/24/java-persistence-an-ongoing-battle.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
