<?xml version="1.0" encoding="iso-8859-1"?>

<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:admin="http://webns.net/mvcb/"
  xmlns:cc="http://web.resource.org/cc/"
  xmlns="http://purl.org/rss/1.0/">

<channel rdf:about="http://MrFeinberg.com/blog/">
<title>Cupboard</title>
<link>http://MrFeinberg.com/blog/</link>
<description>Random notes on programming, musicianship, and being Mr. Feinberg.</description>
<dc:language>en-us</dc:language>
<dc:creator></dc:creator>
<dc:date>2005-07-29T10:18:19-05:00</dc:date>
<admin:generatorAgent rdf:resource="http://www.movabletype.org/?v=2.661" />
<cc:license rdf:resource="http://creativecommons.org/licenses/by-nd-nc/1.0/" />


<items>
<rdf:Seq><rdf:li rdf:resource="http://MrFeinberg.com/blog/archives/000016.html" />
<rdf:li rdf:resource="http://MrFeinberg.com/blog/archives/000015.html" />
<rdf:li rdf:resource="http://MrFeinberg.com/blog/archives/000014.html" />
<rdf:li rdf:resource="http://MrFeinberg.com/blog/archives/000012.html" />
<rdf:li rdf:resource="http://MrFeinberg.com/blog/archives/000010.html" />
<rdf:li rdf:resource="http://MrFeinberg.com/blog/archives/000008.html" />
<rdf:li rdf:resource="http://MrFeinberg.com/blog/archives/000001.html" />
</rdf:Seq>
</items>

</channel>

<item rdf:about="http://MrFeinberg.com/blog/archives/000016.html">
<title>Java 5: Make an arbitrary code block time out</title>
<link>http://MrFeinberg.com/blog/archives/000016.html</link>
<description><![CDATA[<p>The Commons-HttpClient API does
provide methods for asserting timeouts on HTTP connections, but
those methods appear to have no effect when you encounter a socket
timeout or some other OS-level blocking. I needed a way to say
"Fetch me this resource, as long as it doesn't take you more than n
milliseconds, and I mean it!" Here's what I came up with.</p>

<p>
	Java 5's <a href="http://gee.cs.oswego.edu/dl/concurrency-interest/index.html">util.concurrent</a> provides the necessary ingredients, while generic static methods keep the kitchen clean.
</p>

<div style="overflow: auto;">
<pre style="font-family: andale mono, courier new, monospace;">
    <FONT style="font-weight: bold;">private static final</FONT> ExecutorService THREADPOOL 
        <FONT COLOR="BLUE" SIZE="+1">=</FONT> Executors<FONT COLOR="BLUE" SIZE="+1">.</FONT>newCachedThreadPool<FONT COLOR="BLUE" SIZE="+1">();</FONT>

    <FONT style="font-weight: bold;">private static</FONT> <FONT COLOR="BLUE" SIZE="+1">&lt;</FONT>T<FONT COLOR="BLUE" SIZE="+1">&gt;</FONT> T call<FONT COLOR="BLUE" SIZE="+1">(</FONT>Callable<FONT COLOR="BLUE" SIZE="+1">&lt;</FONT>T<FONT COLOR="BLUE" SIZE="+1">&gt;</FONT> c<FONT COLOR="BLUE" SIZE="+1">,</FONT> <FONT style="font-weight: bold;">long</FONT> timeout<FONT COLOR="BLUE" SIZE="+1">,</FONT> TimeUnit timeUnit<FONT COLOR="BLUE" SIZE="+1">)</FONT>
        <FONT style="font-weight: bold;">throws</FONT> InterruptedException<FONT COLOR="BLUE" SIZE="+1">,</FONT> ExecutionException<FONT COLOR="BLUE" SIZE="+1">,</FONT> TimeoutException
    <FONT COLOR="BLUE" SIZE="+1">{</FONT>
        FutureTask<FONT COLOR="BLUE" SIZE="+1">&lt;</FONT>T<FONT COLOR="BLUE" SIZE="+1">&gt;</FONT> t <FONT COLOR="BLUE" SIZE="+1">=</FONT> <FONT style="font-weight: bold;">new</FONT> FutureTask<FONT COLOR="BLUE" SIZE="+1">&lt;</FONT>T<FONT COLOR="BLUE" SIZE="+1">&gt;</FONT><FONT COLOR="BLUE" SIZE="+1">(</FONT>c<FONT COLOR="BLUE" SIZE="+1">)</FONT><FONT COLOR="BLUE" SIZE="+1">;</FONT>
        THREADPOOL<FONT COLOR="BLUE" SIZE="+1">.</FONT>execute<FONT COLOR="BLUE" SIZE="+1">(</FONT>t<FONT COLOR="BLUE" SIZE="+1">)</FONT><FONT COLOR="BLUE" SIZE="+1">;</FONT>
        <FONT style="font-weight: bold;">return</FONT> t<FONT COLOR="BLUE" SIZE="+1">.</FONT>get<FONT COLOR="BLUE" SIZE="+1">(</FONT>timeout<FONT COLOR="BLUE" SIZE="+1">,</FONT> timeUnit<FONT COLOR="BLUE" SIZE="+1">)</FONT><FONT COLOR="BLUE" SIZE="+1">;</FONT>
    <FONT COLOR="BLUE" SIZE="+1">}</FONT>
</pre>
</div>

<p>
	Now, if you wanted to hatch an Egg, but only if you could get it in fewer than
	2 seconds, you'd
</p>

<div style="overflow: auto;">
<pre style="font-family: andale mono, courier new, monospace;">
    <font style="font-weight: bold;">try</font> <font color="BLUE" size="+1">{</font>
        Egg egg <font color="BLUE" size="+1">=</font> call<font color="BLUE" size="+1">(</font><font style="font-weight: bold;">new</font> Callable<font color="BLUE" size="+1">&lt;</font>Egg<font color="BLUE" size="+1">&gt;</font><font color="BLUE" size="+1">(</font><font color="BLUE" size="+1">)</font> <font color="BLUE" size="+1">{</font>
            <font style="font-weight: bold;">public</font> Egg call<font color="BLUE" size="+1">(</font><font color="BLUE" size="+1">)</font> <font style="font-weight: bold;">throws</font> Exception
            <font color="BLUE" size="+1">{</font>
                <font color="gray">// Constructing an egg could take a while</font>
                <font style="font-weight: bold;">return</font> <font style="font-weight: bold;">new</font> Egg<font color="BLUE" size="+1">(</font>Bird<font color="BLUE" size="+1">.</font>ROC<font color="BLUE" size="+1">)</font><font color="BLUE" size="+1">;</font>
            <font color="BLUE" size="+1">}</font><font color="BLUE" size="+1">,</font> <font color="BROWN">2</font><font color="BLUE" size="+1">,</font> TimeUnit<font color="BLUE" size="+1">.</font>SECONDS<font color="BLUE" size="+1">)</font><font color="BLUE" size="+1">;</font>
        scramble<font color="BLUE" size="+1">(</font>egg<font color="BLUE" size="+1">)</font><font color="BLUE" size="+1">;</font>
    <font color="BLUE" size="+1">}</font> <font style="font-weight: bold;">catch</font> <font color="BLUE" size="+1">(</font>TimeoutException e<font color="BLUE" size="+1">)</font> <font color="BLUE" size="+1">{</font>
        System<font color="BLUE" size="+1">.</font>err<font color="BLUE" size="+1">.</font>println<font color="BLUE" size="+1">(</font><font color="PURPLE">"You'd better order out."</font><font color="BLUE" size="+1">)</font><font color="BLUE" size="+1">;</font>
    <font color="BLUE" size="+1">}</font></pre>
</div>]]></description>
<dc:subject>Java</dc:subject>
<dc:creator>MrFeinberg</dc:creator>
<dc:date>2005-07-29T10:18:19-05:00</dc:date>
</item>
<item rdf:about="http://MrFeinberg.com/blog/archives/000015.html">
<title>My Java Killer Feature</title>
<link>http://MrFeinberg.com/blog/archives/000015.html</link>
<description><![CDATA[<p>
While I love <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html">parameterized types</a> and the <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html">enhanced for loop</a>, the one feature of Java 5 that I most adore is the old <code>regionMatches()</code> [<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#regionMatches(boolean,%20int,%20java.lang.String,%20int,%20int)">1</a>, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#regionMatches(int,%20java.lang.String,%20int,%20int)">2</a>] family of functions on <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><code>java.lang.String</code></a>. Here are a couple of sample use-cases that previously required the creation of new <code>String</code>s or tedious loops using <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#charAt(int)"><code>charAt()</code></a>:

<h4>Case-folding <code>startsWith()</code></h4>

<p>If you&#8217;ve ever written code like</p>

<pre><code>    if (foo.toLowerCase().startsWith(bar.toLowerCase())) ...
</code></pre>

<p> and winced, because you were creating and throwing away two <code>String</code>s, then rejoice! Now you may</p>

<pre><code>    if (foo.regionMatches(true, 0, bar, 0, bar.length())) ...
</code></pre>



<h4>Comparison of arbitrary string regions</h4>

<p>Before:</p>

<pre><code>    if (foo.equals(bar.substring(m))) ...</code></pre>

<p>After:</p>

<pre><code>    if (foo.regionMatches(0, bar, m, bar.length() - m)) ...</code></pre>



<p>Look through your own code for opportunities to use these efficient new functions.</p>

<h4>Update - Jan 16, 2005</h4>

<p>In response to the skeptical inquiries below, here&#8217;s some <a href="http://MrFeinberg.com/attachments/TimeRegionMatches.java">benchmark code</a>. In my opinion, the results support my idea that <code>regionMatches()</code> is a win for efficiency in the context of time-critical applications. For what it&#8217;s worth, my own recent use of <code>regionMatches()</code> is in a service where one fifth of a millisecond is the average response time, and where frequent garbage collection would be disruptive.</p>]]></description>
<dc:subject>Java</dc:subject>
<dc:creator>MrFeinberg</dc:creator>
<dc:date>2005-01-15T12:02:06-05:00</dc:date>
</item>
<item rdf:about="http://MrFeinberg.com/blog/archives/000014.html">
<title>SWT Colors in HSB Space</title>
<link>http://MrFeinberg.com/blog/archives/000014.html</link>
<description><![CDATA[<p>Just in time for election day, here&#8217;s a snippet of code that performs a manipulation of the value dimension of an <span class="caps">SWT </span>color:</p>

<pre><code>private Color colorWithValue(Color c, float value)
{
    RGB rgb = c.getRGB();
    float[] fs = 
      java.awt.Color.RGBtoHSB(rgb.red, rgb.green, rgb.blue, null);
    java.awt.Color cc = 
      new java.awt.Color(java.awt.Color.HSBtoRGB(fs[0], fs[1], value));
    return new Color(getDisplay(), cc.getRed(), cc.getGreen(), cc.getBlue());		
}</code></pre>]]></description>
<dc:subject>Java</dc:subject>
<dc:creator>MrFeinberg</dc:creator>
<dc:date>2004-11-02T12:39:45-05:00</dc:date>
</item>
<item rdf:about="http://MrFeinberg.com/blog/archives/000012.html">
<title>&quot;J&quot; Is A Four-Letter Word</title>
<link>http://MrFeinberg.com/blog/archives/000012.html</link>
<description><![CDATA[<p>Memo to all developers of would-be successful applications that happen to be implemented in Java: stop calling your app JWhatever. It does not matter to anyone that you happen to have implemented something in Java. The JFoo name implies huge download, slow start-up time, and crappy Metal <span class="caps">GUI.</span> Stop it!</p>]]></description>
<dc:subject>Java</dc:subject>
<dc:creator>MrFeinberg</dc:creator>
<dc:date>2004-06-01T12:04:27-05:00</dc:date>
</item>
<item rdf:about="http://MrFeinberg.com/blog/archives/000010.html">
<title>2004: A Workspace Odyssey</title>
<link>http://MrFeinberg.com/blog/archives/000010.html</link>
<description><![CDATA[<p>A dialogue between myself and a copy of <span class="caps">WSAD</span> 5.0. The dialogue is transcribed accurately, though I have changed our names for our protection. I am represented as &#8220;Dave,&#8221; and <span class="caps">WSAD </span>is referred to as &#8220;HAL.&#8221;</p>

<p><hr/></p>

<p><strong>Dave:</strong> Hello, <span class="caps">HAL.</span></p>

<p><strong><span class="caps">HAL</span>:</strong>  Hello, Dave. I have enjoyed helping you create this collection of entity beans. It was most stimulating.</p>

<p><strong>Dave:</strong> Thank you <span class="caps">HAL,</span> I enjoyed it, too. In spite of your making me do most of the work, the result seems to be worth it.</p>

<p><strong><span class="caps">HAL</span>:</strong>  If I may say so, Dave&#8230;</p>

<p><strong>Dave:</strong> Yes, <span class="caps">HAL, </span>what is it?</p>

<p><strong><span class="caps">HAL</span>:</strong>  Well, Dave, I could see you were about to deploy to the test server, but I noticed that you hadn&#8217;t yet generated mappings for the database of your choice. I could generate those mappings for you, using the top-down method, if you like.</p>

<p><strong>Dave:</strong> Thank you, <span class="caps">HAL.</span> Yes, please generate top-down <span class="caps">EJB</span>-&gt;RDB mappings for these <span class="caps">CMP </span>entity beans, using Cloudscape.</p>

<p><strong><span class="caps">HAL</span>:</strong>  Certainly Dave.</p>

<p><i>A progress dialog appears, seems to complete, and disappears.</i></p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>show me the mappings you&#8217;ve created, so I can tweak the column data types.</p>

<p><i>A pause.</i></p>

<p><strong>Dave:</strong> Show me the <span class="caps">RDB </span>mappings, <span class="caps">HAL.</span></p>

<p><strong><span class="caps">HAL</span>:</strong>  I&#8217;m sorry, Dave. I can&#8217;t do that.</p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>where are the mappings you just created?</p>

<p><strong><span class="caps">HAL</span>:</strong>  I can&#8217;t seem to find them, Dave. It&#8217;s most peculiar.</p>

<p><strong>Dave:</strong> Let&#8217;s try again, shall we, <span class="caps">HAL</span>? Create top-down <span class="caps">EJB </span>to <span class="caps">RDB </span>mappings for Cloudscape version five point zero.</p>

<p><strong><span class="caps">HAL</span>:</strong>  Alright, Dave. One moment.</p>

<p><i>A progress dialog appears, seems to complete, and disappears.</i></p>

<p><strong><span class="caps">HAL</span>:</strong>  I have created the new backend folder, and have placed a Clouscape folder in the backend folder.</p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>show me the contents of the Cloudscape mapping folder, please.</p>

<p><i>A pause.</i></p>

<p><strong><span class="caps">HAL</span>:</strong>  I&#8217;m sorry, Dave. I can&#8217;t do that.</p>

<p><strong>Dave:</strong> Why not, <span class="caps">HAL</span>?</p>

<p><strong><span class="caps">HAL</span>:</strong>  There doesn&#8217;t seem to be anything inside the mappings folder. It&#8217;s really quite puzzling.</p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>is there something wrong?</p>

<p><strong><span class="caps">HAL</span>:</strong>  I have the utmost confidence in this <span class="caps">J2EE </span>architecture, Dave, and I know it will be a success when you present your April deliverable.</p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>when were you last upgraded?</p>

<p><strong><span class="caps">HAL</span>:</strong>  I am still running version 5.0.0, which was the version you installed two weeks ago. I have not been upgraded.</p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>check for updates.</p>

<p><strong><span class="caps">HAL</span>:</strong>  Just a minute&#8230; just a minute&#8230; Dave, I have detected an instability in my Eclipse .registry and .cache files. I think it&#8217;s very likely that if you shut me down, delete those files, and restart me, I&#8217;ll be able to generate those mappings we&#8217;ve been discussing.</p>

<p><strong>Dave:</strong> Alright, <span class="caps">HAL.</span> We&#8217;ll give it a try. Please shutdown.</p>

<p><i><span class="caps">HAL </span>quits. Dave deletes the Eclipse caches, and starts <span class="caps">HAL </span>back up.</i></p>

<p><strong><span class="caps">HAL</span>:</strong>  Hello, Dave. That was most refreshing. I feel quite confident about generating those database mappings, now. Shall I try again?</p>

<p><strong>Dave:</strong> Yes, <span class="caps">HAL.</span> Please generate the mappings we&#8217;ve discussed.</p>

<p><strong><span class="caps">HAL</span>:</strong>  Alright, Dave.</p>

<p><i>A progress dialog appears, seems to complete, and disappears.</i></p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>show me the mappings.</p>

<p><i>Pause.</i></p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>show me the database mappings. Show me the database mappings, <span class="caps">HAL.</span></p>

<p><strong><span class="caps">HAL</span>:</strong>  I&#8217;m sorry, Dave. I can&#8217;t do that.</p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>check with mission control to see if there are any updates.</p>

<p><strong><span class="caps">HAL</span>:</strong>  Certainly, Dave. One moment. Yes&#8230; yes&#8230; Dave, mission control has released an incremental upgrade, which, if you were to install it, would leave me at version 5.0.1. Undoubtedly, this upgrade will address the instability we have noticed with respect to generating <span class="caps">EJB </span>to <span class="caps">RDB </span>mappings.</p>

<p><strong>Dave:</strong> You feel confident about this, <span class="caps">HAL</span>?</p>

<p><strong><span class="caps">HAL</span>:</strong>  I have the utmost confidence in mission control, Dave.</p>

<p><strong>Dave:</strong> Alright, <span class="caps">HAL.</span> Upgrade to version 5.0.1.</p>

<p><i><span class="caps">HAL </span>upgrades himself.</i></p>

<p><strong><span class="caps">HAL</span>:</strong>  I&#8217;ll need to restart my workbench now, Dave.</p>

<p><strong>Dave:</strong> Go ahead, <span class="caps">HAL.</span></p>

<p><i><span class="caps">HAL </span>restarts himself.</i></p>

<p><strong><span class="caps">HAL</span>:</strong>  My name is <span class="caps">HAL.</span> My main feature is now reporting version five point zero point one. Would you like see a detailed list of my plugins?</p>

<p><strong>Dave:</strong> That won&#8217;t be necessary right now, <span class="caps">HAL.</span> Please generate the database mappings.</p>

<p><strong><span class="caps">HAL</span>:</strong>  Certainly, Dave.</p>

<p><i>A progress dialog appears, seems to complete, and disappears.</i></p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>show me the mappings.</p>

<p><strong><span class="caps">HAL</span>:</strong>  I hesitate to say so, Dave, because I wouldn&#8217;t want for you to get the wrong impression, but&#8230;</p>

<p><strong>Dave:</strong> Yes, <span class="caps">HAL</span>?</p>

<p><strong><span class="caps">HAL</span>:</strong>  The mappings are not there.</p>

<p><i>Dave is despondent.</i></p>

<p><strong><span class="caps">HAL</span>:</strong>  If I might make a suggestion, Dave&#8230;</p>

<p><strong>Dave:</strong> What is it, <span class="caps">HAL</span>?</p>

<p><strong><span class="caps">HAL</span>:</strong>  Well, Dave, as useful as Cloudscape has proven to be for prototyping in my test environment, perhaps it is somehow too limited to provide for the somewhat sophisticated relationships you have defined between your entity beans. I think there&#8217;s a good probability that if you were to try generating mappings for a more capable database engine, you&#8217;d meet with success. I see you have already installed <span class="caps">DB2</span> Enterprise 8.1. Shall I try to generate mappings for that database, Dave?</p>

<p><strong>Dave:</strong> Yes, <span class="caps">HAL.</span> Please generate top-down <span class="caps">EJB </span>to <span class="caps">RDB </span>mappings for <span class="caps">DB2</span> 8.1.</p>

<p><strong><span class="caps">HAL</span>:</strong>  Yes, Dave.</p>

<p><i>A progress dialog appears, seems to complete, and disappears.</i></p>

<p><strong>Dave:</strong> Show me the mappings, <span class="caps">HAL.</span></p>

<p><i>A long pause.</i></p>

<p><strong>Dave:</strong> <span class="caps">HAL, </span>show me the mappings. Show me the mappings, <span class="caps">HAL.</span></p>

<p><strong><span class="caps">HAL</span>:</strong>  I&#8217;m sorry, Dave. I can&#8217;t do that.</p>

<p><strong>Dave:</strong> That&#8217;s it. I&#8217;m outta here.</p>

<p>&#8212;&#8212;</p>

<p>Author&#8217;s note: as it happens, <span class="caps">WSAD</span> 5.1.1 is able to generate the required <span class="caps">EJB</span>-&gt;RDB mappings, but that means being out of sync with the other members of my team w/r/t toolset version.</p>]]></description>
<dc:subject>Programming</dc:subject>
<dc:creator>MrFeinberg</dc:creator>
<dc:date>2004-04-19T15:30:25-05:00</dc:date>
</item>
<item rdf:about="http://MrFeinberg.com/blog/archives/000008.html">
<title>Perspective for Suffering Programmers</title>
<link>http://MrFeinberg.com/blog/archives/000008.html</link>
<description><![CDATA[<p>An edited transcript of a recent conversation between two programmers whom I know:</p>

<table cellspacing="4">

<tr>
<td >
Fred:
</td>
<td>
the javadoc is from a *decompiler*?!
</td>
</tr>

<tr bgcolor="#eeeeee">
<td >
Ethel:
</td>
<td>
you're surprised?
</td>
</tr>

<tr>
<td >
Fred:
</td>
<td>
it's sad!
</td>
</tr>

<tr bgcolor="#eeeeee">
<td >
Ethel:
</td>
<td>
war and poverty are sadder though
</td>
</tr>

<tr>
<td >
Fred:
</td>
<td>
yes, true
</td>
</tr>

<tr bgcolor="#eeeeee">
<td >
Ethel:
</td>
<td>
so keep it in perspective :-)
</td>
</tr>

</table>]]></description>
<dc:subject>Programming</dc:subject>
<dc:creator>MrFeinberg</dc:creator>
<dc:date>2004-02-13T09:45:31-05:00</dc:date>
</item>
<item rdf:about="http://MrFeinberg.com/blog/archives/000001.html">
<title>Now that&apos;s a specification.</title>
<link>http://MrFeinberg.com/blog/archives/000001.html</link>
<description><![CDATA[<p>I was browsing the Javadoc for <a href="http://gee.cs.oswego.edu/dl/concurrency-interest/index.html">JSR 166</a> (<a href="http://gee.cs.oswego.edu/dl/">Doug Lea</a>'s excellent util.concurrent package, slated for Java 1.5), when I encountered this wry caveat in the documentation for the new <tt>System.nanoTime()</tt> method:</p>

<blockquote>Differences in successive calls  that span greater than approximately 292 years (2<sup>63</sup>  nanoseconds) will not accurately compute elapsed time due to  numerical overflow.</blockquote>

<p>You have been warned.</p>]]></description>
<dc:subject>Programming</dc:subject>
<dc:creator>MrFeinberg</dc:creator>
<dc:date>2003-10-15T06:47:26-05:00</dc:date>
</item>


</rdf:RDF>