<?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>Blog.Project13.pl &#187; tricks</title>
	<atom:link href="http://www.blog.project13.pl/index.php/tag/tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.project13.pl</link>
	<description>The Blog of a Coder</description>
	<lastBuildDate>Sun, 05 Feb 2012 02:28:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[Scala] Implicit Manifest tip</title>
		<link>http://www.blog.project13.pl/index.php/coding/1445/scala-implicit-manifest-tip/</link>
		<comments>http://www.blog.project13.pl/index.php/coding/1445/scala-implicit-manifest-tip/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 16:20:07 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[manifest]]></category>
		<category><![CDATA[marshalling]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=1445</guid>
		<description><![CDATA[Hello again! Today I&#8217;d like to show a nice little trick you can do in Scala to make your methods look more &#8220;like Scala&#8221;, and require less typing :-) Let&#8217;s take a look at the two methods bellow: // before: val cookie = unmarshall&#40;classOf&#91;Cookie&#93;, jsonRepresentingACookie&#41; &#160; // which is the same as the bellow sample [...]]]></description>
			<content:encoded><![CDATA[<p>Hello again! Today I&#8217;d like to show a nice little trick you can do in Scala to make your methods look more &#8220;like Scala&#8221;, and require less typing :-) Let&#8217;s take a look at the two methods bellow:</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// before:</span></div>
</li>
<li class="li1">
<div class="de1">val cookie = unmarshall<span class="br0">&#40;</span>classOf<span class="br0">&#91;</span>Cookie<span class="br0">&#93;</span>, jsonRepresentingACookie<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// which is the same as the bellow sample in Java</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Cookie cookie = unmarshall(Cookie.class, jsonRepresentingACookie)</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// after:</span></div>
</li>
<li class="li1">
<div class="de1">val cookie = unmarshall<span class="br0">&#91;</span>Cookie<span class="br0">&#93;</span><span class="br0">&#40;</span>jsonRepresentingACookie<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// which would look like this in Java:</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Cookie cookie = unmarshall&lt;cookie&gt;(json) // NOT enough&lt;/cookie&gt;</span></div>
</li>
</ol>
</div>
<p>In our example we&#8217;ll want to unmarshall a Cookie object from it&#8217;s string representation, a pretty typical task. In order to do that (for example with JAX-B), we&#8217;ll need a Class object we can pass to the deserializer &#8211; so it can work all the field mappings etc. Obviously the second example in Java just isn&#8217;t enough &#8211; we need the Class object, and just using &lt;Cookie&gt; isn&#8217;t enough to retain this type information. Scala obviously suffers from the exact same problem as it&#8217;s a JVM issue (type erasure, grrr!).</p>
<p>But even so, the above Scala code will work fine if we do some tricks in the method definition. We&#8217;ll resort to using: </p>
<ul>
<li> a second (implicit) argument list</li>
<li> Manifest[T]</li>
</ul>
<p>That&#8217;s a bunch of scary looking terms, but it&#8217;s actually quite easy. Let&#8217;s take a look at the definition of the Scala version of unmarshall.<br />
</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">def unmarshal<span class="br0">&#91;</span>T<span class="br0">&#93;</span><span class="br0">&#40;</span>str: <span class="kw3">String</span><span class="br0">&#41;</span><span class="br0">&#40;</span>implicit manifest: <span class="kw3">Manifest</span><span class="br0">&#91;</span>T<span class="br0">&#93;</span><span class="br0">&#41;</span>: T = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; val unmarshaller = createUnmarshaller<span class="br0">&#40;</span>manifest.<span class="me1">erasure</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; unmarshal<span class="br0">&#40;</span>str, unmarshaller<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>First, let&#8217;s analyze the signature of this method. Obviously it&#8217;s parameterized method, thus we need the [T] in front of it. Next is the argument list, with just one argument &#8211; str, the string representation we want to unmarshall &#8211; easy. Now for the more fun part &#8211; the <strong>second argument list</strong>. Yeah, you can have multiple argument lists in Scala. &#8220;Why?&#8221;, you ask? For example in our case the second list is implicit &#8211; this means that the compiler will figure out and try to fill in these arguments with matching types it finds in scope. In the case of supplying a Manifest[T] it&#8217;s fairly easy &#8211; it will just create the manifest based on the T type, during runtime at the call site of the method, and pass it into unmarshall.</p>
<p>A Manifest[T] can be thought of like a &#8220;better&#8221; a Class[_] which contains full type information &#8211; so for example we wouldn&#8217;t loose the information contained in a List&lt;Cookie&gt; like we would with plain Java (where we would have to hack around this JVM issue via a <a href="http://google-gson.googlecode.com/svn/tags/1.1.1/docs/javadocs/com/google/gson/reflect/TypeToken.html" title="Google Gson TypeToken" target="_blank" onclick="urchinTracker('/outgoing/google-gson.googlecode.com/svn/tags/1.1.1/docs/javadocs/com/google/gson/reflect/TypeToken.html?referer=');">TypeToken</a>). Anyway, thanks to the Manifest we&#8217;re able to get the Class we need to get our unmarshalling done! <strong>Manifest.erasure</strong> contains the type we need. If you want to read more about Manifest, the <a href="http://www.scala-lang.org/api/current/scala/reflect/Manifest.html" onclick="urchinTracker('/outgoing/www.scala-lang.org/api/current/scala/reflect/Manifest.html?referer=');">ScalaDoc</a> about it does a really nice job explaining how to use it.</p>
<p>&#8212; EXTRA for Scala Geeks &#8212; </p>
<p>You may have noticed the word &#8220;<strong>implicitly</strong>&#8221; appearing from time to time in Scala discussions. It&#8217;s a shorthand for accessing shorthand passed implicits. Let me explain this on our example &#8211; so instead of the code above we could do the following (which does the same thing):</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">def unmarshal<span class="br0">&#91;</span>T: <span class="kw3">Manifest</span><span class="br0">&#93;</span><span class="br0">&#40;</span>str: <span class="kw3">String</span><span class="br0">&#41;</span>: T = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; val unmarshaller = createUnmarshaller<span class="br0">&#40;</span>implicitly<span class="br0">&#91;</span><span class="kw3">Manifest</span><span class="br0">&#91;</span>T<span class="br0">&#93;</span><span class="br0">&#93;</span>.<span class="me1">erasure</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; unmarshal<span class="br0">&#40;</span>str, unmarshaller<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// and a quick Repl check if it really does what we think it should:</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// scala&gt; def unmarshal[T: Manifest](str: String) = {</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &nbsp; &nbsp; &nbsp;| implicitly[Manifest[T]].erasure</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &nbsp; &nbsp; &nbsp;| }</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// unmarshal: [T](str: String)(implicit evidence$1: Manifest[T])java.lang.Class[_]</span></div>
</li>
</ol>
</div>
<p>So instead of creating a second argument list we say that the Type parameter T will be used as a Manifest. And then we can access it via the implicitly magic method. This approach has been made possible since Scala 2.8, due to how often the above pattern was discovered in real life code.<br />
Anyway &#8211; it&#8217;s up to you which version you like the most.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/coding/1445/scala-implicit-manifest-tip/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[JDK7] java.util.Objects &#8211; forgotten feature?</title>
		<link>http://www.blog.project13.pl/index.php/coding/1284/jdk7-java-lang-objects-forgotten-feature/</link>
		<comments>http://www.blog.project13.pl/index.php/coding/1284/jdk7-java-lang-objects-forgotten-feature/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 09:08:51 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdk7]]></category>
		<category><![CDATA[lang]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=1284</guid>
		<description><![CDATA[This time just a quick post about something I&#8217;d really want to share with you all, and that&#8217;s been kind of forgotten during the release of JDK7 &#8211; java.util.Objects! Be sure to check out it&#8217;s javadoc to see what this small helper class can do for you :-) We&#8217;ll focus on two of it&#8217;s methods. [...]]]></description>
			<content:encoded><![CDATA[<p>This time just a quick post about something I&#8217;d really want to share with you all, and that&#8217;s been kind of forgotten during the release of JDK7 &#8211; <strong>java.util.Objects</strong>! Be sure to check out <a href="http://download.java.net/jdk7/docs/api/java/util/Objects.html" onclick="urchinTracker('/outgoing/download.java.net/jdk7/docs/api/java/util/Objects.html?referer=');">it&#8217;s <strong>javadoc</strong></a> to see what this small helper class can do for you :-) We&#8217;ll focus on two of it&#8217;s methods.</p>
<p>Now, we all remember how painful it it to write good equals and hashCode methods, yet we really should implement them to make our collections work the way they should work (HashAnythings, Lists, Sets). So we sometimes use <strong>Apache Commons</strong> to implement these, or just leave the coding to <strong>IntelliJ</strong> or <strong>NetBeans</strong>. The problem with the second, code generating, solution is that it&#8217;s a hell of a mess. Don&#8217;t believe me&#8230;? Just a quick reminder then&#8230;</p>
<div class="geshi no java">
<div class="head">class Test {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">private</span> <span class="kw3">String</span> &nbsp;name<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">private</span> <span class="kw3">String</span> &nbsp;surname<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">private</span> <span class="kw3">Integer</span> age<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw4">boolean</span> equals<span class="br0">&#40;</span><span class="kw3">Object</span> o<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">this</span> == o<span class="br0">&#41;</span> <span class="kw2">return</span> <span class="kw2">true</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>o == <span class="kw2">null</span> || getClass<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">!</span>= o.<span class="me1">getClass</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw2">return</span> <span class="kw2">false</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Test test = <span class="br0">&#40;</span>Test<span class="br0">&#41;</span> o<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>age <span class="sy0">!</span>= <span class="kw2">null</span> <span class="sy0">?</span> <span class="sy0">!</span>age.<span class="me1">equals</span><span class="br0">&#40;</span>test.<span class="me1">age</span><span class="br0">&#41;</span> : test.<span class="me1">age</span> <span class="sy0">!</span>= <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">return</span> <span class="kw2">false</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>name <span class="sy0">!</span>= <span class="kw2">null</span> <span class="sy0">?</span> <span class="sy0">!</span>name.<span class="me1">equals</span><span class="br0">&#40;</span>test.<span class="me1">name</span><span class="br0">&#41;</span> : test.<span class="me1">name</span> <span class="sy0">!</span>= <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">return</span> <span class="kw2">false</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>surname <span class="sy0">!</span>= <span class="kw2">null</span> <span class="sy0">?</span> <span class="sy0">!</span>surname.<span class="me1">equals</span><span class="br0">&#40;</span>test.<span class="me1">surname</span><span class="br0">&#41;</span> : test.<span class="me1">surname</span> <span class="sy0">!</span>= <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">return</span> <span class="kw2">false</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">true</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw4">int</span> hashCode<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> result = name <span class="sy0">!</span>= <span class="kw2">null</span> <span class="sy0">?</span> name.<span class="me1">hashCode</span><span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="nu0">0</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; result = <span class="nu0">31</span> <span class="sy0">*</span> result + <span class="br0">&#40;</span>surname <span class="sy0">!</span>= <span class="kw2">null</span> <span class="sy0">?</span> surname.<span class="me1">hashCode</span><span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; result = <span class="nu0">31</span> <span class="sy0">*</span> result + <span class="br0">&#40;</span>age <span class="sy0">!</span>= <span class="kw2">null</span> <span class="sy0">?</span> age.<span class="me1">hashCode</span><span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now that we&#8217;ve remembered how painful it looks, let&#8217;s dive into JDK7:  The full code would look like this (this time equals is also accepting subclasses of Test, the above example didn&#8217;t):</p>
<div class="geshi no java">
<div class="head">package pl.project13;</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import java.util.Objects;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Date: 4/19/11</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* @author Konrad Malawski</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> Test <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">private</span> <span class="kw3">String</span> &nbsp;name<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">private</span> <span class="kw3">String</span> &nbsp;surname<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">private</span> <span class="kw3">Integer</span> age<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> Test<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> Test<span class="br0">&#40;</span><span class="kw3">String</span> name, <span class="kw3">String</span> surname, <span class="kw3">Integer</span> age<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">name</span> = name<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">surname</span> = surname<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">age</span> = age<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> Test<span class="br0">&#40;</span><span class="kw3">String</span> name, <span class="kw3">String</span> surname<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">name</span> = name<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">surname</span> = surname<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw4">boolean</span> equals<span class="br0">&#40;</span><span class="kw3">Object</span> o<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">this</span> == o<span class="br0">&#41;</span> <span class="kw2">return</span> <span class="kw2">true</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><span class="br0">&#40;</span>o <span class="kw2">instanceof</span> Test<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw2">return</span> <span class="kw2">false</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &nbsp; &nbsp;if (o == null || getClass() != o.getClass()) return false;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Test that = <span class="br0">&#40;</span>Test<span class="br0">&#41;</span> o<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">boolean</span> result = <span class="kw2">true</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; result = result <span class="sy0">&amp;&amp;</span> Objects.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">this</span>.<span class="me1">name</span>, that.<span class="me1">name</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; result = result <span class="sy0">&amp;&amp;</span> Objects.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">this</span>.<span class="me1">surname</span>, that.<span class="me1">surname</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; result = result <span class="sy0">&amp;&amp;</span> Objects.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">this</span>.<span class="me1">age</span>, that.<span class="me1">age</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw4">int</span> hashCode<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> Objects.<span class="me1">hash</span><span class="br0">&#40;</span>name, surname, age<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Note the<strong> Objects.hash() </strong>method, wich is really a lifesaver and also the <strong>Objects.equals()</strong> method which makes our code NullPointerException safe, which the previous version had to tackle by using <strong>?:</strong>&#8230; And it read&#8217;s much nicer, Of course, you could skip the result variable, but I decided to do it like this for the sake of easy reading.</p>
<p>And here is it&#8217;s test:</p>
<div class="geshi no java">
<div class="head">package pl.project13;</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import java.util.HashSet;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import java.util.Set;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import java.util.TreeSet;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import static junit.framework.Assert.assertEquals;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import static junit.framework.Assert.assertFalse;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import static junit.framework.Assert.assertTrue;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Date: 4/19/11</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* @author Konrad Malawski</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TestTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @org.<span class="me1">junit</span>.<span class="me1">Test</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw4">void</span> testEquals<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <span class="kw3">Exception</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Test test = <span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="st0">&quot;Konrad&quot;</span>, <span class="st0">&quot;Malawski&quot;</span>, <span class="nu0">21</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// true</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertTrue<span class="br0">&#40;</span>test.<span class="me1">equals</span><span class="br0">&#40;</span>test<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertTrue<span class="br0">&#40;</span>test.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="st0">&quot;Konrad&quot;</span>, <span class="st0">&quot;Malawski&quot;</span>, <span class="nu0">21</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// false</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertFalse<span class="br0">&#40;</span>test.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="st0">&quot;Wacław&quot;</span>, <span class="st0">&quot;Malawski&quot;</span>, <span class="nu0">1825</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertFalse<span class="br0">&#40;</span>test.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="st0">&quot;Wacław&quot;</span>, <span class="st0">&quot;Malawski&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertFalse<span class="br0">&#40;</span>test.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertFalse<span class="br0">&#40;</span>test.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">Object</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertFalse<span class="br0">&#40;</span>test.<span class="me1">equals</span><span class="br0">&#40;</span><span class="kw2">null</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @org.<span class="me1">junit</span>.<span class="me1">Test</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw4">void</span> testHashCodeOnCollections<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <span class="kw3">Exception</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Test test = <span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="st0">&quot;Konrad&quot;</span>, <span class="st0">&quot;Malawski&quot;</span>, <span class="nu0">21</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> sizeBefore<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> sizeAfter<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">Set</span> treeSet = <span class="kw2">new</span> HashSet<span class="sy0">&amp;</span>lt<span class="sy0">;&amp;</span>gt<span class="sy0">;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; treeSet.<span class="me1">add</span><span class="br0">&#40;</span>test<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; sizeBefore = treeSet.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; treeSet.<span class="me1">add</span><span class="br0">&#40;</span>test<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; sizeAfter = treeSet.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertEquals<span class="br0">&#40;</span>sizeBefore, sizeAfter<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @org.<span class="me1">junit</span>.<span class="me1">Test</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw4">void</span> testHashCodeOnCollectionsOtherObject<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <span class="kw3">Exception</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Test test = <span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="st0">&quot;Konrad&quot;</span>, <span class="st0">&quot;Malawski&quot;</span>, <span class="nu0">21</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Test test2 = <span class="kw2">new</span> Test<span class="br0">&#40;</span><span class="st0">&quot;Konrad&quot;</span>, <span class="st0">&quot;Malawski&quot;</span>, <span class="nu0">21</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> sizeBefore<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> sizeAfter<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">Set</span> treeSet = <span class="kw2">new</span> HashSet<span class="sy0">&amp;</span>lt<span class="sy0">;&amp;</span>gt<span class="sy0">;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; treeSet.<span class="me1">add</span><span class="br0">&#40;</span>test<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; sizeBefore = treeSet.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; treeSet.<span class="me1">add</span><span class="br0">&#40;</span>test2<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; sizeAfter = treeSet.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertEquals<span class="br0">&#40;</span>sizeBefore, sizeAfter<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now that was nice wasn&#8217;t it? :-) <strong>Objects </strong>probably isn&#8217;t &#8220;the&#8221; killer feature that JDK7 has, but it&#8217;s a really nice addition, so let&#8217;s get to using it :-) The other utility methods in this class are:</p>
<ul>
<li><strong>static &lt;T&gt; int	compare(T a, T b, Comparator &lt; ? super T&gt; c)</strong><br />
Returns 0 if the arguments are identical and c.compare(a, b) otherwise.</li>
<li><strong>static boolean	deepEquals(Object a, Object b)</strong><br />
Returns true if the arguments are deeply equal to each other and false otherwise.</li>
<li><strong>static boolean	equals(Object a, Object b)</strong><br />
Returns true if the arguments are equal to each other and false otherwise.</li>
<li><strong>static int	hash(Object&#8230; values)</strong><br />
Generates a hash code for a sequence of input values.</li>
<li><strong>static int	hashCode(Object o)</strong><br />
Returns the hash code of a non-null argument and 0 for a null argument.</li>
<li><strong>static &lt;T&gt; T	requireNonNull(T obj)</strong><br />
Checks that the specified object reference is not null.</li>
<li><strong>static &lt;T&gt; T	requireNonNull(T obj, String message)</strong><br />
Checks that the specified object reference is not null and throws a customized NullPointerException if it is.</li>
<li><strong>static String	toString(Object o)</strong><br />
Returns the result of calling toString for a non-null argument and &#8220;null&#8221; for a null argument.</li>
<li><strong>static String	toString(Object o, String nullDefault)</strong><br />
Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.</li>
</ul>
<p>Hooray for small litle improvements :-) This class officialy get&#8217;s Duke&#8217;s thumbs up image:</p>
<p style="text-align: center;"><img class="aligncenter" title="Java-duke" src="http://www.blog.project13.pl/wp-content/uploads/2011/04/Java-duke.jpg" alt="" width="200" height="235" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/coding/1284/jdk7-java-lang-objects-forgotten-feature/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[terminal heroes] Display git branch in shell prompt (PS1)</title>
		<link>http://www.blog.project13.pl/index.php/fun/1198/terminal-heroes-display-git-branch-in-shell-prompt-ps1/</link>
		<comments>http://www.blog.project13.pl/index.php/fun/1198/terminal-heroes-display-git-branch-in-shell-prompt-ps1/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 22:29:01 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[terminal heroes]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[branch]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[prompt]]></category>
		<category><![CDATA[ps1]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=1198</guid>
		<description><![CDATA[I don&#8217;t think I&#8217;ve blogged about this but I&#8217;ve seen this sometime ago and now decided to add it to my .bashrc for good. It&#8217;s a simple trick to make an awesome PS1 shell prompt, displaying the branch you are currently on (if you&#8217;re in a git versioned directory). Not that I&#8217;m forgetting what branch [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t think I&#8217;ve blogged about this but I&#8217;ve seen this sometime ago and now decided to add it to my .bashrc for good. It&#8217;s a simple trick to make an awesome PS1 shell prompt, displaying the branch you are currently on (if you&#8217;re in a git versioned directory). Not that I&#8217;m forgetting what branch I&#8217;m on, but I hope it&#8217;ll make me create more branches more often and never &#8220;just work on master&#8221; :-)</p>
<p><a href="http://www.blog.project13.pl/wp-content/uploads/2011/02/awesome-git-branch-in-shell.png"><img src="http://www.blog.project13.pl/wp-content/uploads/2011/02/awesome-git-branch-in-shell.png" alt="" title="awesome-git-branch-in-shell" width="530" class="aligncenter size-full wp-image-1211" /></a></p>
<p>The code comes from <a href="http://arnorehn.de/cgi-bin/weblog_basic/index.php?p=34" onclick="urchinTracker('/outgoing/arnorehn.de/cgi-bin/weblog_basic/index.php?p=34&amp;referer=');">http://arnorehn.de/cgi-bin/weblog_basic/index.php?p=34</a> so big kudos too him :-)</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1"><span class="co0"># in your ~/.bashrc for example</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alias</span> <span class="re2">gitka=</span><span class="st0">&#39;gitk &#8211;all&#39;</span> <span class="co0">#unrelated to this post, but very useful :-)</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">function</span> formattedGitBranch <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re2">_branch=</span><span class="st0">&quot;$(git branch 2&gt;/dev/null | sed -e &quot;</span><span class="sy0">/</span>^\s<span class="sy0">/</span>d<span class="st0">&quot; -e &quot;</span>s<span class="sy0">/</span>^\<span class="sy0">*</span>\s<span class="sy0">//</span><span class="st0">&quot;)&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">test</span> -n <span class="st0">&quot;$_branch&quot;</span> <span class="sy0">&amp;&amp;</span> <span class="kw3">echo</span> -e <span class="st0">&quot; @<span class="es0">\e</span>[0;32m $_branch&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#export PS1=&quot;\u@\h \W \[\e[m\]\$(formattedGitBranch) \[\e[1;32m\]\$ \[\e[m\]\[\e[0m\]&quot; </span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">export</span> <span class="re2">PS1=</span><span class="st0">&quot;<span class="es0">\u</span>@<span class="es0">\h</span> <span class="es0">\W</span><span class="es0">\[</span><span class="es0">\e</span>[m<span class="es0">\]</span><span class="es0">\$</span>(formattedGitBranch)<span class="es0">\[</span><span class="es0">\e</span>[0m<span class="es0">\]</span> <span class="es0">\$</span> &quot;</span></div>
</li>
</ol>
</div>
<p>Cheers and <b>happy hacking</b>!</p>
<p>PS: I&#8217;m waiting for my T-shirt and stickers (for training participants) to arrive from github. I&#8217;d love they make it in time for <a href="http://www.sfi.org.pl" onclick="urchinTracker('/outgoing/www.sfi.org.pl?referer=');">SFI</a> but I&#8217;m not sure if it&#8217;s even possible hmmm&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/fun/1198/terminal-heroes-display-git-branch-in-shell-prompt-ps1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[release] maven-git-commit-id-plugin</title>
		<link>http://www.blog.project13.pl/index.php/fun/1174/release-maven-git-commit-id-plugin/</link>
		<comments>http://www.blog.project13.pl/index.php/fun/1174/release-maven-git-commit-id-plugin/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 15:25:18 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[freedom]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[#maven]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[free software]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=1174</guid>
		<description><![CDATA[Yup, today I&#8217;m releasing yet another piece of cool free software! It solves a problem we&#8217;ve had at our company, the tester tometimes tested stuff that wasn&#8217;t deployed yet, and the developers thought it was deployed and this caused some weird situations sometimes. Using the plugin I&#8217;ve written, we&#8217;re able to expose &#8220;which version is [...]]]></description>
			<content:encoded><![CDATA[<p>Yup, today I&#8217;m releasing yet another piece of cool free software! It solves a problem we&#8217;ve had at our company, the tester tometimes tested stuff that wasn&#8217;t deployed yet, and the developers thought it was deployed and this caused some weird situations sometimes. Using the plugin I&#8217;ve written, we&#8217;re able to expose &#8220;which version is this?&#8221; using repository information from<strong> git </strong>in our webapps. Yes there are maven plugins that do this, but none of them supported git &#8211; so I wrote my own and hope you guys will find it as useful as we do!</p>
<p><strong>UPDATE</strong><br />
The plugin is now available from <strong>Sonatype Nexus</strong>!</p>
<pre>
    <repository>
        <id>sonatype-releases</id>
        <name>Sonatype Releases</name>
        <url>https://oss.sonatype.org/content/repositories/releases/</url>
    </repository>
</pre>
<p><strong>END OF UPDATE</strong></p>
<p>Now I&#8217;ll be a bit lazy and just paste the README I&#8217;ve prepared for <strong><a href="https://github.com/ktoso/maven-git-commit-id-plugin/" target="_blank" onclick="urchinTracker('/outgoing/github.com/ktoso/maven-git-commit-id-plugin/?referer=');">maven-git-commit-id-plugin on github</a></strong>.</p>
<div id="readme">
<div>
<h2>Maven plugin: git-commit-id-plugin</h2>
<p><strong>git-commit-id-plugin</strong> is a plugin quite similar to <a href="https://fisheye.codehaus.org/browse/mojo/tags/buildnumber-maven-plugin-1.0-beta-4" target="_blank" onclick="urchinTracker('/outgoing/fisheye.codehaus.org/browse/mojo/tags/buildnumber-maven-plugin-1.0-beta-4?referer=');">build-number maven plugin</a> for example but as buildnumber only supports svn (which is very sad) and  cvs (which is even more sad, and makes bunnies cry) I had to quickly  develop an git version of such a plugin. For those who don&#8217;t know the  previous plugins, let me explain what this plugin does:</p>
<h3>Sample scenario why this plugin is useful</h3>
<p>If you develop your maven project inside an git repository (which you  hopefully already are docing) you may want to know exactly what changeset is currently deployed online. Why is this useful? Well,  the tester won&#8217;t come to you screaming &#8220;heeey that bug ain&#8217;t fixed&#8221; of  course you&#8217;d reply &#8220;but I fixed it this morning!&#8221; and after some  searching you notice &#8220;oh&#8230; it&#8217;ll be online after the next deployment,  sorry tester&#8230; :-(&#8220;.</p>
<p>This scenario keeps repeating sometimes, thus you can state which  commit fixes/closes the bug, note this in JIRA etc and then the tester  will know if it&#8217;s already online (by the commit date for example).</p>
<h2>Usage</h2>
<h3>Getting the plugin</h3>
<p>I&#8217;ll be trying to get this plugin out to sonatype for others to use if more simply, for now it&#8217;s quickest to just:</p>
<pre><code>  git clone git://github.com/ktoso/maven-git-commit-id-plugin.git maven-git-commit-id-plugin
  cd maven-git-commit-id-plugin
  mvn install
</code></pre>
<p>and you&#8217;re ready to use it ;-) I&#8217;m also thinking about making this  github repo a maven repository, which would make the above step not  needed &#8211; but first let&#8217;s wait if sonatype let me in with this project,  k? ;-)</p>
<h3>Using the plugin</h3>
<p>It&#8217;s really simple to setup this plugin, here&#8217;s a sample pom that you may base your <strong>pom.xml</strong> on:</p>
<pre><code>   &lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
       &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

       &lt;groupId&gt;pl.project13.maven&lt;/groupId&gt;
       &lt;artifactId&gt;my-git-plugin-sample-app&lt;/artifactId&gt;
       &lt;packaging&gt;war&lt;/packaging&gt;
       &lt;version&gt;0.1&lt;/version&gt;
       &lt;name&gt;my-git-plugin-sample-app&lt;/name&gt;
       &lt;url&gt;http://www.blog.project13.pl&lt;/url&gt;

       &lt;parent/&gt;

       &lt;dependencies /&gt;

       &lt;build&gt;
           &lt;!-- GIT COMMIT ID PLUGIN CONFIGURATION --&gt;
           &lt;resources&gt;
               &lt;resource&gt;
                   &lt;directory&gt;src/main/resources&lt;/directory&gt;
                   &lt;filtering&gt;true&lt;/filtering&gt;
                   &lt;includes&gt;
                       &lt;include&gt;**/*.properties&lt;/include&gt;
                       &lt;include&gt;**/*.xml&lt;/include&gt;
                   &lt;/includes&gt;
               &lt;/resource&gt;
           &lt;/resources&gt;

           &lt;plugins&gt;
               &lt;plugin&gt;
                   &lt;groupId&gt;pl.project13.maven&lt;/groupId&gt;
                   &lt;artifactId&gt;git-commit-id-plugin&lt;/artifactId&gt;
                   &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
                   &lt;executions&gt;
                       &lt;execution&gt;
                           &lt;goals&gt;
                               &lt;goal&gt;revision&lt;/goal&gt;
                           &lt;/goals&gt;
                       &lt;/execution&gt;
                   &lt;/executions&gt;
                   &lt;configuration&gt;
                       &lt;prefix&gt;git&lt;/prefix&gt; &lt;!-- that's the default value --&gt;
                       &lt;dateFormat&gt;dd.MM.yyyy '@' HH:mm:ss z&lt;/dateFormat&gt; &lt;!-- that's the default value --&gt;
                       &lt;verbose&gt;true&lt;/verbose&gt; &lt;!-- false is default for this --&gt;
                       &lt;dotGitDirectory&gt;${project.basedir}/../.git&lt;/dotGitDirectory&gt; &lt;!-- required, you have to specify this path --&gt;
                   &lt;/configuration&gt;
               &lt;/plugin&gt;
               &lt;!-- END OF GIT COMMIT ID PLUGIN CONFIGURATION --&gt;

               &lt;!-- other plugins --&gt;
           &lt;/plugins&gt;
       &lt;/build&gt;
   &lt;/project&gt;
</code></pre>
<p>Based on the above part of a working POM you should be able to figure out the rest, I mean you are a maven user after all&#8230; ;-) Note that the resources filtering is important for this plugin to work, don&#8217;t omit it!</p>
<p>Now you just have to include such a properties file in your project under <code>/src/main/resources</code> (and call it <strong>git.properties</strong> for example) and maven will put the appropriate properties in the placeholders:</p>
<pre><code> git.branch=${git.branch}

 git.build.user.name=${git.build.user.name}
 git.build.user.email=${git.build.user.email}
 git.build.time=${git.build.time}

 git.commit.id=${git.commit.id}
 git.commit.user.name=${git.commit.user.name}
 git.commit.user.email=${git.commit.user.email}
 git.commit.message.full=${git.commit.message.full}
 git.commit.message.short=${git.commit.message.short}
 git.commit.time=${git.commit.time}
</code></pre>
<p>The <code>git</code> prefix may be configured in the plugin declaration above.</p>
<h3>Maven resource filtering + Spring = GitRepositoryState Bean</h3>
<p>You&#8217;ll most probably want to wire these plugins somehow to get easy  access to them during runtime. We&#8217;ll use spring as an example of doing  this. Start out with with adding the above steps to your project, next paste  this <strong>git-bean.xml</strong> into the <code>/src/main/resources/</code> directory (or any other, just adjust the paths later on):</p>
<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;

    &lt;bean name="gitRepositoryInformation"&gt;
        &lt;property name="branch" value="${git.branch}"/&gt;
        &lt;property name="commitId" value="${git.commit.id}"/&gt;
        &lt;property name="commitTime" value="${git.commit.time}"/&gt;
        &lt;property name="buildUserName" value="${git.build.user.name}"/&gt;
        &lt;property name="buildUserEmail" value="${git.build.user.email}"/&gt;
        &lt;property name="commitMessageFull" value="${git.commit.message.full}"/&gt;
        &lt;property name="commitMessageShort" value="${git.commit.message.short}"/&gt;
        &lt;property name="commitUserName" value="${git.commit.user.name}"/&gt;
        &lt;property name="commitUserEmail" value="${git.commit.user.email}"/&gt;
    &lt;/bean&gt;

&lt;/beans&gt;
</code></pre>
<p>And here&#8217;s the source of the bean we&#8217;re binding here:</p>
<pre><code>package pl.project13.maven.example.git;

import org.codehaus.jackson.annotate.JsonWriteNullProperties;

/**
 * A spring controlled bean that will be injected
 * with properties about the repository state at build time.
 * This information is supplied by my plugin - &lt;b&gt;pl.project13.maven.git-commit-id-plugin&lt;/b&gt;
 *
 * @author Konrad Malawski
 */
@JsonWriteNullProperties(true)
public class GitRepositoryState {
  String branch;                  // =${git.branch}
  String commitId;                // =${git.commit.id}
  String buildUserName;           // =${git.build.user.name}
  String buildUserEmail;          // =${git.build.user.email}
  String buildTime;               // =${git.build.time}
  String commitUserName;          // =${git.commit.user.name}
  String commitUserEmail;         // =${git.commit.user.email}
  String commitMessageFull;       // =${git.commit.message.full}
  String commitMessageShort;      // =${git.commit.message.short}
  String commitTime;              // =${git.commit.time}

  public GitRepositoryState() {
  }

  /* Generate setters and getters here */
}
</code></pre>
<p>The source for it is also on the repo of this plugin. Of course, <em>feel free to drop out the jackson annotation</em> if you won&#8217;t be using it.</p>
<p>The last configuration related thing we need to do is to load up this bean in your appContext, so open up your <strong>applicationContext.xml</strong> or whatever you call it in your project and add these lines in the  section:</p>
<pre><code>&lt;context:property-placeholder location="classpath:*.properties" /&gt;
&lt;import resource="classpath:/git-bean.xml"/&gt;
</code></pre>
<p>Of course, you may adjust the paths and file locations as you please, no problems here&#8230; :-) <em>Now you&#8217;re ready to use your GitRepositoryState Bean!</em> Let&#8217;s create an sample <strong>Spring MVC Controller</strong> to test it out:</p>
<pre><code> @Controller
 @RequestMapping("/git")
 public class GitService extends BaseWebService {

     @Autowired
     GitRepositoryState gitRepoState;

     @RequestMapping("/status")
     public ModelAndView checkGitRevision() throws WebServiceAuthenticationException {
       ServerResponse&lt;GitRepositoryState&gt; response = new ServerResponse&lt;GitRepositoryState&gt;(gitRepoState);
       return createMAV(response);
     }
 }
</code></pre>
<p>Don&#8217;t mind the createMAV and responses stuff, it&#8217;s just example code.  And feel free to use constructor injection, it&#8217;s actually a better idea  ;-)</p>
<p>In the end <em>this is what this service would return</em>:</p>
<pre><code> {
     "branch" : "testing-maven-git-plugin",
     "commitTime" : "06.01.1970 @ 16:16:26 CET",
     "commitId" : "787e39f61f99110e74deed68ab9093088d64b969",
     "commitUserName" : "Konrad Malawski",
     "commitUserEmail" : "konrad.malawski@java.pl",
     "commitMessageFull" : "releasing my fun plugin :-)
                            + fixed some typos
                            + cleaned up directory structure
                            + added license etc",
     "commitMessageShort" : "releasing my fun plugin :-)",
     "buildTime" : "06.01.1970 @ 16:17:53 CET",
     "buildUserName" : "Konrad Malawski",
     "buildUserEmail" : "konrad.malawski@java.pl"
 }
</code></pre>
<p>That&#8217;s all folks! <strong>Happy hacking!</strong></p>
<h2>Configuration details</h2>
<p>Just a short recap of the available parameters&#8230;</p>
<p>Required parameters:</p>
<ul>
<li> <strong>dotGitDirectory</strong> &#8211; (required) the location of your .git  folder. Try to use ${project.basedir} as root for this, and navigate  using ../ to higher up folder to easily use this plugin in multi module  enviroments etc. An example would be: <code>${project.basedir}/../.git</code></li>
</ul>
<p>Optional parameters:</p>
<ul>
<li> <strong>prefix</strong> &#8211; (default: git) is the &#8220;namespace&#8221; for all exposed properties</li>
<li> <strong>dateFormat</strong> &#8211; (default: dd.MM.yyyy &#8216;@&#8217; HH:mm:ss z) is a  normal SimpleDateFormat String and will be used to represent  git.build.time and git.commit.time</li>
<li> <strong>verbose</strong> &#8211; (default: false) if true the plugin will print a summary of all collected properties when it&#8217;s done</li>
</ul>
<h2>License</h2>
<p>I&#8217;m releasing this plugin under the <strong>GNU Lesser General Public License 3.0</strong>. You&#8217;re free to use it as you wish, the license text is attached in the LICENSE file. You may contact me if you want this to be released on a different license, just send me an email konrad.malawski@java.pl :-)</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/fun/1174/release-maven-git-commit-id-plugin/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>[PL] Trzeźwe spojrzenie na: CoFoJa (Design By Contract form Google)</title>
		<link>http://www.blog.project13.pl/index.php/coding/1168/pl-trzezwe-spojrzenie-na-cofoja-design-by-contract-form-google/</link>
		<comments>http://www.blog.project13.pl/index.php/coding/1168/pl-trzezwe-spojrzenie-na-cofoja-design-by-contract-form-google/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 22:18:51 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[codnig]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[jee]]></category>
		<category><![CDATA[libs]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=1168</guid>
		<description><![CDATA[Design By Contract Dzisiejszy wpis będzie o znanej i generalnie dość znanej i mającej wiele zalet metodyce Design By Contract. Temat &#8220;wypłynął&#8221; ponownie dla wielu javovców tym razem dzięki wydaniu biblioteki  CoFoJa (o którym właśnie będzie ten post) przez dwóch pracowników Google przy ścisłej współpracy z autorem pierwowzoru tego projektu. Dlaczego jednak nietypowy tytuł posta [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Design By Contract</strong></p>
<p>Dzisiejszy wpis będzie o znanej i generalnie dość znanej i mającej wiele zalet metodyce <strong>Design By Contract</strong>. Temat &#8220;wypłynął&#8221; ponownie dla wielu javovców tym razem dzięki wydaniu biblioteki  <strong>CoFoJa</strong> (o którym właśnie będzie ten post) przez dwóch pracowników <strong>Google</strong> przy ścisłej współpracy z <a href="http://www.linkedin.com/pub/dir/Johannes/Rieken" onclick="urchinTracker('/outgoing/www.linkedin.com/pub/dir/Johannes/Rieken?referer=');">autorem pierwowzoru tego projektu</a>. Dlaczego jednak nietypowy tytuł posta &#8211; &#8220;trzeźwe spojrzenie&#8221;? Ponieważ w odróżnieniu od niektórych nie oceniam technologii na podstawie &#8220;wow factor&#8221; a realnej używalności. Gotowi mini review tego toola? No to lecimy&#8230; :-)</p>
<p><strong>Contracts For Java</strong></p>
<p>Na początek warto rzucić okiem na <a href="http://google-opensource.blogspot.com/2011/02/contracts-for-java.html" onclick="urchinTracker('/outgoing/google-opensource.blogspot.com/2011/02/contracts-for-java.html?referer=');">googlowy blog</a> gdzie ogłoszono premierę tego narzędzia oraz oczywiście samą <a href="http://code.google.com/p/cofoja/" onclick="urchinTracker('/outgoing/code.google.com/p/cofoja/?referer=');">stronę domową projektu </a><acronym title="Contracts For Java"><a href="http://code.google.com/p/cofoja/" onclick="urchinTracker('/outgoing/code.google.com/p/cofoja/?referer=');"><strong>CoFoJa</strong></a></acronym>. Oczywiście na samym przekierowaniu nie poprzestanę więc zaczniemy od mini definicji od strony użytkownika metodyki design by contract. Jak możemy przeczytać chociażby <strong>w pragmatycznym programiście </strong>metodyka ta ma dość spory potencjał i ciekawe możliwości &#8211; generalnie chodzi o określanie &#8220;kontraktów&#8221; na naszych metodach. Zakładanie kontraktów ma pewną wyższość nad pisanie testów jednostkowych ponieważ możemy kontrakt założyć na interfejsie a wszystkie implementujące go klasy będą z nim związane</p>
<p>Kontrakt może na przykład zapewnić:</p>
<ul>
<li>wykonanie danej metody tylko i wyłącznie wtedy gdy przekazane jej argumenty spełniają pewne kryteria (w kontekście CoFoJa byłoby to <strong>@Requires</strong>)</li>
<li>zagwarantowanie spełnienia pewnych warunków po opuszczeniu metody (w kontekście CoFoJa byłoby to <strong>@Ensures</strong> ewentualnie <strong>@ThrowEnsures</strong> jeśli chcemy mieć kontrakt na rzucenie wyjątku)</li>
<li>zagwarantowanie ogólnych warunków na konkretnej np. klasie (<strong>@Invariant</strong>)</li>
</ul>
<p>Ok, skoro znamy podstawowe building blocks (zaczerpnięte skądżeby inąd niż z <a href="http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29" onclick="urchinTracker('/outgoing/en.wikipedia.org/wiki/Eiffel_28programming_language_29?referer=');">Eiffla</a> oczywiście) zobaczmy jakieś przykładowe poadnotowane nimi API:</p>
<p><script src="https://gist.github.com/817547.js?file=gistfile1.java"></script> <strong>A teraz praktycznie</strong></p>
<p>Najlepiej jest chwilkę się w niego wgryźć, powinien być stosunkowo zrozumiały. Nie chcemy nigdy mieć ujemnej ilości bananów &#8211; spowodowałoby to złamanie kontraktu. Podobne przykładowe kontrakty wejściowe/wyjściowe widzimy na metodach oraz jeden mały tip: metoda <strong>old()</strong> jest dostarczana automagicznie i oznacza &#8220;starą wartość tego wywołania&#8221; czyli przed wykonaniem metody na którą zakładamy ten kontrakt. Poza tym mamy tutaj do czynienia w sumie z zwyczajnym kodem Java który niestety jest wewnątrz stringa&#8230; Świadomy programista zapewne już ma zapaloną lampkę alarmową, że to będzie nie refaktorowalne i bez sensu&#8230; Na pocieszenie dodam iż kod Java zawarty w tych stringach faktycznie jest &#8220;kompilowany&#8221;i jak coś popsujemy składniowo, kontrakty się nie skompilują i dostaniemy normalne informacje jakby to się nam nie kompilował normalny &#8220;nasz kod&#8221;. Co do wygody refaktoringu&#8230; Na szczęscie korzystam z porządnego IDE (<strong>IntelliJ IDEA</strong>) i takie &#8220;problemy&#8221; mnie nie dotyczą ;-). Wybrałem inject języka <strong>Groovy</strong> do tych stringów aby parser składniowy nie dziwił się pojawienia się metody old() &#8220;z nikąd&#8221;:<a href="http://xlab.pl/wp-content/uploads/2011/02/intellij.png" onclick="urchinTracker('/outgoing/xlab.pl/wp-content/uploads/2011/02/intellij.png?referer=');"><img class="aligncenter size-full wp-image-1752" title="intellij" src="http://xlab.pl/wp-content/uploads/2011/02/intellij.png" alt="" width="600" /></a> <strong>Problem</strong> z &#8220;nie podpowiadaniem metod&#8221; jak i &#8220;nie da się refaktorować&#8221; jak widać został od razu <strong>wyeliminowany</strong> :-) <em><strong>Hooray for JetBrains.</strong></em> Poszukajmy jednak kolejnego problemu z tym API&#8230; Tak jest, <strong>@ThrowEnsures </strong>jest trochę brzydki a nie koniecznie musi być cały w stringu. Jak działa obecnie? Przekazujemy mu listę stringów gdzie parami występują &#8220;wyjątek&#8221; + &#8220;warunek kiedy ma zostać rzucony&#8221;. Chętniej zobaczył bym to w formie podobnej do poniższej (tylko szybki szkic taki), a wy?</p>
<p><script src="https://gist.github.com/817695.js?file=gistfile1.java"></script></p>
<p>Ok czas na uruchomienie kodu z &#8220;włączonymi kontraktami&#8221;&#8230; I&#8230; niestety nie okazało się to takie trywialne na obecnym etapie projektu. Niestety trzeba sobie ręcznie poustawiać processowanie adnotacji (norma, tutaj się niewiele zmieni) jednak trochę problemów nastarcza mi obecnie widoczność klas/pól dla CoFoJa w intellij. Classpath (dla procesora) zdaje się być ustawiony poprawny oraz jego klasa jak i inne opcje też jednak nie dochodzi do poprawnego przeparsowania wszystkich reguł. No cóż, może jutro się uda &#8211; tymczasem odsyłam do <a href="http://fsteeg.com/2011/02/07/setting-up-contracts-for-java-in-eclipse/" onclick="urchinTracker('/outgoing/fsteeg.com/2011/02/07/setting-up-contracts-for-java-in-eclipse/?referer=');">poradnika dla użytkowników Eclipse</a> gdzie to członek społeczności sprawnie poradził sobie z tym problemem :-)</p>
<p><strong>Trzeźwe spojrzenie: &#8220;Czy obecnie warto?</strong>&#8221;</p>
<p>Pozostaje pytanie&#8230; Czy warto się zainteresować CoFoJa jak i samym DBC? Pytanie jest chyba równie ogólne jak &#8220;czy warto meta-programować?&#8221; także wprost odpowiedzi nie udzielę. Miejmy jednak na uwadze że jak na razie używanie CoFoJa nie jest zbyt wygodne a adnotacje można by jeszcze trochę poszlifować. Trzeba by jeszcze rzucić okiem jak stoimy z integracją tego z Maven &#8211; aby normalnie testując bądź klikając sobie po projekcie te kontrakty mogły się przydawać. Sama idea jest ciekawa i jakby integracja z narzędziami była wygodniejsza &#8211; a może od razu pluginy do IDE &#8211; można by się nad tym porządniej zastanawiać. <strong>Jak na razie projekcik odkładamy ten projekt spokojnie na półkę &#8220;worth watching&#8221; i liczymy na rychłe wydanie 2.0.</strong> :-)</p>
<p>PS: Jeżeli interesują Cię takie jak i inne ciekawe metodyki i sposoby tworzenia <strong>pięknego</strong>, czystego<strong> kodu, </strong>zapraszam serdecznie na co 2 tygodniowe spotkania <em><a href="http://sckrk.com" onclick="urchinTracker('/outgoing/sckrk.com?referer=');">SCKRK </a></em>gdzie obecnie dyskutujemy w formie &#8220;reading club&#8221; nad <strong>D</strong>omain <strong>D</strong>riven <strong>D</strong>esign :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/coding/1168/pl-trzezwe-spojrzenie-na-cofoja-design-by-contract-form-google/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>git: &#8220;befriend&#8221; an existing and a remote branch</title>
		<link>http://www.blog.project13.pl/index.php/null/1145/git-befriend-an-existing-and-a-remote-branch/</link>
		<comments>http://www.blog.project13.pl/index.php/null/1145/git-befriend-an-existing-and-a-remote-branch/#comments</comments>
		<pubDate>Mon, 20 Dec 2010 01:41:47 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[null]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[terminal heroes]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=1145</guid>
		<description><![CDATA[Ok, time for another quick post about git. By &#8220;befriend&#8221; in the title I mean &#8220;make them track the same changes&#8221; or something like &#8220;tell git that it&#8217;s the same branch&#8221;* (not the &#8220;same&#8221; but you can get the idea from that sentence). Bellow is an quick flow over why you might need such an [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, time for another quick post about git. By &#8220;befriend&#8221; in the title I mean &#8220;make them track the same changes&#8221; or something like &#8220;tell git that it&#8217;s the same branch&#8221;* (not the &#8220;same&#8221; but you can get the idea from that sentence). Bellow is an quick flow over why you might need such an operation:</p>
<pre>[ktoso@protos somerepo-v2]$ <strong>git remote \
                            add origin \
                            git@github.com:ktoso/somerepo.git</strong>
[ktoso@protos somerepo-v2]$ <strong>git pull</strong>
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 2), reused 9 (delta 2)
Unpacking objects: 100% (9/9), done.
From github.com:ktoso/somerepo
0b59dd3..87b0c22  master     -&gt; origin/master
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull &lt;repository&gt; &lt;refspec&gt;').

See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:
</pre>
<pre>[branch "master"]
remote = &lt;nickname&gt;
merge = &lt;remote-ref&gt;

[remote "&lt;nickname&gt;"]
url = &lt;url&gt;
fetch = &lt;refspec&gt;

See git-config(1) for details.</pre>
<p>Well, git has actually already told us what we might want to do. But can&#8217;t this be done easier? Of course~!</p>
<pre>[ktoso@protos somerepo-v2]$ <strong>git branch --set-upstream \
                                         master \
                                         origin/master</strong>
Branch <strong>master</strong> set up to track remote branch <strong>master from origin</strong>.
[ktoso@protos somerepo-v2]$ git pull
Updating 0b59dd3..87b0c22
Fast-forward
.......</pre>
<p>And now we can easily pull and push changes again, without the hassle of specifying the obvious each time we&#8217;d want to get/share some changes. :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/null/1145/git-befriend-an-existing-and-a-remote-branch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t use += loops on Strings for duke&#8217;s sake&#8230;!</title>
		<link>http://www.blog.project13.pl/index.php/coding/593/dont-use-loops-on-strings-for-christs-sake/</link>
		<comments>http://www.blog.project13.pl/index.php/coding/593/dont-use-loops-on-strings-for-christs-sake/#comments</comments>
		<pubDate>Tue, 04 May 2010 19:34:32 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[polish]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=593</guid>
		<description><![CDATA[It&#8217;s always kinda shocking to see that such easy tasks, and what I&#8217;ve always thought to be &#8220;common knowledge&#8220;, are not that common among some students&#8230; One such thing is always using the += on strings in Java&#8230; C&#8217;mon, everybody knows that + and += are horribly slow. Yeah, I know that when used outside [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s always kinda shocking to see that such easy tasks, and what I&#8217;ve always thought to be &#8220;<strong>common knowledge</strong>&#8220;, are not that common among some students&#8230; One such thing is always using the += on strings in Java&#8230; C&#8217;mon, everybody knows that + and += are horribly slow. Yeah, I know that when used <strong>outside of loops</strong>, the javac compiler is able to optimize it, but most of the time I see people using it inside loops, to print some collection etc. And then they come to me complaining about &#8220;how slow java is&#8221;. <em><strong>Oh the horror, the madness&#8230;</strong></em></p>
<p>Anyway, this post will illustrate, very painfully, how much slower += is&#8230; Let&#8217;s do some (idiotic, yet good enough for this case) tests. First, the version everybody who _knows_ java uses:</p>
<div class="geshi no java">
<div class="head">public static void main(String []params){</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; StringBuilder s = <span class="kw2">new</span> StringBuilder<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span><span class="sy0">;</span> i<span class="sy0">&amp;</span>lt<span class="sy0">;</span><span class="nu0">99999</span><span class="sy0">;</span> i++<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; s.<span class="me1">append</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Let&#8217;s run it&#8230;</p>
<pre>[ktoso@homunculus ~]$ time `java BuilderTest`

<strong>real    0m0.209s</strong>
user    0m0.137s
sys     0m0.039s</pre>
<p>Hmm&#8230; Quite all right I guess&#8230; And now back to the monstrous version some of my colleges keep writing:</p>
<div class="geshi no java">
<div class="head">public static void main(String[] params){</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">String</span> s = <span class="kw2">new</span> <span class="kw3">String</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span><span class="sy0">;</span> i<span class="sy0">&amp;</span>lt<span class="sy0">;</span><span class="nu0">99999</span><span class="sy0">;</span> i++<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; s += i<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>compile and run&#8230;</p>
<pre>[ktoso@homunculus ~]$ time `java StringTest`

<strong>real    2m14.694s</strong>
user    2m10.379s
sys     0m2.254s
</pre>
<p>I even managed to make me some coffee before it finished&#8230; Why is that? For any real Java programmer its obvious, but for some students it&#8217;s not&#8230; String is immutable. Every &#8220;string modification&#8221; is creating a new string, with all the fuss with object creation/allocation, it really takes some time. It&#8217;s a &#8220;feature&#8221; of the language, so how does it fight this kind of problem? Yup, the <strong>StringBuilder is mutable</strong>, thus he is really supreme if it comes down to modifying strings.</p>
<p>So, next time before you come to me panicking about java/speed or some other nonsense, please <strong>RTFM first</strong> ;-)</p>
<hr />
<h3>Update: Let&#8217;s look into the generated bytecode!</h3>
<p>Yeah, why not? Since we&#8217;re talking about an really important feature here, let&#8217;s get more into it&#8230; You can use <strong>javap</strong> to display the bytecode from an class file. I&#8217;ve checked both examples above with it and the main difference are, of course, the lines used for appending those strings&#8230; First the StringBuilder version:</p>
<div class="geshi no java">
<div class="head">0:   new     #2; //class java/lang/StringBuilder</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">3</span>: &nbsp; dup</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">4</span>: &nbsp; invokespecial &nbsp; #<span class="nu0">3</span><span class="sy0">;</span> <span class="co1">//Method java/lang/StringBuilder.&quot;&lt;init&gt;&quot;:()V</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">7</span>: &nbsp; astore_1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">8</span>: &nbsp; iconst_0</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">9</span>: &nbsp; istore_2</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">10</span>: &nbsp;iload_2</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">11</span>: &nbsp;ldc &nbsp; &nbsp; #<span class="nu0">4</span><span class="sy0">;</span> <span class="co1">//int 99999</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">13</span>: &nbsp;if_icmpge &nbsp; &nbsp; &nbsp; <span class="nu0">28</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">16</span>: &nbsp;aload_1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">17</span>: &nbsp;iload_2</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">18</span>: &nbsp;invokevirtual &nbsp; #<span class="nu0">5</span><span class="sy0">;</span> <span class="co1">//Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">21</span>: &nbsp;pop</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">22</span>: &nbsp;iinc &nbsp; &nbsp;<span class="nu0">2</span>, <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">25</span>: &nbsp;<span class="kw2">goto</span> &nbsp; &nbsp;<span class="nu0">10</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">28</span>: &nbsp;return<span class="sy0">&lt;</span>/init<span class="sy0">&gt;</span></div>
</li>
</ol>
</div>
<p>Nothing weird here, right? We expected StringBuilder to be used and simply the append method to be called, quite &#8220;normal&#8221;. By the way, it&#8217;s quite interesting to see how actually java does things inside :-) Ok, now for the String += version:</p>
<div class="geshi no java">
<div class="head">0:   new     #2; //class java/lang/String</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">3</span>: &nbsp; dup</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">4</span>: &nbsp; invokespecial &nbsp; #<span class="nu0">3</span><span class="sy0">;</span> <span class="co1">//Method java/lang/String.&quot;&lt;init&gt;&quot;:()V</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">7</span>: &nbsp; astore_1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">8</span>: &nbsp; iconst_0</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">9</span>: &nbsp; istore_2</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">10</span>: &nbsp;iload_2</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">11</span>: &nbsp;ldc &nbsp; &nbsp; #<span class="nu0">4</span><span class="sy0">;</span> <span class="co1">//int 99999</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">13</span>: &nbsp;if_icmpge &nbsp; &nbsp; &nbsp; <span class="nu0">41</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">16</span>: &nbsp;<span class="kw2">new</span> &nbsp; &nbsp; #<span class="nu0">5</span><span class="sy0">;</span> <span class="co1">//class java/lang/StringBuilder</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">19</span>: &nbsp;dup</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">20</span>: &nbsp;invokespecial &nbsp; #<span class="nu0">6</span><span class="sy0">;</span> <span class="co1">//Method java/lang/StringBuilder.&quot;&lt;/init&gt;&lt;init&gt;&quot;:()V</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">23</span>: &nbsp;aload_1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">24</span>: &nbsp;invokevirtual &nbsp; #<span class="nu0">7</span><span class="sy0">;</span> <span class="co1">//Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">27</span>: &nbsp;iload_2</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">28</span>: &nbsp;invokevirtual &nbsp; #<span class="nu0">8</span><span class="sy0">;</span> <span class="co1">//Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">31</span>: &nbsp;invokevirtual &nbsp; #<span class="nu0">9</span><span class="sy0">;</span> <span class="co1">//Method java/lang/StringBuilder.toString:()Ljava/lang/String;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">34</span>: &nbsp;astore_1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">35</span>: &nbsp;iinc &nbsp; &nbsp;<span class="nu0">2</span>, <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">38</span>: &nbsp;<span class="kw2">goto</span> &nbsp; &nbsp;<span class="nu0">10</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="nu0">41</span>: &nbsp;return<span class="sy0">&lt;</span>/init<span class="sy0">&gt;</span></div>
</li>
</ol>
</div>
<p>Oh! First thing you&#8217;d notice, it&#8217;s now 41 lines and not 28 as it was when we used StringBuilder directly&#8230; Let&#8217;s go on and examine the rest&#8230; What do we have here&#8230; StringBuilder?! That&#8217;s a little less expected, as I thought that the compiler wasn&#8217;t able to guess that we need a StringBuilder in such an situation. Well, it guessed right. Even though we use += StringBuilder&#8217;s append is being called&#8230; And then it&#8217;s being converted to a String&#8230; And each time in the loop there&#8217;s a new StringBuilder (line 16). </p>
<p><strong>To summarize: Even though the javac compiler does use the StringBuilder if it thinks that&#8217;s a good idea, id does it quite stupid. And in the end, we still end up with super slow code! Bottom line is, that you should &#8220;by hand&#8221; use the StringBuilder in your code. Note that even thought you might use + in your code, javac will still make this StringBuilder code :-) Sometimes better, sometimes worse &#8211; as shown in the above examples.</strong></p>
<p>Hope this post was interesting for even the more advanced users out there &#8211; that&#8217;s what for the second part was for :-) If you have any comments, feel free to post them &#8211; it really makes me happy to see people commenting my stuff ;-)</p>
<p>PS: Hmmm&#8230; I wonder how GStrings would do in such an dumb test&#8230; Guess I&#8217;ll have to check someday.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/coding/593/dont-use-loops-on-strings-for-christs-sake/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Terminal Heroes 8: Memory usage scripts</title>
		<link>http://www.blog.project13.pl/index.php/coding/442/memory-usage-scripts/</link>
		<comments>http://www.blog.project13.pl/index.php/coding/442/memory-usage-scripts/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 10:54:21 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[terminal heroes]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[gnu/linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=442</guid>
		<description><![CDATA[I have a low-memory VPS running online and thus always have to worry about what uses how much memory&#8230; Of course top and my favorite htop are very good tools to check this, but sometimes I just want to get a super simple report if I&#8217;m overusing memory or not yet. The bellow scripts (most [...]]]></description>
			<content:encoded><![CDATA[<p>I have a low-memory VPS running online and thus always have to worry about what uses how much memory&#8230; Of course top and my favorite <strong>htop</strong> are very good tools to check this, but sometimes I just want to get a super simple report if I&#8217;m overusing memory or not yet. The bellow scripts (most of them was found online or hacked up from multiple scripts to fit my needs) print simple yet very readable memory usage information.</p>
<h2>memused</h2>
<div class="geshi no bash">
<div class="head">#!/bin/bash</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#if [ $1 = &#39;&#39;]; then</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#    ps auxf | awk &#39;{sum=sum+$6}; END {print sum/1024}&#39;;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#else</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#    ps auxf | grep $1 | awk &#39;{sum=sum+$6}; END {print sum/1024}&#39;;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#fi</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re2">bean=</span>`<span class="kw2">cat</span> <span class="sy0">/</span>proc<span class="sy0">/</span>user_beancounters` <span class="re2">guar=</span>`<span class="kw3">echo</span> <span class="st0">&quot;$bean&quot;</span> <span class="sy0">|</span> <span class="kw2">grep</span> vmguar <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $4;}&#39;</span>` <span class="re2">burst=</span>`<span class="kw3">echo</span> <span class="st0">&quot;$bean&quot;</span> <span class="sy0">|</span> <span class="kw2">grep</span> privvm <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $5;}&#39;</span>` <span class="re2">priv=</span>`<span class="kw3">echo</span> <span class="st0">&quot;$bean&quot;</span> <span class="sy0">|</span> <span class="kw2">grep</span> privvm <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $2;}&#39;</span>`</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="re2">total=</span>guar<span class="sy0">/</span><span class="nu0">256</span> <span class="kw3">let</span> <span class="re2">used=</span>priv<span class="sy0">/</span><span class="nu0">256</span> <span class="kw3">let</span> <span class="re2">burst=</span>burst<span class="sy0">/</span><span class="nu0">256</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot;VPS memory usage:&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot;Used: $used MB&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot;Total: $total MB&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot;Burstable to: $burst MB&quot;</span></div>
</li>
</ol>
</div>
<p>Sample output:<br />
<code>VPS memory usage:<br />
Used: 296 MB<br />
Total: 256 MB<br />
Burstable to: 512 MB</code></p>
<h2>memfree</h2>
<div class="geshi no bash">
<div class="head">#!/bin/bash</div>
<ol>
<li class="li1">
<div class="de1"><span class="co0">#</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># Revised 02-Feb-2007: include kernel memory (kmemsize) in &#39;used&#39; calculation</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># and show percentages in output.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re2">BEAN=</span>`<span class="kw2">cat</span> <span class="sy0">/</span>proc<span class="sy0">/</span>user_beancounters`</div>
</li>
<li class="li1">
<div class="de1"><span class="re2">GUAR=</span>`<span class="kw3">echo</span> <span class="st0">&quot;$BEAN&quot;</span> <span class="sy0">|</span> <span class="kw2">grep</span> vmguar <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $4;}&#39;</span>`</div>
</li>
<li class="li1">
<div class="de1"><span class="re2">PRIV=</span>`<span class="kw3">echo</span> <span class="st0">&quot;$BEAN&quot;</span> <span class="sy0">|</span> <span class="kw2">grep</span> privvm <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $2;}&#39;</span>`</div>
</li>
<li class="li1">
<div class="de1"><span class="re2">KMEM=</span>`<span class="kw3">echo</span> <span class="st0">&quot;$BEAN&quot;</span> <span class="sy0">|</span> <span class="kw2">grep</span> kmem <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $3;}&#39;</span>`</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="re2">TOTL=</span><span class="re1">$GUAR</span><span class="sy0">/</span><span class="nu0">256</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="re2">KMMB=</span><span class="re1">$KMEM</span><span class="sy0">/</span><span class="nu0">1048576</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="re2">PVMB=</span><span class="re1">$PRIV</span><span class="sy0">/</span><span class="nu0">256</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="re2">USED=</span><span class="re1">$KMMB</span>+<span class="re1">$PVMB</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="re2">FREE=</span><span class="re1">$TOTL</span>-<span class="re1">$USED</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#91;</span> <span class="st0">&quot;$FREE&quot;</span> -gt <span class="st0">&quot;0&quot;</span> <span class="br0">&#93;</span>; <span class="kw1">then</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">let</span> <span class="re2">UPER=</span><span class="re1">$USED</span><span class="sy0">*</span><span class="nu0">100</span><span class="sy0">/</span><span class="re1">$TOTL</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">let</span> <span class="re2">FPER=</span><span class="nu0">100</span>-<span class="re1">$UPER</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">let</span> <span class="re2">UPER=</span><span class="st0">&quot;100&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">let</span> <span class="re2">FPER=</span><span class="st0">&quot;0&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">fi</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot;VPS Memory:&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot; Total: $TOTL mb Used: $USED mb (${UPER}%) Free: $FREE mb (${FPER}%)&quot;</span></div>
</li>
</ol>
</div>
<p>Sample output:<br />
<code>VPS Memory:<br />
Total: 256 mb Used: 302 mb (100%) Free: -46 mb (0%)</code></p>
<h2>memheld</h2>
<div class="geshi no bash">
<div class="head">#!/bin/bash</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">grep</span> oomguarpages <span class="sy0">/</span>proc<span class="sy0">/</span>user_beancounters <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{s=$2;t=$3;u=$4; {print &quot;VPS Memory Usage<span class="es0">\n</span>Current Held: &quot; s/256 &quot;MB<span class="es0">\n</span>Max Held: &quot; t/256 &quot;MB<span class="es0">\n</span>Barrier: &quot;u/256&quot;MB&quot; }}&#39;</span></div>
</li>
</ol>
</div>
<p>Sample output:<br />
<code>VPS Memory Usage<br />
Current Held: 160.832MB<br />
Max Held: 294.605MB<br />
Barrier: 256MB</code></p>
<h2>memps</h2>
<div class="geshi no bash">
<div class="head">#!/bin/bash</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot;ps axo pmem,pcpu,comm | sort&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">ps</span> axo pmem,pcpu,<span class="kw2">comm</span> <span class="sy0">|</span> <span class="kw2">sort</span></div>
</li>
</ol>
</div>
<p>Sample output:<br />
<code>ps axo pmem,pcpu,comm | sort<br />
0.0  0.0 script<br />
0.0  0.0 script<br />
0.1  0.0 init<br />
#...<br />
2.2  0.0 httpd<br />
4.0  0.0 mysqld<br />
7.3  0.0 httpd<br />
8.0  0.0 httpd<br />
%MEM %CPU COMMAND</code></p>
<h2>memprivmpages</h2>
<div class="geshi no bash">
<div class="head">#!/bin/bash</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re2">beans=</span>`<span class="kw2">cat</span> <span class="sy0">/</span>proc<span class="sy0">/</span>user_beancounters <span class="sy0">|</span> <span class="kw2">grep</span> priv`</div>
</li>
<li class="li1">
<div class="de1"><span class="re2">max=</span>`<span class="kw3">echo</span> <span class="re1">$beans</span> <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $4;}&#39;</span>`</div>
</li>
<li class="li1">
<div class="de1"><span class="re2">use=</span>`<span class="kw3">echo</span> <span class="re1">$beans</span> <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;{ print $2;}&#39;</span>`</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="st0">&quot;per=$use*100/$max&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="st0">&quot;mb=$use/256&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">let</span> <span class="st0">&quot;mmb=$max/256&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="st0">&quot;privvmpages usage: $mb MB ($per% of $mmb)&quot;</span></div>
</li>
</ol>
</div>
<p>Sample output:<br />
<code>privvmpages usage: 296 MB (57% of 512)</code><br />
For those wondering, yes I do have memory usage <em>above &#8220;100%&#8221; </em>in the output of some of these scripts, and it&#8217;s partially true. A VPS does usually have some guaranteed memory, and some &#8220;burstable&#8221; &#8211; so you can use more than just the guaranteed memory for some period of time. On the mashine I ran this I have 256 guaranteed and a &#8220;total&#8221; of 512MB &#8211; including the burstable memory.</p>
<p>If you like any of these, feel free to use them. I use them by creating a scripts directory and adding it to the PATH, so I can simply invoke them from anywhere.</p>
<p>PS: For more <strong>detailed memory usage information</strong> (as graphs etc), I&#8217;m using <a href="http://munin.projects.linpro.no/" onclick="urchinTracker('/outgoing/munin.projects.linpro.no/?referer=');"><strong>Munin</strong></a> and really recommend it. The graphs it draws are really nice, and it <strong>doesn&#8217;t</strong> inflict heavy load on the server, and it can <strong>monitor remote hosts</strong> if needed!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/coding/442/memory-usage-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>(Almost) Automatically convert files into PDF</title>
		<link>http://www.blog.project13.pl/index.php/fun/285/almost-automatically-convert-files-into-pdf/</link>
		<comments>http://www.blog.project13.pl/index.php/fun/285/almost-automatically-convert-files-into-pdf/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 00:56:55 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[freedom]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[terminal heroes]]></category>
		<category><![CDATA[cool]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[gnu/linux]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[wget]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=285</guid>
		<description><![CDATA[I&#8217;m learning Numerical Analysis right now and a friend of mine found some really nice PS files by dr Marian Bubek ( http://www.icsr.agh.edu.pl/~mownit/mownit.html ). I was really happy to see some *.ps but some people from my fellow students started complaining that they&#8217;d rather have pdf&#8217;s and not ghostscript files (who knows why they&#8217;d do..?). [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m learning Numerical Analysis right now and a friend of mine found some really nice PS files by dr Marian Bubek ( <a href="http://www.icsr.agh.edu.pl/~mownit/mownit.html" onclick="urchinTracker('/outgoing/www.icsr.agh.edu.pl/_mownit/mownit.html?referer=');">http://www.icsr.agh.edu.pl/~mownit/mownit.html</a> ). I was really happy to see some *.ps but some people from my fellow students started complaining that they&#8217;d rather have pdf&#8217;s and not ghostscript files <em>(who knows why they&#8217;d do..?)</em>. Anyways, they all had &#8220;manual&#8221; repetitious methods for converting the files &#8211; even uploading to somewebsite to have them converted over there &#8220;in the cloud&#8221;&#8230; I though of a slightly quicker and more efficient way, here it is, hope you&#8217;ll like it. Ah, there is One &#8220;non standard&#8221; requirement for this to work, you have to install <strong>cups-pdf</strong> :-)</p>
<div class="geshi no bash">
<div class="head">wget http://www.icsr.agh.edu.pl/~mownit/mownit.html -r &#8211;level 1 -A.ps &#8211;cut-dirs 4</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">mv</span> www.icsr.agh.edu.pl<span class="sy0">/*</span> .</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">rm</span> -rf www.icsr.agh.edu.pl<span class="sy0">/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="kw2">file</span> <span class="kw1">in</span> `<span class="kw2">dir</span> -f <span class="sy0">*</span>` ; <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="kw2">lpr</span> -P <span class="st0">&#39;cups-PDF&#39;</span> <span class="re1">$file</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="kw3">echo</span> <span class="st0">&quot;press enter when cups is done printing&#8230;&quot;</span> <span class="co0">#yeah, i got lazy here, one could watch lpq for changes etc.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="kw2">read</span> oczekiwanie;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="kw2">mv</span> <span class="sy0">/</span>home<span class="sy0">/</span>ktoso<span class="sy0">/</span>Pulpit<span class="sy0">/</span>cups-pdf<span class="sy0">/</span>template.pdf .<span class="sy0">/</span><span class="re1">$file</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">done</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># ________________ </span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0">#&lt; Happy hacking! &gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># &#8212;&#8212;&#8212;&#8212;&#8212;- </span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># &nbsp; &nbsp; &nbsp; &nbsp;\ &nbsp; ^__^</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># &nbsp; &nbsp; &nbsp; &nbsp; \ &nbsp;(oo)\_______</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(__)\ &nbsp; &nbsp; &nbsp; )\/\</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||&#8212;-w |</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co0"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| &nbsp; &nbsp; ||</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/fun/285/almost-automatically-convert-files-into-pdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing Blender problems on IntelGMAX4500</title>
		<link>http://www.blog.project13.pl/index.php/coding/269/fixing-blender-problems-on-intelgmax4500/</link>
		<comments>http://www.blog.project13.pl/index.php/coding/269/fixing-blender-problems-on-intelgmax4500/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 19:30:07 +0000</pubDate>
		<dc:creator>Ktoso</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[freedom]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[blender]]></category>
		<category><![CDATA[gnu/linux]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.blog.project13.pl/?p=269</guid>
		<description><![CDATA[I&#8217;ve recently bought a new laptop, a 15.6&#8221; HP Probook, that replaced my netbook Asus EEE 900 (needed some bigger hardware to work on &#8220;on the move&#8221;). One problem I encountered was blender not displaying correctly if I&#8217;d expand any dropdown menu etc. The fix is very simple yet powerfull, just set the following: LIBGL_ALWAYS_SOFTWARE=1 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently bought a new laptop, a 15.6&#8221; HP Probook, that replaced my netbook Asus EEE 900 (needed some bigger hardware to work on &#8220;on the move&#8221;). One problem I encountered was blender not displaying correctly if I&#8217;d expand any dropdown menu etc. The fix is very simple yet powerfull, just set the following:</p>
<p><code>LIBGL_ALWAYS_SOFTWARE=1 blender</code></p>
<p>This will launch blender and render it&#8217;s interface in software mode &#8211; runs perfectly, and no slowdowns could be detected by me so far :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.project13.pl/index.php/coding/269/fixing-blender-problems-on-intelgmax4500/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

