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

<channel>
	<title>Giddy Planet</title>
	<atom:link href="http://giddyplanet.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://giddyplanet.com</link>
	<description>by Rene Hangstrup Møller</description>
	<lastBuildDate>Fri, 22 Jul 2011 12:36:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java 7: Try-With-Resources (AutoCloseable)</title>
		<link>http://giddyplanet.com/2011/07/java-7-project-coin-autocloseable/</link>
		<comments>http://giddyplanet.com/2011/07/java-7-project-coin-autocloseable/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 12:36:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[coin]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java7]]></category>

		<guid isPermaLink="false">http://www.giddyplanet.com/?p=108</guid>
		<description><![CDATA[Another great thing introduced with Java 7 is the &#8220;try-with-resources&#8221; construct &#8211; previously known as Automatic Resource Management (ARM). This feature is part of Project Coin, which is an umbrella project for a set of small changes to the Java &#8230; <a href="http://giddyplanet.com/2011/07/java-7-project-coin-autocloseable/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Another great thing introduced with Java 7 is the &#8220;try-with-resources&#8221; construct &#8211; previously known as Automatic Resource Management (ARM). This feature is part of Project Coin, which is an umbrella project for a set of small changes to the Java language. You can see my other post on <a href="http://www.giddyplanet.com/2011/07/java-7-project-coin/">Java 7: Project Coin</a> for more info.</p>
<p>When you work with files, networks streams or databases you follow a common pattern. </p>
<ol>
<li>Open resources
<li>Perform operation
<li>Close resources
</ol>
<p>You have to wrap the code in a try-finally block to make sure the resources are closed even if an exception occurs. Take a look at this example that copies the contents of one file to another file.</p>
<div id="gist-1099165" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">oldStyleCopyFile</span><span class="o">(</span><span class="n">File</span> <span class="n">source</span><span class="o">,</span> <span class="n">File</span> <span class="n">target</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">FileInputStream</span> <span class="n">in</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">FileOutputStream</span> <span class="n">out</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">in</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileInputStream</span><span class="o">(</span><span class="n">source</span><span class="o">);</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">out</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileOutputStream</span><span class="o">(</span><span class="n">target</span><span class="o">);</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="o">[]</span> <span class="n">buffer</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="o">[</span><span class="mi">4096</span><span class="o">];</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">read</span><span class="o">;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">while</span> <span class="o">((</span><span class="n">read</span> <span class="o">=</span> <span class="n">in</span><span class="o">.</span><span class="na">read</span><span class="o">(</span><span class="n">buffer</span><span class="o">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">out</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">buffer</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">read</span><span class="o">);</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">FileNotFoundException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">finally</span> <span class="o">{</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">out</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">out</span><span class="o">.</span><span class="na">close</span><span class="o">();</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">in</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">in</span><span class="o">.</span><span class="na">close</span><span class="o">();</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1099165/d5aa1eaa386ab5d56e9bcdc5778224849d6c20fb/OldCopyFile.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1099165#file_old_copy_file.java" style="float:right;margin-right:10px;color:#666">OldCopyFile.java</a>
            <a href="https://gist.github.com/1099165">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Java 7 introduces a new construct called try-with-resources. You can initialize the resources inside parenthesis between the try keyword and the opening bracket, and Java will automatically close them for you. This works for all resources that implement the AutoCloseable interface.</p>
<div id="gist-1099165" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">newStyleCopyFile</span><span class="o">(</span><span class="n">File</span> <span class="n">source</span><span class="o">,</span> <span class="n">File</span> <span class="n">target</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">(</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">FileInputStream</span> <span class="n">in</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileInputStream</span><span class="o">(</span><span class="n">source</span><span class="o">);</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">FileOutputStream</span> <span class="n">out</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileOutputStream</span><span class="o">(</span><span class="n">target</span><span class="o">);</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="o">[]</span> <span class="n">buffer</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="o">[</span><span class="mi">4096</span><span class="o">];</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">read</span><span class="o">;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">while</span> <span class="o">((</span><span class="n">read</span> <span class="o">=</span> <span class="n">in</span><span class="o">.</span><span class="na">read</span><span class="o">(</span><span class="n">buffer</span><span class="o">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">out</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">buffer</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">read</span><span class="o">);</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">FileNotFoundException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1099165/d76859f35a61cbccf5f315a1831b5c50732bdf9e/NewCopyFile.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1099165#file_new_copy_file.java" style="float:right;margin-right:10px;color:#666">NewCopyFile.java</a>
            <a href="https://gist.github.com/1099165">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Java 7 also contains NIO.2 which improves file operations in Java, and adds several essential features that Java has been missing. For example, now you can copy a file in one line. As a bonus this method not only copies the file contents but also the file metadata!</p>
<div id="gist-1099165" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">shortCopyFile</span><span class="o">(</span><span class="n">File</span> <span class="n">source</span><span class="o">,</span> <span class="n">File</span> <span class="n">target</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Files</span><span class="o">.</span><span class="na">copy</span><span class="o">(</span><span class="n">source</span><span class="o">.</span><span class="na">toPath</span><span class="o">(),</span> <span class="n">target</span><span class="o">.</span><span class="na">toPath</span><span class="o">());</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1099165/9451262a4b349bb998a0541a0e1bc509ceb05942/ShortCopyFile.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1099165#file_short_copy_file.java" style="float:right;margin-right:10px;color:#666">ShortCopyFile.java</a>
            <a href="https://gist.github.com/1099165">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://giddyplanet.com/2011/07/java-7-project-coin-autocloseable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java 7: Project Coin</title>
		<link>http://giddyplanet.com/2011/07/java-7-project-coin/</link>
		<comments>http://giddyplanet.com/2011/07/java-7-project-coin/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 14:10:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[coin]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java7]]></category>

		<guid isPermaLink="false">http://www.giddyplanet.com/?p=99</guid>
		<description><![CDATA[Java 7 is just around the corner. Sadly it does not contain the highly anticipated closures/lambda expressions, but instead it includes a set of small language changes developed under the codename &#8220;Project Coin&#8221;. In this post I will explore the &#8230; <a href="http://giddyplanet.com/2011/07/java-7-project-coin/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Java 7 is just around the corner. Sadly it does not contain the highly anticipated closures/lambda expressions, but instead it includes a set of small language changes developed under the codename &#8220;Project Coin&#8221;.</p>
<p>In this post I will explore the basic language changes.</p>
<h1>Strings in switch</h1>
<p>Yes, finally you can switch on string constants in switch statements!</p>
<div id="gist-1087499" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">executeCommand</span><span class="o">(</span><span class="n">String</span> <span class="n">command</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">switch</span> <span class="o">(</span><span class="n">command</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="s">&quot;start&quot;</span><span class="o">:</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">startService</span><span class="o">();</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="o">;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="s">&quot;stop&quot;</span><span class="o">:</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">stopService</span><span class="o">();</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="o">;</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="s">&quot;status&quot;</span><span class="o">:</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">showStatus</span><span class="o">();</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="o">;</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">default</span><span class="o">:</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">unknownCommand</span><span class="o">();</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC19'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1087499/e94bc9409c094dc8aa06e42be549101e76f655c4/StringBasedSwitch.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1087499#file_string_based_switch.java" style="float:right;margin-right:10px;color:#666">StringBasedSwitch.java</a>
            <a href="https://gist.github.com/1087499">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>&nbsp;</p>
<h1>Binary Literals and Underscores in Numeric Literals</h1>
<p>If you work with computer graphics, signal processing or integrate with old native C libraries, you often have to perform bit-manipulations and bit-masking etc. For some strange reason Java only supported decimal, octal and hexadecimal literals. Java Coin adds support for binary literals with the 0b prefix, and now it is legal to use underscores inside all numeric literals to make them easier to read.</p>
<div id="gist-1087499" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">numericLiterals</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">mask1</span> <span class="o">=</span> <span class="mi">0</span><span class="n">b1000100010001000</span><span class="o">;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">mask2</span> <span class="o">=</span> <span class="mi">0</span><span class="n">b1000_1000_1000_1000</span><span class="o">;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">color</span> <span class="o">=</span> <span class="mh">0xff</span><span class="n">_f0_fe_ff</span><span class="o">;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">price</span> <span class="o">=</span> <span class="mi">1</span><span class="n">_000_000</span><span class="o">;</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">double</span> <span class="n">PI</span> <span class="o">=</span> <span class="mf">3.141</span><span class="n">_592_653_589_793d</span><span class="o">;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1087499/67c0c7e15289de6709bb5d450f0ac2c9af49d3dc/NumericLiterals.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1087499#file_numeric_literals.java" style="float:right;margin-right:10px;color:#666">NumericLiterals.java</a>
            <a href="https://gist.github.com/1087499">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h1> Type Inference for Constructing Instances of Generic Types</h1>
<p>This feature is going to remove a lot the verbosity of generic types. Take for example this example of a Map of a List of Strings:</p>
<div id="gist-1087499" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">createGenerics</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;();</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;&gt;</span> <span class="n">keyValues</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;&gt;();</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">createWithDiamondOperator</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;&gt;</span> <span class="n">keyValues</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;();</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1087499/19883d414eaf96cb4dea6fb69eb98bd49fbe06de/DiamondOperator.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1087499#file_diamond_operator.java" style="float:right;margin-right:10px;color:#666">DiamondOperator.java</a>
            <a href="https://gist.github.com/1087499">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The first method demonstrates the old way of creating generified collections.</p>
<p>The second method demonstrates how you can save a lot of typing by using the diamond operator <> and let the compiler infer the types from the left hand side.</p>
<h1>Multi-Catch Exceptions</h1>
<p>If you want to perform the same exception handling for a set of Exception types you can now catch them in a single catch statement by separating the types with a pipe character</p>
<div id="gist-1087499" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">multiCatch</span><span class="o">(</span><span class="n">Class</span><span class="o">&lt;</span><span class="n">MultiCatch</span><span class="o">&gt;</span> <span class="n">myClass</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Method</span> <span class="n">method</span> <span class="o">=</span> <span class="n">myClass</span><span class="o">.</span><span class="na">getMethod</span><span class="o">(</span><span class="s">&quot;sayHello&quot;</span><span class="o">);</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">NoSuchMethodException</span> <span class="o">|</span> <span class="n">SecurityException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// common exception handling logic</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC8'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1087499/4533a4aab6c6e97af08d9e20289c0733e6aac955/MultiCatch.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1087499#file_multi_catch.java" style="float:right;margin-right:10px;color:#666">MultiCatch.java</a>
            <a href="https://gist.github.com/1087499">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h1>Automatic Resource Management</h1>
<p>The final part of Project Coin is support for automatic resource management, which I will explore in a separate blog post.</p>
]]></content:encoded>
			<wfw:commentRss>http://giddyplanet.com/2011/07/java-7-project-coin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple HTML5 Todo List</title>
		<link>http://giddyplanet.com/2011/05/simple-html5-todo-list/</link>
		<comments>http://giddyplanet.com/2011/05/simple-html5-todo-list/#comments</comments>
		<pubDate>Wed, 18 May 2011 15:19:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[ender]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.giddyplanet.com/?p=90</guid>
		<description><![CDATA[Lately I have been experimenting with Ender. It is a JavaScript library with a twist. In fact the authors refer to it as &#8220;no-library library&#8221;. It is a tiny wrapper and a command line tool that you can use to &#8230; <a href="http://giddyplanet.com/2011/05/simple-html5-todo-list/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Lately I have been experimenting with <a title="Ender - the no-library library" href="http://ender.no.de/">Ender</a>. It is a JavaScript library with a twist. In fact the authors refer to it as &#8220;no-library library&#8221;. It is a tiny wrapper and a command line tool that you can use to construct a Javascript library from a range of microframeworks. This way you only get the features you actually need, and in theory you can pick and choose between alternative implementations of the individual featues, so you get a library that matches your preferred programming style.</p>
<p>I have created a small todo-list that uses local storage in the browser to persist your todo items. This way, the todolist will remember your items from last visit, without storing anything on the server!</p>
<ul>
<li>You can try it out here: <a title="Simple ToDo List" href="http://www.giddyplanet.com/stuff/enderon/simpletodo.html">Simple ToDo List</a>.</li>
<li>The source is available at <a title="Enderon Simple ToDo List at github" href="https://github.com/rhmoller/Enderon">https://github.com/rhmoller/Enderon</a> under the <a title="MIT License" href="http://www.opensource.org/licenses/mit-license">MIT License</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://giddyplanet.com/2011/05/simple-html5-todo-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canvasteroids</title>
		<link>http://giddyplanet.com/2010/10/canvasteroids/</link>
		<comments>http://giddyplanet.com/2010/10/canvasteroids/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 16:45:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.giddyplanet.com/?p=54</guid>
		<description><![CDATA[A couple of weeks ago I wrote a small game using Javascript and HTML5 canvas. It is a clone of the classic Asteroids game by Atari Inc. The game is not complete yet, but I have decided to put it &#8230; <a href="http://giddyplanet.com/2010/10/canvasteroids/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago I wrote a small game using Javascript and HTML5 canvas. It is a clone of the classic Asteroids game by Atari Inc.</p>
<div id="attachment_75" class="wp-caption alignnone" style="width: 160px"><a href="http://www.giddyplanet.com/wp-content/2010/10/canvasteroids.png"><img src="http://www.giddyplanet.com/wp-content/2010/10/canvasteroids-150x150.png" alt="A screenshot of the game in action" title="CanvAsteroids Screenshot" width="150" height="150" class="size-thumbnail wp-image-75" /></a><p class="wp-caption-text">CanvAsteroids Screenshot</p></div>
<p>The game is not complete yet, but I have decided to put it up anyway. You can try it out here: <a href="http://www.giddyplanet.com/projects/canvasteroids/">CanvAsteroids</a></p>
]]></content:encoded>
			<wfw:commentRss>http://giddyplanet.com/2010/10/canvasteroids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fra LED til Simon</title>
		<link>http://giddyplanet.com/2010/07/fra-led-til-simon/</link>
		<comments>http://giddyplanet.com/2010/07/fra-led-til-simon/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 15:53:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[hackaarhus]]></category>
		<category><![CDATA[osaa]]></category>

		<guid isPermaLink="false">http://www.giddyplanet.com/?p=33</guid>
		<description><![CDATA[Here is the slides, a video and the source code from a 15 minute speed-talk I did for the Tech Talk Tuesday at OSAA yesterday. It shows the highlights of how to build a clone of the classic Simon game, &#8230; <a href="http://giddyplanet.com/2010/07/fra-led-til-simon/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="__ss_4701960" style="width: 425px;">Here is the slides, a video and the source code from a 15 minute speed-talk I did for the Tech Talk Tuesday at OSAA yesterday.</div>
<div style="width: 425px;">It shows the highlights of how to build a clone of the classic Simon game, using an Arduino and some buttons and LEDs. In Danish only!</div>
<h2 style="width: 425px;">Slides</h2>
<div style="width: 425px;"><strong style="display: block; margin: 12px 0 4px;"><a title="Fra LED til Simon" href="http://www.slideshare.net/giddy/osaa-simon">Fra LED til Simon</a></strong><object id="__sse4701960" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=osaa-simon-100707103654-phpapp01&amp;stripped_title=osaa-simon" /><param name="name" value="__sse4701960" /><param name="allowfullscreen" value="true" /><embed id="__sse4701960" type="application/x-shockwave-flash" width="425" height="355" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=osaa-simon-100707103654-phpapp01&amp;stripped_title=osaa-simon" name="__sse4701960" allowscriptaccess="always" allowfullscreen="true"></embed></object></div>
<div id="__ss_4701960" style="width: 425px;">
<div style="padding: 5px 0 12px;">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/giddy">giddy</a>.</div>
<h2 style="padding: 5px 0 12px;">The Video Demonstration</h2>
</div>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/hMDYLVI37HQ&amp;hl=en_US&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/hMDYLVI37HQ&amp;hl=en_US&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<h2>Source Code</h2>
<pre class="brush: cpp">
/*
 * Arduino Clone of the classic Simon Game
 *
 * Made at Hack Aarhus / OSAA
 * http://www.hackaarhus.dk
 * http://www.osaa.dk
 *
 * Released into the public domain by
 * Rene Hangstrup Moeller, July 2010
 *
 * http://www.giddyplanet.com
 */

//---------------------------------------------------------
//	GLOBALS weeeee!
//=========================================================

// the number of leds/switches
int SIZE = 4;

// milliseconds delay between each element in the sequence
int PLAY_DELAY = 250;
int INPUT_DELAY = 200;

// the sequence
int sequence[128];

// current sequence length
int seqlen = 0;

// Sound frequencies for each led-switch pair
int notes[] = {
    261/2, 392/2, 587/2, 880/2
};

//---------------------------------------------------------
//	PIN MAPPINGS
//=========================================================

// the buzzer control pin
int buzzPin = 11;

// calculate pin for led
int ledPin(int num) {
  return (num + 1) * 2;
}

// calculate pin for button
int buttonPin(int num) {
  return ledPin(num) + 1;
}

//---------------------------------------------------------
//	HELPER FUNCTIONS
//=========================================================

// mute the buzzer and turn off all leds
void turnOff() {
    noTone(buzzPin);
    for (int n = 0; n < SIZE; n++) {
      digitalWrite(ledPin(n), LOW);
    }
}

// new game
void newGame() {
  // generate random sequence with two entries
  sequence[0] = random(SIZE);
  sequence[1] = random(SIZE);
  seqlen = 2;

  // blink leds 10 times to indicate game start
  for (int i = 0; i < 10; i++) {
    for (int n = 0; n < SIZE; n++) {
      digitalWrite(ledPin(n), HIGH);
    }
    delay(200);

    turnOff();
    delay(200);
  }
}

// add an entry to the sequence
void expand() {
  sequence[seqlen++] = random(SIZE);
}

// play the sequence
void playSequence() {
  Serial.println("Playing sequence");

  for (int i = 0; i < seqlen; i++) {
    int v = sequence[i];
    tone(buzzPin, notes[v]);

    for (int n = 0; n < SIZE; n++) {
      digitalWrite(ledPin(n), n == v ? HIGH : LOW);
    }

    delay(PLAY_DELAY);
    noTone(buzzPin);

    for (int n = 0; n < SIZE; n++) {
      digitalWrite(ledPin(n), LOW);
    }

    delay(PLAY_DELAY);
  }
  turnOff();
}

// wait for button press
int nextButton() {

  while (true) {

    for (int n = 0; n < SIZE; n++) {
      if (digitalRead(buttonPin(n)) == HIGH) {
        digitalWrite(ledPin(n), HIGH);

        while (digitalRead(buttonPin(n)) == HIGH) {
          delay(INPUT_DELAY);
        }

        turnOff();
        return n;
      }
    }
    delay(INPUT_DELAY);
  }

  return 0; // never
}

// read sequence from player
int readSequence() {
  for (int i = 0; i < seqlen; i++) {
    int nb = nextButton();

    if (nb != sequence[i]) {
      Serial.println("Error");
      return false;
    }
  }

  Serial.println("Accepted");
  return true;
}

//---------------------------------------------------------
//	ARDUINO ENTRY POINTS
//=========================================================

// initialize the Arduino
void setup() {
  // initialize random number sequence
  randomSeed(micros() + analogRead(0));

  // configure pins for LEDs and buttons
  for (int i = 0; i < SIZE; i++) {
    pinMode(ledPin(i), OUTPUT);
    pinMode(buttonPin(i), INPUT);
  }

  // configure pin for sound
  pinMode(buzzPin, OUTPUT);

  // initialize serial communication for debugging
  Serial.begin(9600);
  Serial.println("Setup completed. Welcome to Simon");
}

// The game loop - each run corresponds to a game
void loop() {
  Serial.println("Starting new game");
  newGame();

  int running = true;

  while (running) {
    delay(1000);
    playSequence();
    running = readSequence();
    if (running) {
      expand();
    }
  }   

  delay(250);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://giddyplanet.com/2010/07/fra-led-til-simon/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Simplicity</title>
		<link>http://giddyplanet.com/2009/12/simplicity/</link>
		<comments>http://giddyplanet.com/2009/12/simplicity/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 10:14:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.giddyplanet.com/?p=19</guid>
		<description><![CDATA[Stumbled upon this fantastic quote: It seems strange to have to emphasize simplicity. You&#8217;d think simple would be the default. Ornate is more work. But something seems to come over people when they try to be creative. Beginning writers adopt &#8230; <a href="http://giddyplanet.com/2009/12/simplicity/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Stumbled upon this fantastic quote:</p>
<blockquote><p><span style="font-family: verdana; font-size: x-small;">It seems strange to have to emphasize simplicity. You&#8217;d think simple would be the default.  Ornate is more work.  But something seems to come over people when they try to be creative.  Beginning writers adopt    a pompous tone that doesn&#8217;t sound anything like the way  they speak.  Designers trying to be artistic resort to swooshes and curlicues.  Painters discover that they&#8217;re expressionists. It&#8217;s all evasion. Underneath the long words or the &#8220;expressive&#8221; brush strokes, there is not much going on, and that&#8217;s frightening.</span></p>
<p><span style="font-family: verdana; font-size: x-small;">When you&#8217;re forced to be simple, you&#8217;re forced to face the real problem. When you can&#8217;t deliver ornament, you have to deliver substance.</span></p>
<p><a title="Taste for Makers" href="http://www.paulgraham.com/taste.html" target="_self">http://www.paulgraham.com/taste.html</a></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://giddyplanet.com/2009/12/simplicity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello World</title>
		<link>http://giddyplanet.com/2009/11/hello-world-2/</link>
		<comments>http://giddyplanet.com/2009/11/hello-world-2/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 16:07:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.giddyplanet.com/?p=17</guid>
		<description><![CDATA[&#8230;please hold&#8230;]]></description>
			<content:encoded><![CDATA[<p>&#8230;please hold&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://giddyplanet.com/2009/11/hello-world-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

