<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>JavaWorld's Daily Brew</title>
  <subtitle>Starting conversations in the Java developer community</subtitle>
  <link rel="alternate" type="text/html" href="http://www.javaworld.com/community"/>
  <link rel="self" type="application/atom+xml" href="http://www.javaworld.com/community/atom/feed"/>
  <id>http://www.javaworld.com/community/atom/feed</id>
  <updated>2013-04-16T12:00:01-04:00</updated>
  <entry>
    <title>ElasticSearch on EC2 in less than 60 seconds</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13376" />
    <id>http://www.javaworld.com/community/node/13376</id>
    <published>2013-05-17T17:24:00-04:00</published>
    <updated>2013-05-17T22:03:23-04:00</updated>
    <author>
      <name>Andrew Glover</name>
    </author>
    <summary type="html"><![CDATA[<p><img class="left" src="http://aglover.github.com/images/mine/es-bonzai.jpg" />Curious to see what all the <a href="http://www.elasticsearch.org/">ElasticSearch</a> hubbub is about? Wanna see it in action without a lot of elbow grease? Then look no further, friend &#8211; in less than 60 seconds, I&#8217;ll show you how to install <a href="http://www.ibm.com/developerworks/java/library/j-javadev2-24/">ElasticSearch</a> on an <a href="http://aws.amazon.com/">AWS AMI</a>.</p>
    ]]></summary>
    <content type="html"><![CDATA[<p><img class="left" src="http://aglover.github.com/images/mine/es-bonzai.jpg" />Curious to see what all the <a href="http://www.elasticsearch.org/">ElasticSearch</a> hubbub is about? Wanna see it in action without a lot of elbow grease? Then look no further, friend &#8211; in less than 60 seconds, I&#8217;ll show you how to install <a href="http://www.ibm.com/developerworks/java/library/j-javadev2-24/">ElasticSearch</a> on an <a href="http://aws.amazon.com/">AWS AMI</a>.</p>
<p>You&#8217;ll first <a href="http://www.drdobbs.com/web-development/getting-started-with-the-cloud-amazon-we/231601598">need an AWS account</a> along with an SSH key pair. If you don&#8217;t already have those two steps done, go ahead and do that. The steps that follow suggest a particular AMI; however, you are free to select the <a href="http://aws.amazon.com/ec2/instance-types/">instance type</a>. <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html">Micro instance types</a> are free to use; consequently, you can get <a href="http://thediscoblog.com/blog/2013/05/14/the-democratization-of-search/">up and running with ElasticSearch</a> in less than a minute <em>for free</em>.</p>
<p><!-- more --></p>
<p>Now that you&#8217;ve got an <a href="http://www.ibm.com/developerworks/web/library/j-s3/">AWS</a> account and an SSH key pair, go ahead and create a new security group (or edit an existing one). It&#8217;s important that the <a href="http://www.elasticsearch.org/tutorials/elasticsearch-on-ec2/">following ports</a> are open:</p>
<ul>
<li>22 (required for SSH)</li>
<li>80 (ElasticSearch uses HTTP for standard API calls)</li>
<li>9200 (required for ElasticSearch)</li>
<li>9300 (required for ElasticSearch)</li>
</ul>
<p>Next, fire up a Linux AMI. I, for example, prefer <a href="http://cloud-images.ubuntu.com/locator/ec2/">ami-c30360aa</a> (this is Ubuntu Server version 13.04) and I configure the AMI to use the security group that I just covered.</p>
<p>Now, SSH to your newly instantiated AMI.  Once on the AMI, you&#8217;ll need to install Java. Never fear though, I&#8217;ve got you covered. All you need to do is run a handy script via the <a href="https://github.com/aglover/ubuntu-equip">Ubuntu-Equip project</a>, that I use frequently just for this sorta thing:</p>
<p><figure class='code'><figcaption><span>installing Java</span></figcaption><br />
<div class="highlight">
<table>
<tr>
<td class="gutter">
<pre class="line-numbers"><span class='line-number'>1</span>
</pre></td>
<td class='code'>
<pre><code class='bash'><span class='line'>wget --no-check-certificate <a href="https://github.com/aglover/ubuntu-equip/raw/master/equip_java.sh" title="https://github.com/aglover/ubuntu-equip/raw/master/equip_java.sh">https://github.com/aglover/ubuntu-equip/raw/master/equip_java.sh</a> <span class="o">&amp;&amp;</span> bash equip_java.sh
</span></code></pre></td>
</tr>
</table>
</div>
<p></figure></p>
<p>You&#8217;ll need to accept the license from Oracle. Once that script completes, go ahead and type  <code>java -version</code> and you should see Oracle&#8217;s JDK (i.e Java version &#8220;1.7.0_21&#8221;).</p>
<p>Next, download and install ElasticSearch via another nifty <a href="https://github.com/aglover/ubuntu-equip">Ubuntu-Equip</a> script:</p>
<p><figure class='code'><figcaption><span>installing elasticsearch</span></figcaption><br />
<div class="highlight">
<table>
<tr>
<td class="gutter">
<pre class="line-numbers"><span class='line-number'>1</span>
</pre></td>
<td class='code'>
<pre><code class='bash'><span class='line'>wget --no-check-certificate <a href="https://github.com/aglover/ubuntu-equip/raw/master/equip_elasticsearch.sh" title="https://github.com/aglover/ubuntu-equip/raw/master/equip_elasticsearch.sh">https://github.com/aglover/ubuntu-equip/raw/master/equip_elasticsearch.s...</a> <span class="o">&amp;&amp;</span> bash equip_elasticsearch.sh
</span></code></pre></td>
</tr>
</table>
</div>
<p></figure></p>
<p>This script doesn&#8217;t start ElasticSearch for you; thus, go ahead and change directories into the <code>elasticsearch</code> directory and fire it up like so:</p>
<p><figure class='code'><figcaption><span>starting elasticsearch</span></figcaption><br />
<div class="highlight">
<table>
<tr>
<td class="gutter">
<pre class="line-numbers"><span class='line-number'>1</span>
</pre></td>
<td class='code'>
<pre><code class='bash'><span class='line'>~/elasticsearch<span class="nv">$ </span>bin/elasticsearch -f
</span></code></pre></td>
</tr>
</table>
</div>
<p></figure></p>
<p>Take a deep breath (but not too deep, as I need you to finish in less than 60 seconds) and find the Public DNS of the AMI you&#8217;ve been working on. Go ahead and copy it, then fire up a browser on your local machine and go to <a href="http://YOUR_AMI_DNS_NAME.com:9200/_plugin/inquisitor/" title="http://YOUR_AMI_DNS_NAME.com:9200/_plugin/inquisitor/">http://YOUR_AMI_DNS_NAME.com:9200/_plugin/inquisitor/</a> (be sure to note the port).</p>
<p>By the way, <a href="https://github.com/polyfractal/elasticsearch-inquisitor">Inquisitor</a> is a handy web application that lets you query your indexes. It was installed via the Ubuntu-Equip script &#8211; this tool is invaluable in figuring out how to properly query your indexes.</p>
<p>And that is it. In less than 60 seconds you&#8217;ve got ElasticSearch running in the cloud for you. Want to create a cluster? No problem, just follow these steps again to fire up another ElasticSearch instance and then <a href="http://www.elasticsearch.org/videos/three-nodes-and-one-cluster/">configure the cluster accordingly</a>.</p>
<p>I&#8217;ve not gone over <a href="http://www.elasticsearch.org/guide/reference/setup/configuration/">configuring ElasticSearch</a> nor have I showed you how to create ElasticSearch as a service on a Linux instance, but for one minute, what do you expect?</p>
    ]]></content>
  </entry>
  <entry>
    <title>Mobile-isticly optimized in 10 seconds</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13375" />
    <id>http://www.javaworld.com/community/node/13375</id>
    <published>2013-05-15T13:20:00-04:00</published>
    <updated>2013-05-17T22:03:23-04:00</updated>
    <author>
      <name>Andrew Glover</name>
    </author>
    <summary type="html"><![CDATA[<p><img class="right" src="http://aglover.github.com/images/mine/unoptmobile.png" />Anyone ever told you that your website isn&#8217;t mobile optimized? Or have you ever seen a lilliputian-looking website on your device? You know, one that renders so small you are forced to squint as you enlarge various parts of the site with your fingers just to read it?</p>
    ]]></summary>
    <content type="html"><![CDATA[<p><img class="right" src="http://aglover.github.com/images/mine/unoptmobile.png" />Anyone ever told you that your website isn&#8217;t mobile optimized? Or have you ever seen a lilliputian-looking website on your device? You know, one that renders so small you are forced to squint as you enlarge various parts of the site with your fingers just to read it?</p>
<p>Websites render this way on mobile devices because they lack a simple <code>meta</code> tag. While the subject of mobile website optimization can be rather involved (especially when dealing with <a href="http://css-tricks.com/css-media-queries/">CSS media queries</a>, which take longer than 10 seconds to understand), there is a simple trick that can at least can make your website render normally on a mobile device. And it can be done in 10 seconds.</p>
<p><!--more--></p>
<p>Simply add the following <code>meta</code> tag in the <code>head</code> element of your website&#8217;s index page:</p>
<p><figure class='code'><figcaption><span>viewport definition</span></figcaption><br />
<div class="highlight">
<table>
<tr>
<td class="gutter">
<pre class="line-numbers"><span class='line-number'>1</span>
</pre></td>
<td class='code'>
<pre><code class='html'><span class='line'><span class="nt">&lt;meta</span> <span class="na">name=</span><span class="s">&quot;viewport&quot;</span> <span class="na">content=</span><span class="s">&quot;width=device-width, initial-scale=1&quot;</span><span class="nt">&gt;</span>
</span></code></pre></td>
</tr>
</table>
</div>
<p></figure></p>
<p>The viewport <code>meta</code> tag is supported by browsers on both iOS, Android, and by other device browsers including Internet Explorer Mobile on Windows Phone 8. This tag instructs a browser on how to properly display a webpage; without it, a webpage is, unfortunately, displayed mini-style on device browsers, which have a narrow width.</p>
<p>Thus, the viewport tag essentially zooms in the display of a webpage. In the case of the example tag above, the width of the website is set to the device&#8217;s width and the scale is set to 100% &#8211; this&#8217;ll allow the website to be displayed normally on a mobile device. Website visitors won&#8217;t have to squint or pinch and expand to just to read the site&#8217;s relevant text.</p>
<p><a href="http://www.paulund.co.uk">Paulund.co.uk</a> has a really <a href="http://www.paulund.co.uk/understanding-the-viewport-meta-tag">good write up</a> regarding the usage of the viewport <code>meta</code> tag as well as CSS media queries; what&#8217;s more, the good folks who created <a href="http://html5boilerplate.com/mobile/">HTML5boilerplate.com</a> have a <a href="http://t.co/dKP3o1e">nifty presentation</a> that&#8217;s worth a read too.</p>
    ]]></content>
  </entry>
  <entry>
    <title>The democratization of search</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13374" />
    <id>http://www.javaworld.com/community/node/13374</id>
    <published>2013-05-14T11:33:00-04:00</published>
    <updated>2013-05-17T22:03:23-04:00</updated>
    <author>
      <name>Andrew Glover</name>
    </author>
    <summary type="html"><![CDATA[<p><img class="right" src="http://aglover.github.com/images/mine/democracy.jpg" />Over the past year and a half, I&#8217;ve watched <a href="http://www.elasticsearch.org/">ElasticSearch</a> grow from a seemingly part-time code experiment into a thriving ecosystem.</p>
    ]]></summary>
    <content type="html"><![CDATA[<p><img class="right" src="http://aglover.github.com/images/mine/democracy.jpg" />Over the past year and a half, I&#8217;ve watched <a href="http://www.elasticsearch.org/">ElasticSearch</a> grow from a seemingly part-time code experiment into a thriving ecosystem. Not only has the number of <a href="http://www.elasticsearch.org/guide/clients/">client libraries</a> grown from 1 to over 25 (and counting!); it&#8217;s now a <a href="http://www.elasticsearch.com/">commercially sponsored project</a> to the tune of <a href="http://www.whiteboardmag.com/youre-hot-or-not-why-elasticsearch-raised-24-million-just-3-months-after-a-10-million-round/">$34 million</a> (a <a href="http://www.zdnet.com/elasticsearch-raises-24-million-big-data-analytics-7000011496/">$10M series A</a> and a <a href="http://gigaom.com/2013/02/19/open-source-search-tool-elasticsearch-gets-24m/">$24M series B</a>) with 200,000 downloads a month.</p>
<p>Search is the touchstone of the Internet; without search the Internet wouldn&#8217;t be all that useful. Google&#8217;s meteoric rise and its resultant eponymous name for search is clear evidence as to the importance of search. Search, however, hasn&#8217;t always been that easy nor affordable to implement.</p>
<p><!--more--></p>
<p>Before the likes of open source projects like <a href="http://lucene.apache.org/">Lucene</a>, implementing search in an application involved an expensive commercial product or a series of SQL <code>like</code> statements that were never really good enough. Lucene, however, isn&#8217;t a simple hobbyist&#8217;s pursuit. Lucene requires a lot of expertise; what&#8217;s more, successful projects within the Lucene ecosystem, like <a href="http://lucene.apache.org/solr/">Solr</a> lack a key feature that is defining modern architectures: <a href="http://www.searchblox.com/solr-vs-elasticsearch">distributed</a>.</p>
<p>The continued low cost of storage combined with cheap rent-able infrastructures like <a href="http://www.drdobbs.com/web-development/getting-started-with-the-cloud-amazon-we/231601598">AWS</a> has made it convenient to store a plethora of data, thus bringing to bear the importance that applications must support searching vast amounts of data, do it quickly, and affordably. This is where <a href="http://www.ibm.com/developerworks/java/library/j-javadev2-24/">ElasticSearch shines</a>.</p>
<p><a href="http://www.elasticsearch.org/overview/">ElasticSearch sits on top of Lucene</a> and adds not only a simple API for adding and searching content, but does it in a distributed manner. With infinitesimal arm grease, you can set up a search cluster that smears your data and resultant queries across a series of nodes. Not only is this resultant architecture fast, but it&#8217;s easy to set up and extremely affordable as search nodes can run on commodity hardware. In essence, ElasticSearch brings search to the masses.</p>
<p>Google and its resultant ease of search has changed the mindset of application users. Searching content is a <em>presumed feature</em> and if you don&#8217;t provide it, you&#8217;re already a few steps behind your competition. ElasticSearch is clearly a path to constructing a viable, easy to install, affordable, distributed search infrastructure for any application. If you don&#8217;t believe me, take a look at some of the innovative companies with substantial amounts of users using it: <a href="https://github.com/">Github</a>, <a href="https://foursquare.com/">foursquare</a>, and <a href="http://stackoverflow.com/">Stack Overflow</a> are just a few.</p>
    ]]></content>
  </entry>
  <entry>
    <title>On Languages, VMs, Optimization, and the Way of the World</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13373" />
    <id>http://www.javaworld.com/community/node/13373</id>
    <published>2013-05-11T06:05:00-04:00</published>
    <updated>2013-05-18T02:00:07-04:00</updated>
    <author>
      <name>Charles Nutter</name>
    </author>
    <summary type="html"><![CDATA[<p dir="ltr" style="text-align: left;" trbidi="on">
<p style="color: #222222; font-family: arial; font-size: small;">I shouldn't be up this late, but I've been doing lots of thinking and exploring tonight.</p>
    ]]></summary>
    <content type="html"><![CDATA[<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="color: #222222; font-family: arial; font-size: small;">I shouldn't be up this late, but I've been doing lots of thinking and exploring tonight.</div>
<div style="color: #222222; font-family: arial; font-size: small;">In studying various VMs over the past few years, I've come up with a list of do's and don't that make things optimize right. These apply to languages, the structures that back them, and the VMs that optimize those languages, and from what I've seen there's a lot of immutable truths here given current optimization technology.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Let's dive in.</p>
</div>
<h3 style="text-align: left;">#1: Types don't have to be static</h3>
<div style="color: #222222; font-family: arial; font-size: small;">JVM and other dynamic-optimizing runtimes have proven this out. At runtime, it's possible to gather the same information static types would provide you at compile time, leading to optimizations at least as good as fully statically-typed, statically-optimized code. In some cases, it may be possible to do a better job, since runtime profiling is based on real execution, real branch percentages, real behavior, rather than a guess at what a program might do. You could probably make the claim that static optimization is a halting problem, and dynamic optimization eventually can beat it by definition since it can optimize what the program is actually doing.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">However, this requires one key thing to really work well.</p>
</div>
<h3 style="text-align: left;">#2: Types need to be predictable</h3>
<div style="color: #222222; font-family: arial; font-size: small;">In order for runtime optimization to happen, objects need to have predictable types and those types need to have a predictable structure. This isn't to say that types must be statically declared...they just need to look the same on repeat visits. If objects can change type (smalltalk's become, perl's and C's weak typing) you're forced to include more guards against those changes, or you're forced to invalidate more code whenever something changes (or in the case of C, you just completely shit the bed when things aren't as expected). If change is possible and exposed at a language level, there may be nothing you can do to cope with all those different type shapes, and optimization can only go so far.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">This applies both to the shape of a type's method table (methods remaining consistent once encountered) and the shape of the type's instances (predictable object layout). Many dynamically-typed languages impose dynamic type shape and object shape on VMs that run them, preventing those VMs from making useful predictions about how to optimize code. Optimistic predictions (generating synthetic types for known type shapes or preemptively allocating objects based on previously-seen shapes) still have to include fallback logic to maintain the mutable behavior, should it ever be needed. Again, optimization potential is limited, because the shape of the world can change on a whim and the VM has to be vigilent</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">The alternative summation of #1 and #2 is that types don't have to be statically declared, but they need to be statically defined. Most popular dynamic languages do neither, but all they really need to do is the latter.</p>
</div>
<h3 style="text-align: left;">#3: You can't cheat the CPU</h3>
<div style="color: #222222; font-family: arial; font-size: small;">Regardless of how clever you'd like to be in your code or language or VM or JIT, the limiting factor is how modern CPUs actually run your code. There's a long list of expectations you must meet to squeeze every last drop of speed out of a system, and diverging from those guidelines will always impose a penalty. This is the end...the bottom turtle...the unifying theory. It is, at the end of the day, the CPU you must appease to get the best performance. All other considerations fall out of that, and anywhere performance does not live up to expectations you are guaranteed to discover that someone tried to cheat the CPU.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Traditionally, static typing was the best way to guarantee we produced good CPU instructions. It gave us a clear picture of the world we could ponder and meditate over, eventually boiling out the secrets of the universe and producing the fastest possible code. But that always assumed a narrow vision of a world with unlimited resources. It assumed we could make all the right decisions for a program ahead of time and that no limitations outside our target instruction set would ever affect us. In the real world, however, CPUs have limited cache sizes, multiple threads, bottlenecked memory pipelines, and basic physics to contend with (you can only push so many electrons through a given piece of matter without blowing it up). Language and VM authors ignore the expectations of their target systems only at great peril.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Let's look at a few languages and where they fit.</p>
</div>
<h3 style="text-align: left;">Language Scorecard</h3>
<div style="color: #222222; font-family: arial; font-size: small;">Java is statically typed and types are of a fixed shape. This is the ideal situation mostly because of the type structure being predictable. Once encountered, a rose is just a rose. Given appropriate dynamic optimizations, there's no reason Java code can't compete with or surpass statically-typed and statically-compiled C/++, and in theory there's nothing preventing Java code from becoming optimal CPU instructions.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Dart is dynamically typed (or at least, types are optional and the VM doesn't care about them), but types are of a fixed shape. If programmers can tolerate fixed-shape types, Dart provides a very nice dynamic language that still can achieve the same optimizations as statically-typed Java or statically-compiled C/++.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Groovy is dynamically typed with some inference and optimization if you specify static types, but most (all?) types defined in Groovy are not guaranteed to be a fixed shape. As a result, even when specifying static types, guards must be inserted to check that those types' shapes have not changed. Groovy does, however, guarantee object shape is consistent over time, which avoids overhead from being able to reshape objects at runtime.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Ruby and JavaScript are dynamically typed and types and objects can change shape at runtime. This is a confluence of all the hardest-to-optimize language characteristics. In both cases, the best we can do is to attempt to predict common type and object shapes and insert guards for when we're wrong, but it's not possible to achieve the performance of a system with fully-predictable type and object shapes. Prove me wrong.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Now of course when I say it's not possible, I mean it's not possible for the general case. Specific cases of a known closed-world application can indeed be optimized as though the types and objects involved had static shapes. I do something along these lines in my RubyFlux compiler, which statically analyzes incoming Ruby code and assumes the methods it sees defined and the fields it sees accessed will be the only methods and fields it ever needs to worry about. But that requires omitting features that can mutate type and object structure, or else you have to have a way to know which types and objects those features will affect. Sufficiently smart compiler indeed.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Python has similar structural complexities to Ruby and adds in the additional complexity of an introspectable call stack. Under those circumstances, even on-stack execution state is not safe; a VM can't even make guarantees about the values it has in hand or the shape of a given call's activation. PyPy does an admirable job of attacking this problem by rewriting currently-running code and lifting on-stack state to the heap when it is accessed, but this approach prevents dropping unused local state (since you can't predict who might want to see it) and also fails to work under parallel execution (since you can't rewrite code another thread might be executing). Again, the dynamicity of a "cool" feature brings with it intrinsic penalties that are reducible but not removable.</p>
</div>
<h3 style="text-align: left;">Get to the Damn Point, Already</h3>
<div style="color: #222222; font-family: arial; font-size: small;">So what am I trying to say in all this? I started the evening by exploring a benchmark post comparing Dart's VM with JVM on the same benchmark. The numbers were not actually very exciting...with a line-by-line port from Dart to Java, Java came out slightly behind Dart. With a few modifications to the Java code, Java pulled slightly ahead. With additional modifications to the Dart code, it might leapfrog Java again. But this isn't interesting because Dart and Java can both rely on type and object shapes remaining consistent, and as a result the optimizations they perform can basically accomplish the same thing. Where it matters, they're similar enough that VMs don't care about the differences.</div>
<div style="color: #222222; font-family: arial; font-size: small;"></div>
<div style="color: #222222; font-family: arial; font-size: small;">Where does this put languages I love, like Ruby? It's probably fair to concede that Ruby can't ever achieve the raw, straight-line performance of type-static (not statically-typed) languages like Dart or Java, regardless of the VM technologies involved. We'll be able to get close; JRuby can, with the help of invokedynamic, make method calls *nearly* as fast as Java calls, and by generating type shapes we can make object state *nearly* as predictable as Java types, but we can't go all the way. Regardless of how great the underlying VM is, if you can't hold to its immutable truths, you're walking against the wind. Ruby on Dart would probably not be any faster than Ruby on JVM, because you'd still have to implement mutable types and growable objects in pretty much the same way. Ruby on PyPy might be able to go farther, since the VM is designed for mutable types and growable objects, but you might have to sacrifice parallelism or accept that straight-line object-manipulating performance won't go all the way to a Java or Dart. Conversely, languages that make those type-static guarantees might be able to beat dynamic languages when running on dynamic language VMs (e.g. dart2js) for exactly the same reasons that they excel on their own VMs: they provide a more consistent view of the world, and offer no surprises to the VM that would hinder optimization. You trade dynamicity at the language level for predictability at the VM level.</p>
</div>
<h3 style="text-align: left;">The Actual Lesson</h3>
<div style="color: #222222; font-family: arial; font-size: small;">I guess the bottom line for me is realizing that there's always going to be a conflict between what programmers want out of programming languages and what's actually possible to give them. There's no magical fairy world where every language can be as fast as every other language, because there's no way to predict how every program is going to execute (or in truth, how a given program is going to execute given a general strategy). And that's ok; most of these languages can still get very close to each other in performance, and over time the dynamic type/object-shaped languages may offer ways to ratchet down some of that dynamism...or they might not care and just accept what limitations result. The important thing is for language users to recognize that nothing is free, and to understand the implications of language features and design decisions they make in their own programs.</div>
</div>
    ]]></content>
  </entry>
  <entry>
    <title>Hold your horses! It&#039;s still only a two horse race.</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13372" />
    <id>http://www.javaworld.com/community/node/13372</id>
    <published>2013-05-09T12:49:00-04:00</published>
    <updated>2013-05-17T22:03:23-04:00</updated>
    <author>
      <name>Andrew Glover</name>
    </author>
    <summary type="html"><![CDATA[<!--paging_filter--><p>There has been a lot of excitement in recent months regarding a few new entrants to the mobile operating systems arena including <a href="http://www.mozilla.org/en-US/firefox/partners/" rel="nofollow" rel="nofollow">Firefox OS</a>, <a href="https://www.tizen.org/" rel="nofollow" rel="nofollow">Tizen</a>, and <a href="https://wiki.ubuntu.com/Touch" rel="nofollow" rel="nofollow">Ubuntu Touch</a>.</p>
    ]]></summary>
    <content type="html"><![CDATA[<!--paging_filter--><p>There has been a lot of excitement in recent months regarding a few new entrants to the mobile operating systems arena including <a href="http://www.mozilla.org/en-US/firefox/partners/" rel="nofollow">Firefox OS</a>, <a href="https://www.tizen.org/" rel="nofollow">Tizen</a>, and <a href="https://wiki.ubuntu.com/Touch" rel="nofollow">Ubuntu Touch</a>. These relatively nascent projects, which are all built on top of a Linux kernel, appear to have fully embraced HTML 5 apps; indeed, HTML 5 apps on these operating systems are first class citizens that can run without a browser. Moreover, the ubiquity of HTML 5 means the same app can run on all 3 of these operating systems plus Android and iOS!</p>
<p>What&#8217;s also exciting is that there are some major players behind these projects &#8211; Mozilla is, of course behind Firefox OS, Canonical is behind Ubuntu Touch, and quite a few large players are behind Tizen, including Samsung. <a href="http://www.forbes.com/sites/haydnshaughnessy/2013/04/13/how-mozillas-firefox-os-will-open-up-the-smartphone-market/" rel="nofollow">Firefox OS even appears to be courting carriers with an opportunity to run their own app stores</a>. This app store opportunity naturally has carriers foaming at the mouth to re-assert some semblance of control: behold the Verizon app store! All apps are $0.99 and we&#8217;ll just bill you at the end of the month (like we used to do for all those ringtones you were accustomed to download back when we were in control of all things device-related before Apple destroyed that cash cow).</p>
<p>Naturally, we&#8217;re all eager to see how these new entrants fair. It reminds me of how interested I was in learning more about <a href="http://www.windowsphone.com/en-us/how-to/wp8/start/whats-new-in-windows-phone" rel="nofollow">Windows Phone 8</a> and even <a href="http://global.blackberry.com/blackberry-10.html" rel="nofollow">Blackberry&#8217;s latest and greatest attempt</a> at regaining some composure.</p>
<p>But then I remember this stunning fact: Apple and Google</p>
<p>together control nearly 9 out of 10 smartphones sold globally.</p>
<p><strong>Tomi Ahonen</strong> <cite><a href="http://www.forbes.com/sites/haydnshaughnessy/2013/04/09/ios-vs-android-can-competitors-break-apple-and-googles-stranglehold/" rel="nofollow">Can These Competitors Break Apple and Google&#8217;s Stranglehold on the Mobile OS?</a></cite></p>
<p>Even with industry heavyweight contenders (who are not new to mobile!) like <a href="http://www.windowsphone.com/en-us" rel="nofollow">Microsoft</a> and <a href="http://us.blackberry.com/" rel="nofollow">Blackberry</a> (who, we all should remember, <em>owned</em> the smartphone market before Apple got involved) haven&#8217;t really made a dent. Granted, it&#8217;s early to rush to conclusions, however, preliminary signs don&#8217;t bode well for either company.</p>
<p>The co-CEO of Samsung had some stinging criticism for Microsoft:</p>
<p>Smartphones and tablets based on Microsoft&#8217;s Windows operating system aren&#8217;t selling very well. There is a preference in the market for Android. In Europe, we&#8217;re also seeing lackluster demand for Windows-based products.</p>
<p><strong>JK Shin</strong> <cite><a href="http://www.theverge.com/2013/3/15/4106980/samsung-ceo-jk-shin-says-windows-isnt-selling-very-well" rel="nofollow">New Samsung CEO Says There Is &#8216;Lackluster Demand&#8217; for Windows Tablets and Phones</a></cite></p>
<p>Blackberry appears to have some demand in the UK and Canada, a recently analyst T. Michael Walkley had this to say:</p>
<p>While we anticipate stronger near-term results for BlackBerry as higher margin BB10 smartphones sell into the channel, we do not believe BlackBerry can achieve sell-through market share levels to return to sustained profit levels</p>
<p><strong>T. Michael Walkley</strong> <cite><a href="http://business.financialpost.com/2013/05/06/blackberry-estimates-cut-despite-q10-strength-likely-offsetting-z10-weakness/" rel="nofollow">BlackBerry Sales Forecast Cut Despite Strong Demand for Q10</a></cite></p>
<p>The salient point, nevertheless, is that neither Microsoft or Blackberry have yet to make a dent into the Google-Apple duopoly. And so while it&#8217;s easy to get excited about new entrants like Ubuntu Touch, the reality, at least for the time being, is that these operating systems will have little impact in the smartphone market.</p>
<p>After all, when your competition essentially owns 9 out of every 10 smartphones, evolutionary enhancements aren&#8217;t enough to break that stranglehold: you have to do something <em>revolutionary</em>. Besides, we&#8217;ve already seen this before: the iPhone was not an evolutionary offshoot of the Blackberry. It was revolutionary. Sadly, neither Ubuntu Touch, Tizen, nor Firefox OS appear revolutionary.</p>
    ]]></content>
  </entry>
  <entry>
    <title>JDK 8&#039;s Calendar.Builder</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13371" />
    <id>http://www.javaworld.com/community/node/13371</id>
    <published>2013-05-08T10:04:00-04:00</published>
    <updated>2013-05-08T11:59:59-04:00</updated>
    <author>
      <name>Dustin Marx</name>
    </author>
    <summary type="html"><![CDATA[<!--paging_filter--><p>One of the defining characteristics of the <a href="http://jaxenter.com/5-reasons-to-be-excited-about-java-in-2013-45938.html" rel="nofollow" rel="nofollow">brave new world of Java</a> is the increasing prevalence of the builder pattern in the <a href="http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern" rel="nofollow" rel="nofollow">Java space</a>. <a href="http://groovy.codehaus.org/" rel="nofollow" rel="nofollow">Groovy</a>, which appears to be the most popular alternative language (to Java) on the JVM, is well-known for its heavy use of the Builder in both the core libraries and in Groovy-supported libraries and frameworks.</p>
    ]]></summary>
    <content type="html"><![CDATA[<!--paging_filter--><p>One of the defining characteristics of the <a href="http://jaxenter.com/5-reasons-to-be-excited-about-java-in-2013-45938.html" rel="nofollow">brave new world of Java</a> is the increasing prevalence of the builder pattern in the <a href="http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern" rel="nofollow">Java space</a>. <a href="http://groovy.codehaus.org/" rel="nofollow">Groovy</a>, which appears to be the most popular alternative language (to Java) on the JVM, is well-known for its heavy use of the Builder in both the core libraries and in Groovy-supported libraries and frameworks. <a href="https://twitter.com/joshbloch" rel="nofollow">Josh Bloch</a> brought the pattern to the forefront of Java developer community mindset with coverage of the <a href="http://stackoverflow.com/questions/5007355/builder-pattern-in-effective-java" rel="nofollow">pattern</a> in <a href="http://www.informit.com/articles/article.aspx?p=1216151&amp;seqNum=2" rel="nofollow">Item #2</a> of the second edition of his highly influential <a href="http://www.informit.com/store/effective-java-9780321356680" rel="nofollow">Effective Java</a>. There have been several builders added to the JDK including the addition of <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Locale.Builder.html" rel="nofollow">Locale.Builder</a> in <a href="http://docs.oracle.com/javase/7/docs/api/" rel="nofollow">J2SE 1.7</a>. In this post, I briefly introduce <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.Builder.html" rel="nofollow">Calendar.Builder</a> coming to <a href="http://download.java.net/jdk8/docs/api/" rel="nofollow">JDK 8</a>.</p>
<p>Today, a Java developer typically populates an instance of the <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html" rel="nofollow">Calendar class</a> by either calling one of the "set" methods that accepts a lengthy list of content for the instance or by calling individual "set" methods on the instance one after another. These two typical approaches for populating a <code>Calendar</code> instance are demonstrated in the next two code listings.</p>
<p><strong>Populating Calendar with Single 'set' Method</strong></p>
<p>/**<br />
 * Demonstrate pre-JDK 8 method of instantiating Calendar instance using<br />
 * "set" method for main fields.<br />
 */<br />
public static void demoCalendarWithSingleSet()<br />
{<br />
   final Calendar calendar =<br />
      Calendar.getInstance(TimeZone.getTimeZone(timeZoneId),<br />
         ENGLISH);<br />
   calendar.set(2013, APRIL, 6, 15, 45, 22);<br />
   out.println("Calendar via Constructor: " + stringifyCalendar(calendar));<br />
}</p>
<p><strong>Populating Calendar with Multiple Individual 'set' Methods</strong></p>
<p>/**<br />
 * Demonstrate pre-JDK 8 method of instantiating Calendar instance using<br />
 * individual "set" calls for each pair of field names and values.<br />
 */<br />
public static void demoCalendarWithIndividualSets()<br />
{<br />
   final Calendar calendar =<br />
      Calendar.getInstance(<br />
         TimeZone.getTimeZone(timeZoneId),<br />
         ENGLISH);<br />
   calendar.set(YEAR, 2013);<br />
   calendar.set(MONTH, APRIL);<br />
   calendar.set(DATE, 6);<br />
   calendar.set(HOUR, 3);<br />
   calendar.set(MINUTE, 45);<br />
   calendar.set(SECOND, 22);<br />
   calendar.set(AM_PM, PM);<br />
   out.println("Calendar via set methods: " + stringifyCalendar(calendar));<br />
}</p>
<p>SIDE NOTE: In both of the examples above, I used another <a href="http://marxsoftware.blogspot.com/2012/04/are-static-imports-becoming.html" rel="nofollow">increasingly popular feature</a> of modern Java: the <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html" rel="nofollow">static import</a>. The constants such as <a href="http://download.java.net/jdk8/docs/api/java/util/Locale.html#ENGLISH" rel="nofollow">ENGLISH</a>, <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html#YEAR" rel="nofollow">YEAR</a>, and <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html#SECOND" rel="nofollow">SECOND</a> are actually statically imported from classes such as <a href="http://download.java.net/jdk8/docs/api/java/util/Locale.html" rel="nofollow">Locale</a> and <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html" rel="nofollow">Calendar</a>. As I have <a href="http://marxsoftware.blogspot.com/2012/04/are-static-imports-becoming.html" rel="nofollow">previously written</a>, static imports seem to be increasingly popular with Java developers, especially in light of the trend toward fluent interfaces.</p>
<p>The two "traditional" approaches shown above show different ways to populate the <code>Calendar</code> instance. One extreme is to set each individual field separately while the other is to set all the significant fields with a single "set" method. There are advantages to each approach. The single "set" method has fewer states of an "unfinished" object than the multiple-set approach, but the multiple-set approach is more readable because the name of the value being set is clear based on the first parameter to each "set" method. The single-set approach is a little unwieldy because it takes six integers that can be easily mixed up in order passed because there is no obvious way to differentiate which integer is which other than the implicit order.</p>
<p><a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.Builder.html" rel="nofollow">Calendar.Builder</a> leverages on the advertised benefits of the Builder as described by Bloch: removes the existence of "inconsistent states partway through [an object's] construction." This is demonstrated in the next code listing.</p>
<p><strong>Calendar.Builder Allows Single-Statement Instantiation with Readable Settings</strong></p>
<p>   /**<br />
    * Demonstrate using JDK 8's Calendar.Builder to instantiate an instance of<br />
    * Calendar using the set methods to set each field individually based on<br />
    * field name and value.<br />
    */<br />
   public static void demoCalendarWithCalendarBuilderSetFields()<br />
   {<br />
      final Calendar calendar =<br />
         new Calendar.Builder()<br />
            .set(YEAR, 2013)<br />
            .set(MONTH, APRIL)<br />
            .set(DATE, 6)<br />
            .set(HOUR, 15)<br />
            .set(MINUTE, 45)<br />
            .set(SECOND, 22)<br />
            .setTimeZone(TimeZone.getTimeZone(timeZoneId))<br />
            .setLocale(ENGLISH)<br />
            .build();<br />
      out.println(<br />
           "Calendar via Calendar.Builder 'set' Fields: "<br />
         + stringifyCalendar(calendar));<br />
   }</p>
<p>In the above code listing, the <code>Calendar</code> instance is created AND populated in one statement, removing the need to risk an object being in an inconsistent state across multiple statements. This example retains the readability of the traditional individual "set" methods approach [<a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.Builder.html#set(int,%20int)" rel="nofollow">set(int, int)</a>] with the added safety of having the object populated fully immediately at instantiation.</p>
<p>For developers who want to provide fewer individual "set" methods, another opportunity with <code>Calendar.Builder</code> is to use the <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.Builder.html#setDate(int,%20int,%20int)" rel="nofollow">setDate(int, int, int)</a> and <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.Builder.html#setTimeOfDay(int,%20int,%20int)" rel="nofollow">setTimeOfDay(int, int, int)</a> methods as demonstrated in the next code listing.</p>
<p><strong>Calendar.Builder Setting Date and Time as Two Calls</strong></p>
<p>/**<br />
 * Demonstrate using JDK 8's Calendar.Builder to instantiate an instance of<br />
 * Calendar using the "setDate" and "setTimeOfDay" builder methods.<br />
 */<br />
public static void demoCalendarWithCalendarBuilderSetDaySetTime()<br />
{<br />
   final Calendar calendar =<br />
      new Calendar.Builder()<br />
         .setDate(2013, APRIL, 6)<br />
         .setTimeOfDay(15, 45, 22)<br />
         .setTimeZone(TimeZone.getTimeZone(timeZoneId))<br />
         .setLocale(ENGLISH).build();<br />
   out.println(<br />
        "Calendar via Calendar.Builder setDate/setTimeOfDay: "<br />
      + stringifyCalendar(calendar));<br />
}</p>
<p>There are fewer characters and lines to type with this approach, but it partially reintroduces the disadvantage of being more likely to have an integer parameter inadvertently switched as each of the two methods takes three integers (or an overloaded version of <code>setTimeOfDay()</code> will take a fourth integer representing milliseconds).</p>
<p>For developers wanting the ultimate flexibility in specifying <code>Calendar</code> parameters during its instantiation, <code>Calendar.Builder</code> provides the method <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.Builder.html#setFields(int...)" rel="nofollow">setFields(int ...)</a> that takes an arbitrary length of pairs of integers with the first integer of the pair representing the field to be set and the second integer of the pair representing the value for that field. This method is used in the next code listing.</p>
<p><strong>Specifying Calendar Fields via Calendar.Builder's setFields Method</strong></p>
<p>   /**<br />
    * Demonstrate using JDK 8's Calendar.Builder to instantiate an instance of<br />
    * Calendar using the setFields method that allows providing of Calendar<br />
    * fields as key/value pairs.<br />
    */<br />
   public static void demoCalendarWithCalendarBuilderSetPairs()<br />
   {<br />
      final Calendar calendar =<br />
         new Calendar.Builder()<br />
            .setFields(YEAR, 2013, MONTH, APRIL, DATE, 6, HOUR, 15, MINUTE, 45, SECOND, 22)<br />
            .setTimeZone(TimeZone.getTimeZone("timeZoneId"))<br />
            .setLocale(ENGLISH)<br />
            .build();<br />
      out.println(<br />
           "Calendar via Calendar.Builder setPairs: "<br />
         + stringifyCalendar(calendar));<br />
   }</p>
<p>This <code>setFields(int ...)</code> method brings greater risk of mangling the order of integers used for instantiation of the new instance of <code>Calendar</code>, but using the statically imported <code>Calendar</code> constants does improve readability and reduces the likelihood of mixing the integers incorrectly. If an odd number of integers is provided (meaning that there is an incomplete pair), an <a href="http://download.java.net/jdk8/docs/api/java/lang/IllegalArgumentException.html" rel="nofollow">IllegalArgumentException</a> is thrown.</p>
<p>Although <code>Calendar.Builder</code> provides some convenience in instantiating and populating an instance of <code>Calendar</code>, anyone fortunate enough to adopt JDK 8 will have access to the <a href="http://openjdk.java.net/projects/threeten/" rel="nofollow">new date/time API</a> and so the question might be asked, "Why use Calendar.Builder?" Perhaps the best answer is that there are millions of lines of existing code, libraries, and frameworks out there using and expecting <code>Calendar</code> instances, so it will probably be a long time before the need for <code>Calendar</code> is completely gone (if ever). Fortunately, <code>Calandar.Builder</code> makes it easy to covert an instance of <a href="http://download.java.net/jdk8/docs/api/java/time/Instant.html" rel="nofollow">Instant</a> (part of the <a href="http://download.java.net/jdk8/docs/api/java/time/package-summary.html" rel="nofollow">new Java data/time API</a>) into a <code>Calendar</code> via <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.Builder.html#setInstant(long)" rel="nofollow">CalendarBulder.setInstant(long)</a>. This is demonstrated in the next code listing.</p>
<p><strong>Converting Instant to Calendar with Calendar.Builder</strong></p>
<p>   /**<br />
    * Demonstrate using JDK 8's Calendar.Builder to instantiate an instance of<br />
    * Calendar based on "now" Instant.<br />
    */<br />
   public static void demoCalendarWithCalendarBuilderInstant()<br />
   {<br />
      final Calendar calendar =<br />
         new Calendar.Builder().setInstant(Instant.now().toEpochMilli())<br />
            .setTimeZone(TimeZone.getTimeZone(timeZoneId))<br />
            .setLocale(ENGLISH)<br />
            .build();<br />
      out.println("Calendar via Calendar.Builder and Instant: " + stringifyCalendar(calendar));<br />
   }</p>
<p>Note that an overloaded version of the <code>setInstant</code> method accepts a <code>Date</code> for instantiating a <code>Calendar</code>. In both cases, whether instantiated with <code>setInstant(long)</code> or <code>setInstant(Date)</code>, no other "set" method on <code>Calender.Builder</code> should be called to avoid an <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalStateException.html" rel="nofollow">IllegalStateException</a>.</p>
<p>It is easy to go the other direction (getting an <code>Instant</code> from a <code>Calendar</code>) using <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html#toInstant()" rel="nofollow">Calendar.toInstant()</a>. Other methods introduced to <code>Calendar</code> with JDK 1.8 are related to providing the <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html#getCalendarType()" rel="nofollow">current instance's calendar type</a> (as a String) or the set of <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html#getAvailableCalendarTypes()" rel="nofollow">available calendar types</a> (Set of Strings). When I run <a href="http://download.java.net/jdk8/docs/api/java/util/Calendar.html#getAvailableCalendarTypes()" rel="nofollow">Calendar.getAvailableCalendarTypes()</a> on my system, I see these three Strings: "<a href="http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html" rel="nofollow">gregory</a>", "<a href="http://docs.oracle.com/javase/6/docs/technotes/guides/intl/enhancements.html" rel="nofollow">japanese</a>", and "<a href="http://en.wikipedia.org/wiki/Buddhist_calendar" rel="nofollow">buddhist</a>" (the same three calendars documented in <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html" rel="nofollow">Supported Calendars</a>)</p>
<p><strong>Conclusion</strong></p>
<p><a href="http://marxsoftware.blogspot.com/2012/03/how-badly-do-we-want-new-java-datetime.html" rel="nofollow">Like many Java developers</a>, I'm looking forward to an improved Java data/time API that is built into the standard Java Development Kit. However, I also realize that, especially in large code bases and when using libraries and frameworks expecting <code>Calendar</code> or <code>Date</code>, that I won't be free of <code>Calendar</code> and <code>Date</code> for some time. The introduction of <code>Calendar.Builder</code> in JDK 8 eases that burden a bit.</p>
<p>Original posting available at <a href="http://marxsoftware.blogspot.com/" rel="nofollow">http://marxsoftware.blogspot.com/</a> (Inspired by Actual Events)</p>
    ]]></content>
  </entry>
  <entry>
    <title>A jQuery inspired server side view model for Java</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13370" />
    <id>http://www.javaworld.com/community/node/13370</id>
    <published>2013-05-07T02:33:13-04:00</published>
    <updated>2013-05-13T06:00:12-04:00</updated>
    <author>
      <name>Johannes Brodwal</name>
    </author>
    <summary type="html"><![CDATA[<p>In HTML applications, jQuery has changed the way people thing about view rendering. Instead of an input or a text field in the view pulling data into it, the jQuery code pushes data into the view. How could this look in a server side situation like Java?</p>
<p>In this code example, I read an HTML template file from the classpath, set the value of an input field and append more data based on a template (also in the HTML file).</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>In HTML applications, jQuery has changed the way people thing about view rendering. Instead of an input or a text field in the view pulling data into it, the jQuery code pushes data into the view. How could this look in a server side situation like Java?</p>
<p>In this code example, I read an HTML template file from the classpath, set the value of an input field and append more data based on a template (also in the HTML file).</p>
<div class="wp_syntax">
<table>
<tr>
<td class="code">
<pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContactServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> nameQuery <span style="color: #339933;">=</span> req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nameQuery&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Read a pure html template from the classpath</span>
        Xhtml document <span style="color: #339933;">=</span> Xhtml.<span style="color: #006633;">fromResource</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;html/contact/index.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Set the value of the nameQuery input field in the HTML</span>
        document.<span style="color: #006633;">getForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#findForm&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nameQuery&quot;</span>, nameQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Element</span> contactsEl <span style="color: #339933;">=</span> document.<span style="color: #006633;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#contacts&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Element</span> contactTmpl <span style="color: #339933;">=</span> contactsEl.<span style="color: #006633;">take</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.contact&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Contact contact <span style="color: #339933;">:</span> findContacts<span style="color: #009900;">&#40;</span>nameQuery<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Fill in the .contact template of the HTML with data</span>
            <span style="color: #003399;">Element</span> contactHtml <span style="color: #339933;">=</span> contactTmpl.<span style="color: #006633;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span>contact.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// And add it to the #contacts element</span>
            contactsEl.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>contactHtml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        document.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>resp.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        resp.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td>
</tr>
</table>
</div>
<p>This is a simplified version of the HTML:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="code">
<pre class="html" style="font-family:monospace;">&lt;html&gt;
  &lt;form method='get' id=&quot;findForm&quot;&gt;
	&lt;input type='text' name='nameQuery' value='null' /&gt;
    &lt;input type='submit' name='findContact' value=&quot;Find contact&quot; /&gt;
  &lt;/form&gt;
  &lt;ul id='contacts'&gt;
    &lt;li class='contact'&gt;Johannes (4444444)&lt;/li&gt;
  &lt;/ul&gt;
&lt;/html&gt;</pre></td>
</tr>
</table>
</div>
<p>This is a third way from the alternatives of templated views like Velocity and JSP and from component models like JSF. In this model, the view, the model and the binding of the model variables to the view are all separate.</p>
<p>Disclaimer: In this example, I&#8217;ve used my still in pre-alpha XML library with the working name of <a href="http://github.com/jhannes/eaxy">Eaxy</a>. You can get similiar results with libraries like <a href="http://johannesbrodwall.com/2013/05/07/a-jquery-inspired-server-side-view-model-for-java/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-jquery-inspired-server-side-view-model-for-java">jSoup</a> and <a href="http://johannesbrodwall.com/2013/05/07/a-jquery-inspired-server-side-view-model-for-java/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-jquery-inspired-server-side-view-model-for-java">JOOX</a>.</p>
<p>Caveat: I&#8217;ve never tried this on a grand scale. It&#8217;s an idea that compels me for three reasons: First, it&#8217;s very explicit. Nothing happens through @annotation, conventions or some special syntax in a template. Second, it&#8217;s very unit testable. There is nothing tying this code to running in a web application server. Finally, it&#8217;s easy to get to this code through incremental steps. I initially wrote the example application with code that embedded the HTML as strings in Java code and refactored to use the Java Query approach.</p>
<p>Could this approach be worth trying out more?</p>
    ]]></content>
  </entry>
  <entry>
    <title>The Rails, CloudFront, and Heroku performance hat-trick</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13369" />
    <id>http://www.javaworld.com/community/node/13369</id>
    <published>2013-05-01T09:10:00-04:00</published>
    <updated>2013-05-17T22:03:23-04:00</updated>
    <author>
      <name>Andrew Glover</name>
    </author>
    <summary type="html"><![CDATA[<!--paging_filter--><!--paging_filter--><p><img class="right" src="http://aglover.github.com/images/mine/hat-trick.png" /><a href="http://aws.amazon.com/cloudfront/">Amazon CloudFront</a> is a pay-as-you-go global <a href="http://en.wikipedia.org/wiki/Content_delivery_network">content delivery network</a> (or CDN) that provides high availability and high performance serving of static assets. Basically, it means users have to wait less time to view your web app regardless of their location on the globe.</p>    ]]></summary>
    <content type="html"><![CDATA[<!--paging_filter--><p><img class="right" src="http://aglover.github.com/images/mine/hat-trick.png" /><a href="http://aws.amazon.com/cloudfront/">Amazon CloudFront</a> is a pay-as-you-go global <a href="http://en.wikipedia.org/wiki/Content_delivery_network">content delivery network</a> (or CDN) that provides high availability and high performance serving of static assets. Basically, it means users have to wait less time to view your web app regardless of their location on the globe.</p>

<p>It&#8217;s easy to configure a <a href="http://rubyonrails.org/">Rails</a> app to take advantage of CloudFront; what&#8217;s more, if your Rails app is hosted on <a href="http://www.ibm.com/developerworks/java/library/j-javadev2-21/">Heroku</a>, there&#8217;s <a href="https://github.com/romanbsd/heroku-deflater">a nifty gem</a>, dubbed heroku-deflater, that&#8217;ll enable <a href="http://en.wikipedia.org/wiki/HTTP_compression">HTTP compression</a> of static assets (<a href="http://stackoverflow.com/questions/12326191/any-way-to-serve-gzip-assets-from-heroku">other than images</a>).</p>

<p>To get these three entities to play nicely together requires a few simple steps. Let me show you how.</p>

<!--more-->


<p>First, if you want to enable HTTP gzip compression of static assets other than images from a Heroku app, then add the heroku-deflater gem to your <code>Gemfile</code>. This gem doesn&#8217;t compress images as in some cases, zipping images creates bigger ones!</p>

<p>Once you&#8217;ve run <code>bundle install</code> and deployed your app to Heroku, fire up a terminal and run <code>[cURL](<a href="http://thediscoblog.com/blog/2013/04/18/curling-for-wget/" title="http://thediscoblog.com/blog/2013/04/18/curling-for-wget/">http://thediscoblog.com/blog/2013/04/18/curling-for-wget/</a>)</code> to verify that the HTTP response Content-Encoding is gzip like so:</p>

<figure class='code'><figcaption><span>cURL testing gzip response</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>curl -i -H <span class="s2">&quot;Accept-Encoding: gzip,deflate&quot;</span> <a href="http://your.awesome.web.app" title="http://your.awesome.web.app">http://your.awesome.web.app</a>
</span></code></pre></td></tr></table></div></figure>


<p>You should see in the response this key phrase:</p>

<figure class='code'><figcaption><span>cURL response</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>Content-Encoding: gzip
</span></code></pre></td></tr></table></div></figure>


<p>If you do see the Content-Encoding set to gzip, then you are good to go. If, for some reason, you don&#8217;t see it, check your environment&#8217;s configuration file (which you will have to edit to get CloudFront working anyway) and verify that the property <code>config.serve_static_assets</code> is set to <code>true</code>.</p>

<p>Next, sign into the AWS Management Console and enable CloudFront if you haven&#8217;t already. From the CloudFront admin page, create a new distribution via the Create Distribution button on the top left.</p>

<p><img class="center" src="http://aglover.github.com/images/mine/cloudfront_1.png" /></p>

<p>Once the create distribution wizard begins, be sure to select Download as your delivery method.</p>

<p><img class="center" src="http://aglover.github.com/images/mine/cloudfront_2.png" /></p>

<p>In the next screen, there are some important fields you&#8217;ll need to fill out, namely: the Origin Source Name and the Viewer Protocol Policy. For the Origin Source Name, you will need to put in your app&#8217;s URL or the Heroku URL (if you do not map a custom domain name to it). If you web site supports HTTPS, then be sure to set the Viewer Protocol Policy to HTTP and HTTPS.</p>

<p><img class="center" src="http://aglover.github.com/images/mine/cloudfront_3.png" /></p>

<p>The only other important setting after these two is the Price Class. It&#8217;s here where you can set where CloudFront will essentially serve up your content &#8211; the default setting of Use All Edge Locations is most likely what you need.</p>

<p><img class="center" src="http://aglover.github.com/images/mine/cloudfront_5.png" /></p>

<p>Finally, click the Create Distribution button &#8211; once you do that, it&#8217;ll take a bit for things to initialize (basically, the CDN needs to get built and this may take up to 30 minutes).</p>

<p>Now to configure your Rails app, you&#8217;ll need to open up your target environment&#8217;s configuration file (i.e. <code>production.rb</code>). The <a href="http://bindle.me/blog/index.php/395/caches-cdns-and-heroku-cedar">two fields</a> you&#8217;ll want to be sure are properly set are <code>serve_static_assets</code> and <code>static_cache_control</code>. In particular, you are setting the cache control variable to one year. This means that once a static asset, like a JavaScript file is downloaded to the browser, it&#8217;ll be cached for one year. Don&#8217;t fret, however, if you think that&#8217;ll inhibit change &#8211; the file that is ultimately downloaded has a hash attached to it (via the magic of <a href="http://guides.rubyonrails.org/asset_pipeline.html">Rail&#8217;s Asset Pipeline</a>). Consequently, the file that is cached is something like <code>your_js_file-asdf098203820980a980</code> where that last bit is a hash value that&#8217;ll change if the file itself changes.</p>

<figure class='code'><figcaption><span>production.rb edited to support CDN</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">serve_static_assets</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">static_cache_control</span> <span class="o">=</span> <span class="s1">&#39;public, max-age=31536000&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The last change you need to make to your environment file is to set the <code>asset_host</code> to the CloudFront domain that you just created. You can find this in your AWS Management Console &#8211; it&#8217;ll be a cryptic URL like <a href="http://asdjlkj2321.cloudfront.net" title="http://asdjlkj2321.cloudfront.net">http://asdjlkj2321.cloudfront.net</a>.</p>

<figure class='code'><figcaption><span>production.rb edited to support asset host</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">action_controller</span><span class="o">.</span><span class="n">asset_host</span> <span class="o">=</span> <span class="s1">&#39;the domain name from AWS Dashboard&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Commit your changes and deploy your app.</p>

<p>To verify things are kosher, you&#8217;ll need to give it some time (check the status of your CloudFront CDN &#8211; if it&#8217;s status is Enabled then you are good to go!). If things are ready, then fire up a browser and go to your app.</p>

<p>In this case, <a href="http://thediscoblog.com/blog/2013/04/15/chromes-console-commands/">I&#8217;m using Chrome</a>. Go to JavaScript console and hit the Network tab.</p>

<p><img class="center" src="http://aglover.github.com/images/mine/cloudfront_6.png" /></p>

<p>Surf around and you&#8217;ll note a few things &#8211; one, that the assets like images and JavaScript files are being severed up from your CDN (just look at the URL) and that the size will often say &#8220;(from cache)&#8221; &#8211; that means the CDN is handling the load rather than <a href="http://www.ibm.com/developerworks/podcast/glover-heroku-110811/">Heroku</a>. You should also note that your web app is probably a bit more snappy!</p>

<p><img class="center" src="http://aglover.github.com/images/mine/cloudfront_7.png" /></p>

<p>Check out your Heroku logs and you&#8217;ll note fewer hits in this case &#8211; dynamic pages are still being loaded, however, static assets are not anymore &#8211; that&#8217;s the job of your CDN!</p>

<p>CloudFront isn&#8217;t free; nevertheless, I think you&#8217;ll find the corresponding cost quite reasonable. Pricing will vary depending on how much content you&#8217;ll be serving up with CloudFront &#8211; this is a function of how many visitors you have <em>along with</em> how many static assets that are ultimately downloaded to a user&#8217;s browser. For instance, 500GB of average content will cost you less than $75/month.</p>

<p>CloudFront&#8217;s pay-as-you-go model makes it extremely affordable to add a nice bit of pep to your app&#8217;s performance along with using gzip compression and HTTP caching. And hopefully as I&#8217;ve shown you, it&#8217;s rather easy to do with a Rails app running on Heroku.</p>    ]]></content>
  </entry>
  <entry>
    <title>More on Types</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13368" />
    <id>http://www.javaworld.com/community/node/13368</id>
    <published>2013-05-01T05:54:21-04:00</published>
    <updated>2013-05-01T06:00:12-04:00</updated>
    <author>
      <name>Ted Neward</name>
    </author>
    <summary type="html"><![CDATA[<!--paging_filter--><p>
With my most recent blog post, some of you were a little less than impressed with<br />
the idea of using types, One reader, in particular, suggested that:
</p>
<p>
Your encapsulating type aliases don't... encapsulate :|
</p>
<p>
Actually, it kinda does. But not in the way you described.
</p>
<p>
using X = qualified.type;
</p>
<p>
merely introduces an alias, and will consequently (a) not prevent assignment of</p>
<p>a FirstName to a LastName (b) not even be detectible as such from CLI metadata</p>
<p>(i.e. using reflection).
</p>
    ]]></summary>
    <content type="html"><![CDATA[<!--paging_filter--><p>
With my most recent blog post, some of you were a little less than impressed with<br />
the idea of using types, One reader, in particular, suggested that:
</p>
<p>
Your encapsulating type aliases don't... encapsulate :|
</p>
<p>
Actually, it kinda does. But not in the way you described.
</p>
<p>
using X = qualified.type;
</p>
<p>
merely introduces an alias, and will consequently (a) not prevent assignment of<br />
<br />
a FirstName to a LastName (b) not even be detectible as such from CLI metadata<br />
<br />
(i.e. using reflection).
</p>
<p>
This is true—the using statement only introduces an alias, in much the same way that<br />
C++’s “typedef” does. It’s not perfect, by any real means.
</p>
<p>
Also, the alias is lexically scoped, and doesn't actually _declare a public name_<br />
(so, it would need to be redeclared in all 'client' compilation units.
</p>
<p>
(This won't be done, of course, because the clients would have no clue about<br />
<br />
this and happily be passing `System.String` as ever).
</p>
<p>
The same goes for C++ typedefs, or, indeed C++11 template aliases:
</p>
<p>
using FirstName = std::string;<br />
<br />
using LastName = std::string;
</p>
<p>
You'd be better off using BOOST_STRONG_TYPEDEF (or a roll-your-own version of this<br />
thing that is basically a CRTP pattern with some inherited constructors. When your<br />
compiler has the latter feature, you could probably do without an evil MACRO).
</p>
<p>
All of which is also true. Frankly, the “using” statement is a temporary stopgap,<br />
simply a placeholder designed to say, “In time, this will be replaced with a full-fledged<br />
type.”
</p>
<p>
And even more to the point, he fails to point out that my “Age” class from my example<br />
doesn’t really encapsulate the fact that Age is, fundamentally, an “int” under the<br />
covers—because Age possesses type conversion operators to convert it into an int on<br />
demand (hence the “implicit” in that operator declaration), it’s pretty easy to get<br />
it back to straight “int”-land. Were I not so concerned with brevity, I’d have created<br />
a type that allowed for addition on it, though frankly I probably would forbid subtraction,<br />
and most certainly multiplication and division. (What does multiplying an Age mean,<br />
really?)
</p>
<p>
See, in truth, I cheated, because I know that the first reaction most O-O developers<br />
will have is, “Are you crazy? That’s tons more work—just use the int!” Which, is both<br />
fair, and an old argument—the C guys said the same thing about these “object” things,<br />
and how much work it was compared to just declaring a data structure and writing a<br />
few procedures to manipulate them. Creating a full-fledged type for each domain—or<br />
each fraction of a domain—seems… heavy.
</p>
<p>
Truthfully, this is <strong>much</strong> easier to do in F#. And in Scala. And in<br />
a number of different languages. Unfortunately, in C#, Java, and even C++ (and frankly,<br />
I don’t think the use of an “evil MACRO” is unwarranted, if it doesn’t promote bad<br />
things). The fact that “doing it right” in those languages means “doing a ton of work<br />
to get it right” is exactly why nobody does it—and suffers the commensurate loss of<br />
encapsulation and integrity in their domain model.
</p>
<p>
Another poster pointed out that there is a <em>much</em> better series on this at <a href="http://www.fsharpforfunandprofit.com" rel="nofollow">http://www.fsharpforfunandprofit.com</a>.<br />
In particular, check out the series on <a href="http://fsharpforfunandprofit.com/series/designing-with-types.html" rel="nofollow">&quot;Designing<br />
with Types&quot;</a>—it expresses everything I wanted to say, albeit in F# (where<br />
I was trying, somewhat unsuccessfully, to example-code it in C#). By the way, I suspect<br />
that almost every linguistic feature he uses would translate pretty easily/smoothly<br />
over to Scala (or possibly Clojure) as well.
</p>
<p>
Another poster pointed out that doing this type-driven design (TDD, anyone?) would<br />
create some serious havoc with your persistence. Cry me a river, and then go use a<br />
persistence model that fits an object-oriented and type-oriented paradigm. Like, I<br />
dunno, an <a href="http://www.db4o.com" rel="nofollow">object database</a>. Particularly considering<br />
that you shouldn’t want to expose your database schema to anyone outside the project<br />
anyway, if you’re concerned about code being tightly coupled. (As in, any other code<br />
outside this project—like a reporting engine or an ETL process—that accesses your<br />
database directly now is tied to that schema, and is therefore a tight-coupling restriction<br />
on evolving your schema.)
</p>
<p>
Achieving good encapsulation isn’t a matter of trying to hide the methods being used—it’s<br />
(partly) a matter of allowing the type system to carry a significant percentage of<br />
the cognitive load, so that you don’t have to. Which, when you think on it, is kinda<br />
what objects and strongly-typed type systems are supposed to do, isn’t it?
</p>
<p></p>
<p>Enterprise consulting, mentoring or instruction. Java, C++, .NET or XML services.<br />
1-day or multi-day workshops available. <a href="mailto:ted@tedneward.com" rel="nofollow">Contact<br />
me for details</a>.</p>
    ]]></content>
  </entry>
  <entry>
    <title>On Types</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13366" />
    <id>http://www.javaworld.com/community/node/13366</id>
    <published>2013-04-26T20:59:12-04:00</published>
    <updated>2013-05-01T06:00:12-04:00</updated>
    <author>
      <name>Ted Neward</name>
    </author>
    <summary type="html"><![CDATA[<p>
Recently, having been teaching C# for a bit at Bellevue College, I’ve been thinking<br />
more and more about the way in which we approach building object-oriented programs,<br />
and particularly the debates around types and type systems.</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>
Recently, having been teaching C# for a bit at Bellevue College, I’ve been thinking<br />
more and more about the way in which we approach building object-oriented programs,<br />
and particularly the debates around types and type systems. I think, not surprisingly,<br />
that the way in which the vast majority of the O-O developers in the world approach<br />
types and when/how they use them is flat wrong—both in terms of the times when they<br />
create classes when they shouldn’t (or shouldn’t have to, anyway, though obviously<br />
this is partly a measure of their language), and the times when they should create<br />
classes and don’t.
</p>
<p>
The latter point is the one I feel like exploring here; the former one is certainly<br />
interesting on its own, but I’ll save that for a later date. For now, I want to think<br />
about (and write about) how we often don’t create types in an O-O program, and should,<br />
because doing so can often create clearer, more expressive programs.
</p>
<h3>A Person<br />
</h3>
<p>
Common object-oriented parlance suggests that when we have a taxonomical entity that<br />
we want to represent in code (i.e., a concept of some form), we use a class to do<br />
so; for example, if we want to model a “person” in the world by capturing some of<br />
their critical attributes, we do so using a class (in this case, C#):
</p>
<p>
<font face="Consolas">class Person<br />
<br />
{<br />
<br />
&#160;&#160;&#160; public string FirstName { get; set; }<br />
<br />
&#160;&#160;&#160; public string LastName { get; set; }<br />
<br />
&#160;&#160;&#160; public int Age { get; set; }<br />
<br />
&#160;&#160;&#160; public bool Gender { get; set; }<br />
<br />
}</font>
</p>
<p>
Granted, this is a pretty simplified case; O-O enthusiasts will find lots of things<br />
wrong with this code, most of which have to do with dealing with the complexities<br />
that can arise.
</p>
<p>
From here, there’s a lot of ways in which this conversation can get a lot more complicated—how,<br />
where and when should inheritance factor into the discussion, for example, and how<br />
exactly do we represent the relationship between parents and children (after all,<br />
some children will be adopted, some will be natural birth, some will be disowned)<br />
and the relationship between various members who wish to engage in some form of marital<br />
status (putting aside the political hot-button of same-sex marriage, we find that<br />
some states respect “civil unions” even where no formal ceremony has taken place,<br />
many cultures still recognize polygamy—one man, many wives—as Utah did up until the<br />
mid-1800s, and a growing movement around polyamory—one or more men, one or more women—looks<br />
like it may be the next political hot-button around marriage) definitely depends on<br />
the business issues in question…
</p>
<p>
… but that’s the whole point of encapsulation, right? That if the business needs change,<br />
we can adapt as necessary to the changed requirements without having to go back and<br />
rewrite everything.
</p>
<h4>Genders<br />
</h4>
<p>
Consider, for example, the rather horrible decision to represent “gender” as a boolean:<br />
while, yes, at birth, there are essentially two genders at the biological level, there<br />
are some interesting birth defects/disorders/conditions in which a person’s gender<br />
is, for lack of a better term, screwed up—men born with female plumbing and vice versa.<br />
The system might need to track that. Or, there are those who consider themselves to<br />
have been born into the wrong gender, and choose to live a lifestyle that is markedly<br />
different from what societal norms suggest (the transgender crowd). Or, in some cases,<br />
the gender may not have even been determined yet: fetuses don’t develop gender until<br />
about halfway through the pregnancy.
</p>
<p>
Which suggests, offhand, that the use of a boolean here is clearly a Bad Idea. But<br />
what suggests as its replacement? Certainly we could maintain an internal state string<br />
or something similar, using the get/set properties to verify that the strings being<br />
set are correct and valid, but the .NET type system has a better answer: Given that<br />
there is a finite number of choices to gender—whether that’s two or four or a dozen—it<br />
seems that an enumeration is a good replacement:
</p>
<p>
<font face="Consolas">enum Gender<br />
<br />
{<br />
<br />
&#160;&#160;&#160; Male, Female,<br />
<br />
&#160;&#160;&#160; Indeterminate,<br />
<br />
&#160;&#160;&#160; Transgender<br />
<br />
}</font>
</p>
<p>
<font face="Consolas">class Person<br />
<br />
{<br />
<br />
&#160;&#160;&#160; public string FirstName { get; set; }<br />
<br />
&#160;&#160;&#160; public string LastName { get; set; }<br />
<br />
&#160;&#160;&#160; public int Age { get; set; }<br />
<br />
&#160;&#160;&#160; public Gender Gender { get; set; }<br />
<br />
}</font>
</p>
<p>
Don’t let the fact that the property and the type have the same name be too confusing—not<br />
only does it compile cleanly, but it actually provides some clear description of what’s<br />
being stored. (Although, I’ll admit, it’s confusing the first time you look at it.)<br />
More importantly, there’s no additional code that needs to be written to enforce only<br />
the four acceptable values—or, extend it as necessary when that becomes necessary.
</p>
<h3>
</h3>
<h4>Ages<br />
</h4>
<p>
Similarly, the age of a person is not an integer value—people cannot be negative age,<br />
nor do they usually age beyond a hundred or so. Again, we could put code around the<br />
get/set blocks of the Age property to ensure the proper values, but it would again<br />
be easier to let the type system do all the work:
</p>
<p>
<font face="Consolas">struct Age<br />
<br />
{<br />
<br />
&#160;&#160;&#160; int data;<br />
<br />
&#160;&#160;&#160; public Age(int d)<br />
<br />
&#160;&#160;&#160; {<br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160; Validate(d);<br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160; data = d;<br />
<br />
&#160;&#160;&#160; }</font>
</p>
<p>
<font face="Consolas">&#160;&#160;&#160; public static void Validate(int d)<br />
<br />
&#160;&#160;&#160; {<br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (d &lt; 0)<br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw new ArgumentException(&quot;Age<br />
cannot be negative&quot;);<br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (d &gt; 120)<br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw new ArgumentException(&quot;Age<br />
cannot be over 120&quot;);<br />
<br />
&#160;&#160;&#160; }</font>
</p>
<p>
<font face="Consolas">&#160;&#160;&#160; // explicit int to Age conversion operator<br />
<br />
&#160;&#160;&#160; public static implicit operator Age(int a)<br />
<br />
&#160;&#160;&#160; { return new Age(a); }</font>
</p>
<p>
<font face="Consolas">&#160;&#160;&#160; // explicit Age to int conversion operator<br />
<br />
&#160;&#160;&#160; public static implicit operator int(Age a)<br />
<br />
&#160;&#160;&#160; { return a.data; }<br />
<br />
}</font>
</p>
<p>
<font face="Consolas">class Person<br />
<br />
{<br />
<br />
&#160;&#160;&#160; public string FirstName { get; set; }<br />
<br />
&#160;&#160;&#160; public string LastName { get; set; }<br />
<br />
&#160;&#160;&#160; public Age Age { get; set; }<br />
<br />
&#160;&#160;&#160; public Gender Gender { get; set; }<br />
<br />
}</font>
</p>
<p>
Notice that we’re still having to write the same code, but now the code is embodied<br />
in a type, which is itself intrinsically reusable—we can reuse the Age type in other<br />
classes, which is more than we can say if that code lives in the Person.Age property<br />
getter/setter. Again, too, now the Person class really has nothing to do in terms<br />
of ensuring that age is maintained properly (and by that, I mean greater than zero<br />
and less than 120). (The “implicit” in the conversion operators means that the code<br />
doesn’t need to explicitly cast the int to an Age or vice versa.)
</p>
<p>
Technically, what I’ve done with Age is create a restriction around the integer (System.Int32<br />
in .NET terms) type; were this XSD Schema types, I could do a derivation-by-restriction<br />
to restrict an xsd:int to the values I care about (0 – 120, inclusive). Unfortunately,<br />
no O-O language I know of permits derivation-by-restriction, so it requires work to<br />
create a type that “wraps” another, in this case, an Int32.
</p>
<h4>
</h4>
<h4>
</h4>
<h4>Names<br />
</h4>
<p>
Names are another point of problem, in that there’s all kinds of crazy cases that<br />
(as much as we’d like to pretend otherwise) turn out to be far more common than we’d<br />
like—not only do most people have middle names, but sometimes women will take their<br />
husband’s last name and hyphenate it with their own, making it sort of a middle name<br />
but not really, or sometimes people will give their children to multiple middle names,<br />
Japanese names put family names first, sometimes people choose to take a single name,<br />
and so on. This is again a case where we can either choose to bake that logic into<br />
property getters/setters, or bake it into a single type (a “Name” type) that has the<br />
necessary code and properties to provide all the functionality that a person’s name<br />
represents.
</p>
<p>
So, without getting into the actual implementation, then, if we want to represent<br />
names in the system, then we should have a full-fledged “Name” class that captures<br />
the various permutations that arise:
</p>
<p>
<font face="Consolas">class Name<br />
<br />
{&#160;&#160;<br />
<br />
&#160;&#160;&#160; public Title Honorific { get { ... } }<br />
<br />
&#160;&#160;&#160; public string Individual { get { ... } }<br />
<br />
&#160;&#160;&#160; public string Nickname { get { ... } }<br />
<br />
&#160;&#160;&#160; public string Family { get { ... } }<br />
<br />
&#160;&#160;&#160; public string Full { get { ... } }<br />
<br />
&#160;&#160;&#160; public static Name Parse(string incoming) { ... }&#160;<br />
<br />
}</font>>
</p>
<p>
<font face="Consolas"></font>
</p>
<p>
See, ultimately, everything will have to boil back to the core primitives within the<br />
language, but we need to build stronger primitives for the system—Name, Title, Age,<br />
and don’t even get me started on relationships.
</p>
<h4>
</h4>
<h4>
</h4>
<h4>Relationships<br />
</h4>
<p>
Parent-child relationships are also a case where things are vastly more complicated<br />
than just the one-to-many or one-to-one (or two-to-one) that direct object references<br />
encourage; in the case of families, given how complex the modern American family can<br />
get (and frankly, it’s not any easier if we go back and look at medieval families,<br />
either—go have a look at any royal European genealogical line and think about how<br />
you’d model that, particularly Henry VIII), it becomes pretty quickly apparent that<br />
modeling the relationships themselves often presents itself as the only reasonable<br />
solution.
</p>
<p>
I won’t even begin to get into that example, by the way, simply because this blog<br />
post is too long as it is. I might try it for a later blog post to explore the idea<br />
further, but I think the point is made at this point.
</p>
<h3>
</h3>
<h3>Summary<br />
</h3>
<p>
The object-oriented paradigm often finds itself wading in tens of thousands of types,<br />
so it seems counterintuitive to suggest that we need more of them to make programs<br />
more clear. I agree, many O-O programs are too type-heavy, but part of the problem<br />
there is that we’re spending too much time creating classes that we shouldn’t need<br />
to create (DTOs and the like) and not enough time thinking about the actual entities<br />
in the system.
</p>
<p>
I’ll be the first to admit, too, that not all systems will need to treat names the<br />
way that I’ve done—sometimes an age is just an integer, and we’re OK with that. Truthfully,<br />
though, it seems more often than not that we’re later adding the necessary code to<br />
ensure that ages can never be negative, have to fall within a certain range, and so<br />
on.
</p>
<p>
As a suggestion, then, I throw out this idea: <strong><em>Ensure that all of your<br />
domain classes never expose primitive types to the user of the system.</em></strong> In<br />
other words, Name never exposes an “int” for Age, but only an “Age” type. C# makes<br />
this easy via “using” declarations, like so:
</p>
<p>
<font face="Consolas">using FirstName = System.String;<br />
<br />
using LastName = System.String;</font>
</p>
<p>
which can then, if you’re thorough and disciplined about using the FirstName and LastName<br />
types instead of “string”, evolve into fully-formed types later in their own right<br />
if they need to. C++ provides “typedef” for this purpose—unfortunately, Java lacks<br />
any such facility, making this a much harder prospect. (This is something I’d stick<br />
at the top of my TODO list were I nominated to take Brian Goetz’s place at the head<br />
of Java9 development.)
</p>
<p>
In essence, encapsulate the primitive types away so that when they don’t need to be<br />
primitives, or when they need to be more complex than just simple holders of data,<br />
they don’t have to be, and clients will never know the difference. That, folks, is<br />
what encapsulation is trying to be about.
</p>
<p><img width="0" height="0" src="http://blogs.tedneward.com/aggbug.ashx?id=1a5622b2-d891-43ad-af4b-785ba018e862" /><br />
<br />
<hr /><br />
Enterprise consulting, mentoring or instruction. Java, C++, .NET or XML services.<br />
1-day or multi-day workshops available. <a href="mailto:ted@tedneward.com">Contact<br />
me for details</a>.</p>
    ]]></content>
  </entry>
  <entry>
    <title>Interesting Java Blog Posts in Mid-April 2013</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13365" />
    <id>http://www.javaworld.com/community/node/13365</id>
    <published>2013-04-26T10:37:00-04:00</published>
    <updated>2013-04-26T11:00:00-04:00</updated>
    <author>
      <name>Dustin Marx</name>
    </author>
    <summary type="html"><![CDATA[<p>There have been numerous Java-related blog posts in recent days and I reference and summarize four of them here.</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>There have been numerous Java-related blog posts in recent days and I reference and summarize four of them here.</p>
<p><strong>Creating a Memory Leak</strong></p>
<p>In the "rather evil post" <a href="http://plumbr.eu/blog/how-to-create-a-memory-leak">How to Create a Memory Leak</a>, <a href="http://plumbr.eu/about-us">Ivo Mägi</a> describes and provides code examples of two commons leaks. The first leak covered is related to objects lacking the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()">hashCode()</a>/<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)">equals(Object)</a> implementations to be used properly in <a href="http://marxsoftware.blogspot.com/2010/02/basic-java-hashcode-and-equals.html">collections needing them to be correct</a>. The second covered leak is related to the <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513622">change of handling</a> for <a href="http://java-performance.info/changes-to-string-java-1-7-0_06/">String internals</a> in <a href="http://www.oracle.com/technetwork/java/javase/7u6-relnotes-1729681.html">Java 7 Update 6</a>. The second problem is a reminder of the subtle and difficult to diagnose issues that can occur when even minor differences exist between versions of Java used in different environments.</p>
<p><strong>StringBuffer Was Never a Good Idea</strong></p>
<p><a href="http://www.blogger.com/profile/17982030676088168612">Peter Lawrey</a>'s blog post <a href="http://vanillajava.blogspot.com/2013/04/why-synchronized-stringbuffer-was-never.html">Why a synchronized StringBuffer was never a good idea</a> articulates something I've found to be the case: most uses of <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html">building up strings</a> do not need to be thread safe. Even when thread safety is needed, Lawrey points out why <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html">StringBuffer</a> often is not sufficient.</p>
<p><strong>9 Fallacies of Java Performance</strong></p>
<p><a href="http://www.infoq.com/author/Ben-Evans">Ben Evans</a> looks at <a href="http://www.infoq.com/articles/9_Fallacies_Java_Performance">9 Fallacies of Java Performance</a> including the outdated mantra "Java is slow" and problems with using microbenchmarks to make performance decisions. He points out that "Java performance-tuning intuition is often misleading" and that "we require empirical data and tools to help us visualize and understand the platform's behavior."</p>
<p><strong>Infamous Java Bugs and Pitfalls</strong></p>
<p>Murat Yener has collected some Java-related "bugs and pitfalls" in his post <a href="http://www.devchronicles.com/2013/04/infamous-java-bugs-and-pitfalls.html">Infamous Java Bugs and Pitfalls</a> such as <a href="http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html">autoboxing</a> issues and <a href="http://marxsoftware.blogspot.com/2010/01/caution-double-to-bigdecimal-in-java.html">BigDecimal quirks</a>.</p>
<p><strong>Conclusion</strong></p>
<p>The four blog posts on Java cited above have been recently posted and provide good reminders and potentially new information on things to be cognizant of when developing with Java.</p>
<div class="blogger-post-footer">
<p>Original posting available at <a href="http://marxsoftware.blogspot.com/">http://marxsoftware.blogspot.com/</a> (Inspired by Actual Events)</p>
</div>
<p><img src="http://feeds.feedburner.com/~r/InspiredByActualEvents/~4/1a76EGMgNzo" height="1" width="1" /></p>
    ]]></content>
  </entry>
  <entry>
    <title>Mobile for the masses: gestures and test deployments</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13364" />
    <id>http://www.javaworld.com/community/node/13364</id>
    <published>2013-04-24T21:13:00-04:00</published>
    <updated>2013-05-17T22:03:23-04:00</updated>
    <author>
      <name>Andrew Glover</name>
    </author>
    <summary type="html"><![CDATA[<p><img class="right" src="http://aglover.github.com/images/mine/mftm_1.jpg" />My second article in <a href="http://www.ibm.com/developerworks/">IBM developerWorks</a>&#8217; series <a href="http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=mobile+for+the+masses">Mobile for the Masses</a> is out! <a href="http://www.ibm.com/developerworks/java/library/j-mobileforthemasses2/index.html">This article</a> covers how to go about designing a mobile interface from the standpoint of usability via gestures. In essence, eschew buttons and prefer the swipe!</p>
    ]]></summary>
    <content type="html"><![CDATA[<p><img class="right" src="http://aglover.github.com/images/mine/mftm_1.jpg" />My second article in <a href="http://www.ibm.com/developerworks/">IBM developerWorks</a>&#8217; series <a href="http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=mobile+for+the+masses">Mobile for the Masses</a> is out! <a href="http://www.ibm.com/developerworks/java/library/j-mobileforthemasses2/index.html">This article</a> covers how to go about designing a mobile interface from the standpoint of usability via gestures. In essence, eschew buttons and prefer the swipe! What&#8217;s more, I cover at a high level how to test your app on an Android device.</p>
<p>As the article summary states:</p>
<blockquote><p>Mobile users are typically distracted, busy, and ergonomically constrained, so build your mobile application UIs accordingly. Andrew Glover discusses key factors that differentiate mobile apps from web apps, then guides you through the creation of a mobile application UI that uses swipe gestures for navigation rather than button clicks.</p>
<p><footer><strong>IBM DeveloperWorks</strong> <cite><a href="http://www.ibm.com/developerworks/java/library/j-mobileforthemasses2/index.html">Mobile for the Masses: Take a Swipe at It! Programming Gestures in Android</a></cite></footer></p></blockquote>
<p>So what are you waiting for? Check out &#8221;<a href="http://www.ibm.com/developerworks/java/library/j-mobileforthemasses2/index.html">Mobile for the masses: Take a swipe at it! Programming gestures in Android</a>&#8221; and trade buttons for swipes in your Android mobile apps! And if you missed the first article, check out &#8221;<a href="http://www.ibm.com/developerworks/library/j-mobileforthemasses1/">Mobile for the masses: A gentle introduction to Android</a>&#8221; and learn the ins and outs of Android development!</p>
<p>Stay tuned for more articles in this series and don&#8217;t forget to watch for <a href="http://thediscoblog.com/blog/categories/mobile/">mobile focused articles on this blog</a> as well.</p>
    ]]></content>
  </entry>
  <entry>
    <title>More Reasons to Update to Java 7</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13362" />
    <id>http://www.javaworld.com/community/node/13362</id>
    <published>2013-04-22T10:50:00-04:00</published>
    <updated>2013-04-22T12:00:00-04:00</updated>
    <author>
      <name>Dustin Marx</name>
    </author>
    <summary type="html"><![CDATA[<p>In <a href="https://blogs.oracle.com/java/entry/java_se_7_update_21">Java SE 7 Update 21 Release and more</a>, <a href="http://blog.eisele.net/2011/10/my-interview-with-tori-wieldt-java.html">Tori Wieldt</a> pointed out that "Oracle has released three updates to Java" this past week: <a href="http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html">Java 6 Update 45</a>, <a href="http://www.oracle.com/technetwork/java/javase/7u21-relnotes-1932873.html">Java 7 Update 21</a>, and <a href="http://www.oracle.com/technetwork/java/javase/emb7u21-relnotes-1932887.html">Java SE Embe</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>In <a href="https://blogs.oracle.com/java/entry/java_se_7_update_21">Java SE 7 Update 21 Release and more</a>, <a href="http://blog.eisele.net/2011/10/my-interview-with-tori-wieldt-java.html">Tori Wieldt</a> pointed out that "Oracle has released three updates to Java" this past week: <a href="http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html">Java 6 Update 45</a>, <a href="http://www.oracle.com/technetwork/java/javase/7u21-relnotes-1932873.html">Java 7 Update 21</a>, and <a href="http://www.oracle.com/technetwork/java/javase/emb7u21-relnotes-1932887.html">Java SE Embedded 7 Update 21</a>. This <a href="https://blogs.oracle.com/java/">The Java Source</a> blog post briefly focuses on "new security <i>features</i> in this release" (more than just <a href="http://krebsonsecurity.com/2013/04/java-update-plugs-42-security-holes/">security fixes</a>): "<a href="http://www.oracle.com/technetwork/java/javase/7u21-relnotes-1932873.html#serverjre">new Server JRE package</a>" (tools used commonly for server "without the Java plug-in, auto-update or installer found in the regular JRE package"), removal of <i>low</i> and <i>custom</i> settings from <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/jcp.html#security">Java Control Panel</a>, and changes to <a href="https://blogs.oracle.com/java/entry/imp_your_applets_and_web">Security Dialogs</a>. <a href="https://plus.google.com/100362024804331957185/posts">Markus Eisele</a> has posted additional details on the new security features in <a href="http://blog.eisele.net/2013/04/java-7-update-21-security-improvements.html">Java 7 Update 21 Security Improvements in Detail</a>.</p>
<p>Whether it be security fixes or security features, the attention paid to security in Java SE 6 and Java SE 7 has come at a <a href="http://www.infoworld.com/d/application-development/java-8-release-delayed-until-next-year-216797">price</a>: it looks like Java 8 is likely to be delayed again (this time pushing <a href="http://openjdk.java.net/projects/jdk8/milestones#General_Availability">General Availability</a> to First Quarter 2014). In the blog post <a href="http://mreinhold.org/blog/secure-the-train">Java 8: Secure the train</a>, <a href="http://mreinhold.org/">Mark Reinhold</a> writes, "Maintaining the security of the Java Platform always takes priority over developing new features, and so these efforts have inevitably taken engineers away from working on Java 8. ... As a consequence of this renewed focus on security the Java 8 schedule, with a GA release in early September, is no longer achievable." For people waiting to upgrade from Java 6 to skip to Java 8, this delay of Java 8 is another reason to consider upgrading to Java 7 now.</p>
<p>Although Oracle has been <a href="http://www.infoq.com/news/2013/01/jdk6-retirement">warning</a> of the end of free availability of <a href="http://www.oracle.com/technetwork/java/javase/eol-135779.html">Java 6 updates</a> since the beginning of the year and <a href="https://blogs.oracle.com/henrik/entry/java_6_eol_h_h">extended Public Updates</a> multiple times, <a href="https://blogs.oracle.com/henrik/">Henrik Stahl</a>'s post <a href="https://blogs.oracle.com/henrik/entry/oracle_jdk_7u21_released_with">Oracle JDK 7u21 released with new security features</a> states that "we are not planning any more public JDK 6 updates" (past Java 6 Update 45). Users of Java 6 are encouraged to <a href="http://marxsoftware.blogspot.com/Migrating%20from%20Java%20SE%206%20to%20Java%20SE%207">migrate to Java 7</a> to continue receiving public updates. In his Question-and-Answer formatted post, Stahl also covers alternatives such as access to newer Java 6 releases via the appropriate support license ("Oracle middleware or applications customer") or via separately purchased <a href="http://www.oracle.com/us/technologies/java/standard-edition/support/overview/index.html">Java SE support</a>.</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-0UMStnECqSQ/UXSxPvnxHdI/AAAAAAAAELU/voXvmZCT9YY/s1600/JavaSE7EndOfPublicSupportJava6Update45.png" imageanchor="1"><img border="0" src="http://4.bp.blogspot.com/-0UMStnECqSQ/UXSxPvnxHdI/AAAAAAAAELU/voXvmZCT9YY/s320/JavaSE7EndOfPublicSupportJava6Update45.png" /></a>
</div>
<p>Besides access to future public releases of Java SE, there are other reasons to upgrade to Java 7. Some of these are enumerated in <a href="http://www.agilejava.eu/2011/07/15/7-reasons-to-upgrade-to-java-7/">7 reasons to upgrade to Java 7</a> and the "Features of Java 7" section of "<a href="http://java.com/en/download/faq/why_upgrade.xml">Why should I upgrade to Java 7?</a>"</p>
<div class="blogger-post-footer">
<p>Original posting available at <a href="http://marxsoftware.blogspot.com/">http://marxsoftware.blogspot.com/</a> (Inspired by Actual Events)</p>
</div>
<p><img src="http://feeds.feedburner.com/~r/InspiredByActualEvents/~4/yRzDyVa5Ia8" height="1" width="1" /></p>
    ]]></content>
  </entry>
  <entry>
    <title>cURLing for Wget</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13360" />
    <id>http://www.javaworld.com/community/node/13360</id>
    <published>2013-04-18T09:33:00-04:00</published>
    <updated>2013-05-17T22:03:23-04:00</updated>
    <author>
      <name>Andrew Glover</name>
    </author>
    <summary type="html"><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Wget">Wget</a> is an extremely handy utility I use all the time when I find myself on a Linux box. It&#8217;s quite helpful, for example, for downloading files. Need to <a href="https://github.com/aglover/ubuntu-equip/blob/master/equip_ruby.sh">install Ruby</a>? No problem, just download the binary like so:</p>
    ]]></summary>
    <content type="html"><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Wget">Wget</a> is an extremely handy utility I use all the time when I find myself on a Linux box. It&#8217;s quite helpful, for example, for downloading files. Need to <a href="https://github.com/aglover/ubuntu-equip/blob/master/equip_ruby.sh">install Ruby</a>? No problem, just download the binary like so:</p>
<p><figure class='code'><figcaption><span>wget example of downloading Ruby binary</span></figcaption><br />
<div class="highlight">
<table>
<tr>
<td class="gutter">
<pre class="line-numbers"><span class='line-number'>1</span>
</pre></td>
<td class='code'>
<pre><code class='bash'><span class='line'>wget <a href="http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz" title="http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz">http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz</a>
</span></code></pre></td>
</tr>
</table>
</div>
<p></figure></p>
<p></p>
<p>and you&#8217;re one step closer. There&#8217;s no flags to remember either.</p>
<p><!--more--></p>
<p>Wget, however, isn&#8217;t natively available on OSX. From time to time, I&#8217;m stung to see the nefarious &#8216;command not found&#8217; message after expectantly waiting to see some file I need downloaded.</p>
<p>Luckily, you can force <a href="http://curl.haxx.se/docs/manpage.html">cURL</a> to act like Wget with a few flags. Simply use the <code>-OL</code> flags like so:</p>
<p><figure class='code'><figcaption><span>cURL acting like Wget</span></figcaption><br />
<div class="highlight">
<table>
<tr>
<td class="gutter">
<pre class="line-numbers"><span class='line-number'>1</span>
</pre></td>
<td class='code'>
<pre><code class='bash'><span class='line'>curl -OL <a href="http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz" title="http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz">http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz</a>
</span></code></pre></td>
</tr>
</table>
</div>
<p></figure></p>
<p></p>
<p>and you&#8217;ll be on your way to downloading some file.</p>
<p>Of course, it&#8217;s possible to get <code>wget</code> on your Mac via <a href="http://osxdaily.com/2012/05/22/install-wget-mac-os-x/">MacPorts or Homebrew</a>; nevertheless, knowing you can achieve the same goals with cURL is always handy.</p>
    ]]></content>
  </entry>
  <entry>
    <title>JDK 8 Javadoc Tweaked For Methods Listings</title>
    <link rel="alternate" type="text/html" href="http://www.javaworld.com/community/node/13353" />
    <id>http://www.javaworld.com/community/node/13353</id>
    <published>2013-04-16T10:03:00-04:00</published>
    <updated>2013-04-16T12:00:01-04:00</updated>
    <author>
      <name>Dustin Marx</name>
    </author>
    <summary type="html"><![CDATA[<p>Since it's beginning, <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html">Javadoc</a> output has been largely static HTML with navigation links and simple stylesheet styling of its appearance.</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>Since it's beginning, <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html">Javadoc</a> output has been largely static HTML with navigation links and simple stylesheet styling of its appearance. <a href="http://docs.oracle.com/javase/7/docs/api/">Java SE 7</a> saw the <a href="http://marxsoftware.blogspot.com/2011/06/new-java-se-7-javadoc-api-documentation.html">first significant change in Javadoc output</a> default appearance in a long time and now it looks like <a href="http://openjdk.java.net/projects/jdk8/">JDK 8</a> will introduce a new twist on generated Javadoc output. In this post, I look briefly at the more dynamic filtering of methods in Javadoc output by type of method that appears to be available with JDK 8's <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html">javadoc</a> tool.</p>
<p>The following two screen snapshots compare the JDK 7 and JDK 8 versions of default Javadoc output for the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html">Calendar</a> class (chosen for its variety of method types).</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-VOpBQ6-iBbU/UWtnVFUyKmI/AAAAAAAAEKU/TLrNbau13l8/s1600/javase7JavadocCalendarMethods.png" imageanchor="1"><img border="0" src="http://2.bp.blogspot.com/-VOpBQ6-iBbU/UWtnVFUyKmI/AAAAAAAAEKU/TLrNbau13l8/s320/javase7JavadocCalendarMethods.png" /></a>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-6VYurcniJ4o/UWtnYRahUFI/AAAAAAAAEKc/dyKUAr8IVNU/s1600/jdk8JavadocCalendarMethods.png" imageanchor="1"><img border="0" src="http://1.bp.blogspot.com/-6VYurcniJ4o/UWtnYRahUFI/AAAAAAAAEKc/dyKUAr8IVNU/s320/jdk8JavadocCalendarMethods.png" /></a>
</div>
<p>Although the Javadoc output for JDK 8 has the same overall styles (including orange "tabs" for "Constructors", "Fields", "Methods" and so forth) as Java 7 Javadoc documentation, the methods in the JDK 8 generated documentation have multiple orange tabs above the listing of methods that allow the reader to click on the appropriate tab to see the appropriate subset of methods. The names of the tabs are about as self-explanatory as you could want with names like "All Methods" (all methods on the class), "<a href="http://www.javaworld.com/javaqa/2001-11/03-qa-1121-mrhappy.html">Static Methods</a>" (class-level methods), "<a href="http://math.hws.edu/javanotes/c5/s1.html">Instance Methods</a>" (object-level methods), "<a href="http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html">Abstract Methods</a>" (methods without implementation), and "<a href="http://butunclebob.com/ArticleS.DeanWampler.ShouldYouEverOverrideConcreteMethods">Concrete Methods</a>" (implemented methods).</p>
<p>The next series of screen snapshots demonstrate how clicking on any of these tabs invokes embedded JavaScript code that calls appropriate functions in the <code style="font-weight: bold;">script.js</code> file that exists at the root level of the generated Javadoc directory structure.</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-k31xB5cxiLs/UWtrxADnB6I/AAAAAAAAEK8/-CJnMrPKVP4/s1600/jdk8JavadocCalendarStaticMethods.png" imageanchor="1"><img border="0" src="http://1.bp.blogspot.com/-k31xB5cxiLs/UWtrxADnB6I/AAAAAAAAEK8/-CJnMrPKVP4/s320/jdk8JavadocCalendarStaticMethods.png" /></a>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-sPUlIlekqoQ/UWtrxfqI1VI/AAAAAAAAELA/vTiMxvfOlK4/s1600/jdk8JavadocCalendarInstanceMethods.png" imageanchor="1"><img border="0" src="http://3.bp.blogspot.com/-sPUlIlekqoQ/UWtrxfqI1VI/AAAAAAAAELA/vTiMxvfOlK4/s320/jdk8JavadocCalendarInstanceMethods.png" /></a>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-idzlTEXw-WM/UWtrwbeDQdI/AAAAAAAAEKs/LmPEAnKH97w/s1600/jdk8JavadocCalendarAbstractMethods.png" imageanchor="1"><img border="0" src="http://3.bp.blogspot.com/-idzlTEXw-WM/UWtrwbeDQdI/AAAAAAAAEKs/LmPEAnKH97w/s320/jdk8JavadocCalendarAbstractMethods.png" /></a>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-bPQ-ZojnymM/UWtrw0XSVNI/AAAAAAAAEK0/P5twRAuxt3g/s1600/jdk8JavadocCalendarConcreteMethods.png" imageanchor="1"><img border="0" src="http://2.bp.blogspot.com/-bPQ-ZojnymM/UWtrw0XSVNI/AAAAAAAAEK0/P5twRAuxt3g/s320/jdk8JavadocCalendarConcreteMethods.png" /></a>
</div>
<p>This is a minor, but still somewhat handy, feature of Javadoc in JDK 8. I have often looked through methods of Javadoc for a class with mixed method types for methods that fit one particular type. Now, rather than needing to scan for keywords such as "static" or "abstract" or for the absence of those same words, I can simply use these "tabs" to filter the types of methods I want to see for a given class.</p>
<div class="blogger-post-footer">
<p>Original posting available at <a href="http://marxsoftware.blogspot.com/">http://marxsoftware.blogspot.com/</a> (Inspired by Actual Events)</p>
</div>
<p><img src="http://feeds.feedburner.com/~r/InspiredByActualEvents/~4/lBe9TC3wgTg" height="1" width="1" /></p>
    ]]></content>
  </entry>
</feed>
