<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-665270312096612395</id><updated>2024-09-06T02:00:46.070+02:00</updated><category term="Java"/><category term="ZK"/><category term=".NET and VS2005"/><category term="C#"/><category term="Eclipse"/><category term="Linux"/><category term="SQL"/><category term="Java Swing"/><category term="Javascript"/><category term="Ant"/><category term="Internet"/><category term="Python"/><category term="git"/><title type='text'>XMedekO&#39;s blog</title><subtitle type='html'>Blog of Ondrej Medek, alias Ondra, aka XMedekO, or MeDon.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>46</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-7069640452495760876</id><published>2019-04-27T21:09:00.001+02:00</published><updated>2019-10-11T10:17:50.124+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><title type='text'>Running Docker on Windows 10 Home</title><content type='html'>If you try to install &lt;a href=&quot;https://docs.docker.com/docker-for-windows/&quot;&gt;Docker for Windows&lt;/a&gt; on Windows 10 Home edition, it would fail with a message that it requires Windows Professional. The reason is, that the current Docker requires &lt;a href=&quot;https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v&quot;&gt;Hyper-V enabled&lt;/a&gt;. But do not worry. If you have a CPU with Hyper-V (&lt;a href=&quot;https://www.thewindowsclub.com/check-intel-amd-processor-supports-hyper-v&quot;&gt;you can check it with msinfo32&lt;/a&gt;), there&#39;s a way, how install Docker on your machine. All credits goes to the Docker users &lt;a href=&quot;https://forums.docker.com/t/installing-docker-on-windows-10-home/11722/25&quot;&gt;in this forum post&lt;/a&gt;, especially to Marcel Hesselbarth and his blog &lt;a href=&quot;https://www.deskmodder.de/blog/2018/08/23/windows-10-home-hyper-v-aktivieren/&quot;&gt;Windows 10 Home Hyper-V aktivieren&lt;/a&gt; (in German).

&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;1.&lt;/b&gt; Make a file &lt;code&gt;hyperv.bat&lt;/code&gt; file and run is at Administrator:&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;@rem Install Hyper-V on Windows Home
pushd &quot;%~dp0&quot;
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum &amp;gt;hyper-v.txt
for /f %%i in (&#39;findstr /i . hyper-v.txt 2^&amp;gt;nul&#39;) do dism /online /norestart /add-package:&quot;%SystemRoot%\servicing\Packages\%%i&quot;
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL
pause
&lt;/pre&gt;
You need to reboot a computer after that.

&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;2.&lt;/b&gt; Change your registry: &lt;code&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion&lt;/code&gt; and change &lt;code&gt;EditionID&lt;/code&gt; from &lt;code&gt;Core&lt;/code&gt; to &lt;code&gt;Professional&lt;/code&gt;

&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;3.&lt;/b&gt; Install &lt;a href=&quot;https://docs.docker.com/docker-for-windows/&quot;&gt;Docker for Windows&lt;/a&gt;.

&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;4.&lt;/b&gt; Change your registry back.

&lt;br /&gt;
&lt;br /&gt;
And voilà, we have a Docker running on Windows 10.



</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/7069640452495760876/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/7069640452495760876' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/7069640452495760876'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/7069640452495760876'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2019/04/running-docker-on-windows-10-home.html' title='Running Docker on Windows 10 Home'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-1016592763887893531</id><published>2017-10-05T08:10:00.000+02:00</published><updated>2019-04-27T21:08:34.630+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Python logger by class</title><content type='html'>Common Python logging practice is to have one logger per module (e.s. &lt;a href=&quot;https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/&quot;&gt;see the Fang&#39;s blog&lt;/a&gt;):&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-py&quot;&gt;import logging
logger = logging.getLogger(__name__)
&lt;/pre&gt;
However, the general habit in other OOP programming languages (Java, C#, ...) is to have one (private static) logger per class:
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;public class MyObject {
    private final static Logger LOG = Logger.getLogger(MyObject.class.getName());
}
&lt;/pre&gt;
In Python, a logger per class may be created, too:
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-py&quot;&gt;import logging
class Foo(object):
    __log = logging.getLogger(__name__ + &#39;.Foo&#39;)

    def foo(self):
        self.__log.info(&#39;foo called&#39;)


class Bar(Foo):
    __log = logging.getLogger(__name__ + &#39;.Bar&#39;)

    def bar(self):
        self.__log.info(&#39;bar called&#39;)


bar = Bar()
bar.foo()
bar.bar()

&amp;gt;&amp;gt;&amp;gt; INFO:__main__.Foo:foo called
&amp;gt;&amp;gt;&amp;gt; INFO:__main__.Bar:bar called
&lt;/pre&gt;
&lt;br /&gt;
Note the double leading underscore &lt;code&gt;__log&lt;/code&gt; to exploit the &lt;a href=&quot;https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles&quot;&gt;Python name mangling&lt;/a&gt; to simulate a private attribute.

However, it is not very convenient to write the name of the class as a string. One way to avoid that is to init the logger after the class has been created:
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-py&quot;&gt;class Bar(Foo):
    def bar(self):
        self.__log.info(&#39;bar called&#39;)


Bar._Bar__log = logging.getLogger(__name__ + Bar.__name__) # manual name mangling
&lt;/pre&gt;
Still not very nice. We had to type the name of the class manually again. So let&#39;s exploit a decorator black magic to do it automatically:
&lt;pre class=&quot;prettyprint lang-py&quot;&gt;def addlogger(cls: type):
    aname = &#39;_{}__log&#39;.format(cls.__name__)
    setattr(cls, aname, logging.getLogger(cls.__module__ + &#39;.&#39; + cls.__name__))
    return cls


@addlogger
class Foo(object):
    def foo(self):
        self.__log.info(&#39;foo called&#39;)


@addlogger
class Bar(Foo):
    def bar(self):
        self.__log.info(&#39;bar called&#39;)


bar = Bar()
bar.foo()
bar.bar()

&amp;gt;&amp;gt;&amp;gt; INFO:__main__.Foo:foo called
&amp;gt;&amp;gt;&amp;gt; INFO:__main__.Bar:bar called
&lt;/pre&gt;
And voilà, we have a private static logger for each class with just one simple line or code.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/1016592763887893531/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/1016592763887893531' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/1016592763887893531'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/1016592763887893531'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2017/10/python-logger-by-class.html' title='Python logger by class'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-5813691357808868498</id><published>2016-10-19T09:50:00.001+02:00</published><updated>2016-10-19T09:52:38.319+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".NET and VS2005"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>.NET and Windows proxy problems</title><content type='html'>Windows has two HTTP APIs, which are unfortunatelly not 100% compatible in case of proxy settings:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;WinINet&lt;/i&gt;, used by Internet Explorer and most of the Windows C/C++ applications. Window proxy settings dialog sets the proxy for the WinINet&lt;/li&gt;
&lt;li&gt;&lt;i&gt;WinHTTP&lt;/i&gt; used by .NET.&lt;/li&gt;
&lt;/ul&gt;
So, when you develop a .NET application, is may happen that on a certain computer the Internet Explorer and other apps are connecting well, while your .NET app cannot connect to the Internet.&lt;br /&gt;
Since some proxy settings are hidden to the user by the &quot;Automatically detect settings&quot;checkbox, I have made a simple program &lt;a href=&quot;https://github.com/xmedeko/WinProxyViewer&quot;&gt;WinProxyViewer&lt;/a&gt;, which may be run with a inexperienced user and the result sent to the application authors.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Default Credentials&lt;/h3&gt;
&lt;br /&gt;
.NET does not pick up the proxy credentials entered in the Windows proxy settings, e.g. see this &lt;a href=&quot;http://stackoverflow.com/questions/299940/&quot;&gt;StackOverflow thread&lt;/a&gt;. One solution is to add to the application config:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-xml&quot;&gt;&amp;lt;system .net=&quot;&quot;&amp;gt;
  &amp;lt;defaultproxy usedefaultcredentials=&quot;true&quot; /&amp;gt;
&amp;lt;/system&amp;gt;
&lt;/pre&gt;
But I preffer the solution in the code (at least for the desktop apps):
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cs&quot;&gt;try
{
    System.Net.IWebProxy proxy = System.Net.WebRequest.DefaultWebProxy;
    PropertyInfo propInfo = proxy.GetType().GetProperty(&quot;WebProxy&quot;, BindingFlags.NonPublic | BindingFlags.Instance);
    ((System.Net.WebProxy)propInfo.GetValue(proxy)).UseDefaultCredentials = true;
}
catch (Exception ex)
{
    // log the exception
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
More proxies&lt;/h3&gt;
&lt;br /&gt;
The automatic proxy script for the &lt;i&gt;WinINet&lt;/i&gt; may contain more proxy servers. The &lt;i&gt;WinINet&lt;/i&gt; chooses next cycle the list until it finds the first proxy working. While the .NET (&lt;i&gt;WinHTTP&lt;/i&gt;) use the &lt;b&gt;first proxy from the list always&lt;/b&gt;, event when it does not work. So the .NET app may be disconnected, while the Internet Explorer is working. There is not known remedy for that. Fortunately, such situations are very rare.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
NTLM proxy&lt;/h3&gt;
&lt;br /&gt;
.NET (&lt;i&gt;WinHTTP&lt;/i&gt;) cannot automatically detect NTLM proxy and use Window credentials for it like the &lt;i&gt;WinINet&lt;/i&gt;. There is not known remedy for that. NTLM proxies are sometimes used in the enterpise environments.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Summary&lt;/h3&gt;
&lt;br /&gt;
.NET app cannot be 100% compatible with all the Windows proxy settings. A possible solution for that is to have own proxy settings in the app. But the app may be used by people who cannot (security or have no knowledge) to set the proxy settings in the .NET app. So they may be situations, when .NET stays disconnected.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/5813691357808868498/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/5813691357808868498' title='Komentáře: 2'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/5813691357808868498'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/5813691357808868498'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2016/10/net-and-windows-proxy-problems.html' title='.NET and Windows proxy problems'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-2915269939039644666</id><published>2016-05-19T21:56:00.001+02:00</published><updated>2016-05-27T10:59:51.568+02:00</updated><title type='text'>Atlassian Bamboo and xUnit testing</title><content type='html'>I needed to run &lt;a href=&quot;https://xunit.github.io/&quot;&gt;xUnit&lt;/a&gt; test on the Atlassian Bamboo Cloud (OnDemand) build server. It has no runner for xUnit, but can be run from the command line by &lt;a href=&quot;https://xunit.github.io/docs/getting-started-desktop#run-tests&quot;&gt;xunit.runner.console&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Configure your build server&lt;/h3&gt;
&lt;br /&gt;
On the build server, just NuGet the runner:&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;nuget.exe xunit.runner.console&lt;/pre&gt;
The Bamboo cannot parse xUnit output. It can handle the nUnit output nd the xUnit has an option to print results in the nUnit format. So, create a simple &lt;code&gt;xunit.cmd&lt;/code&gt; script:
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;@echo off
@rem xunit runner for Bamboo

if &quot;%2&quot;==&quot;&quot; (
  echo Usage: test.dll report.xml
  exit /b 1
)

if not exist %~dp2 mkdir %~dp2

&quot;c:\path\to\xunit.runner.console.2.1.0\tools\xunit.console.exe&quot; %1 -nunit %2
&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
Configure Bamboo&lt;/h3&gt;
&lt;br /&gt;
Then go to your &lt;i&gt;Bamboo Administration - Agents Summary&lt;/i&gt; and choose your agent. Add a new capability:
&lt;br /&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Capability type&lt;/td&gt;&lt;td&gt;&lt;b&gt;Executable&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Executable label&lt;/td&gt;&lt;td&gt;&lt;b&gt;xUnit&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Path&lt;/td&gt;&lt;td&gt;&lt;code&gt;c:\path\to\xunit.cmd&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
To add the xUnit to you plan&#39;s job, just add the Command task after the project build (e.g. MSBuild, or NAnt). I expect you have a module MyProjectTests in your solution. Then configure the xUnit task: 
&lt;br /&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Executable&lt;/td&gt;&lt;td&gt;&lt;b&gt;xUnit&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Argument&lt;/td&gt;&lt;td&gt;&lt;code&gt;bin\Release\MyProjectTests.dll test-reports\MyProjectTests.xml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Working sub directory&lt;/td&gt;&lt;td&gt;&lt;code&gt;MyProjectTests&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The final trick is to add the NUnit Parser task, just take care to put is as the &lt;b&gt;final task&lt;/b&gt;:
&lt;br /&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;NUnit Test Results File/Directory&lt;/td&gt;&lt;td&gt;&lt;code&gt;MyProjectTests/test-reports/*.xml&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The bamboo logic is, that:
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Any task fails, and no test result is found, it assumes the compilation error has occurred.
&lt;/li&gt;
&lt;li&gt;Any task fails, and a test result is found, it assumes a test has failed.
&lt;/li&gt;
&lt;li&gt;All tasks pass, the build is OK. If the test results are found, they are picked up. 
&lt;/li&gt;
&lt;/ol&gt;
</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/2915269939039644666/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/2915269939039644666' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/2915269939039644666'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/2915269939039644666'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2016/05/atlassian-bamboo-and-xunit-testing.html' title='Atlassian Bamboo and xUnit testing'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-9014407149237475401</id><published>2016-03-18T17:39:00.000+01:00</published><updated>2016-03-18T17:40:50.792+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>C# Caliburn.Micro Check Unmatched Elements</title><content type='html'>&lt;a href=&quot;http://caliburnmicro.com/&quot;&gt;Caliburn.Micro&lt;/a&gt; is a great C# MVVM framework. It saves the developer&#39;s fingers with the binding by the &lt;code&gt;x:Name&lt;/code&gt; convention:
&lt;pre class=&quot;prettyprint lang-xml&quot;&gt;&amp;lt;textbox x:name=&quot;Username&quot; /&amp;gt;&lt;/pre&gt;

However, when one mistypes the &lt;code&gt;x:Name&lt;/code&gt; or rename the property in the ViewModel then the Caliburn silently does not apply the binding. Such mistakes are hard to track. Fortunately Caliburn comes with the help of &lt;code&gt;ViewModelBinder.HandleUnmatchedElements&lt;/code&gt;. We have a simple rule in the project: if the &lt;code&gt;x:Name&lt;/code&gt; starts with the capital letter, then it has to be binded by the Caliburn. Then I have been able to plug in a simple method to check for unmathched elements starting with the capital:

&lt;pre class=&quot;prettyprint lang-csharp&quot;&gt;ViewModelBinder.HandleUnmatchedElements = (elems, type) =&amp;gt;
{
    if (!elems.Any())
        return;

    // elems with Name with the first letter upper case
    elems = elems.Where(e =&amp;gt; !string.IsNullOrEmpty(e.Name) &amp;&amp; e.Name.StartsWith(e.Name.Substring(0, 1).ToUpperInvariant()));
    if (!elems.Any())
        return;

    throw new InvalidOperationException(type + &amp;quot; contains Caliburn unmatched elements &amp;quot; + string.Join(&amp;quot;,&amp;quot;, elems.Select(e =&amp;gt; e.Name)));
};
&lt;/pre&gt;

 
</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/9014407149237475401/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/9014407149237475401' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/9014407149237475401'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/9014407149237475401'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2016/03/c-caliburnmicro-check-unmatched-elements.html' title='C# Caliburn.Micro Check Unmatched Elements'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-9065819431002178746</id><published>2015-06-08T17:49:00.001+02:00</published><updated>2016-05-20T08:33:48.575+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>C# Safe Event Pattern</title><content type='html'>C# language has a very good support for event programming. However, is does not address a two common issues with event handlers:
&lt;ol&gt;
&lt;li&gt;An event handler is registered more times, but we want to call it just once.&lt;/li&gt;
&lt;li&gt;An event handler is registered from longer living object then the event source. Then the event handler must be unregistered. Otherwise it may cause memory leaks.&lt;/li&gt;
&lt;/ol&gt;
First issue is usually simple to solve. The memory leaks are best avoided when using weak references. There are several solutions to .NET weak event references. e.g. see &lt;a href=&quot;http://www.codeproject.com/Articles/738109/The-NET-weak-event-pattern-in-Csharp&quot;&gt;The .NET weak event pattern in C#&lt;/a&gt;. I have decided to go with .NET 4.5 &lt;code&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.windows.weakeventmanager%28v=vs.110%29.aspx&quot;&gt;WeakEventManager&lt;/a&gt;&lt;/code&gt;. The following pattern makes it easy for &lt;code&gt;MyEvent&lt;/code&gt; consumer to write the code without worrying about event handler unregistering. 


&lt;pre class=&quot;prettyprint lang-cs&quot;&gt;
private event MyEventArgs _myEvent;

public event EventHandler&amp;lt;MyEventArgs&amp;gt; MyEvent
{
    add
    {
        // first unregister if registered
        MyEvent-= value;
        // register
        WeakEventManager&amp;lt;ThisType, MyEventArgs&amp;gt;.AddHandler(_myEvent, &quot;MyEvent&quot;, value);
    }
    remove
    {
        WeakEventManager&amp;lt;ThisType, MyEventArgs&amp;gt;.RemoveHandler(_myEvent, &quot;MyEvent&quot;, value);
    }
}
&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/9065819431002178746/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/9065819431002178746' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/9065819431002178746'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/9065819431002178746'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2015/06/c-safe-event-pattern.html' title='C# Safe Event Pattern'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-7515117865165909742</id><published>2015-05-19T08:03:00.001+02:00</published><updated>2015-05-19T08:03:30.837+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>Log4net Configuration for Desktop Application</title><content type='html'>I have configured &lt;a href=&quot;https://logging.apache.org/log4net/&quot;&gt;log4net&lt;/a&gt; for out current project, a plain Windows Desktop application. I wanted log to rolling file into the &lt;code&gt;AppData\Local&lt;/code&gt; folder. Not the Roaming folder, since log are specific to the machine and it is waste of resources to let them roam around. Also I wanted to see logs in console, but in DEBUG mode only, not in production.

First problem to find the &lt;code&gt;AppData\Local\MyAppName&lt;/code&gt; has been solved by &lt;code&gt;log4net.Util.PatternString&lt;/code&gt; class. It parses a special &lt;code&gt;%envFolderPath&lt;/code&gt; and &lt;code&gt;%appdomain&lt;/code&gt; patterns.

The second problem, to configure &lt;code&gt;ConsoleAppender&lt;/code&gt; just in DEBUG mode has several solutions. It is possible to load different configs, or to configure &lt;code&gt;ConsoleAppender&lt;/code&gt; programatically. I have decided to create own &lt;code&gt;DebugFilter&lt;/code&gt;, which denies all log in production mode. This way I have just one log4net config file. The cons is that it is very slightly less efficient to have configured an Appender which denies all log, but it&#39;s OK for most of applications.

&lt;pre class=&quot;prettyprint lang-xml&quot;&gt;
&amp;lt;log4net&amp;gt;
  &amp;lt;appender name=&amp;quot;RollingLogFileAppender&amp;quot; type=&amp;quot;log4net.Appender.RollingFileAppender&amp;quot;&amp;gt;
    &amp;lt;file type=&amp;quot;log4net.Util.PatternString&amp;quot; value=&amp;quot;%envFolderPath{LocalApplicationData}\%appdomain\log.txt&amp;quot; /&amp;gt;
    &amp;lt;appendToFile value=&amp;quot;true&amp;quot; /&amp;gt;
    &amp;lt;maxSizeRollBackups value=&amp;quot;2&amp;quot; /&amp;gt;
    &amp;lt;maximumFileSize value=&amp;quot;10MB&amp;quot; /&amp;gt;
    &amp;lt;rollingStyle value=&amp;quot;Size&amp;quot; /&amp;gt;
    &amp;lt;staticLogFileName value=&amp;quot;true&amp;quot; /&amp;gt;
    &amp;lt;layout type=&amp;quot;log4net.Layout.PatternLayout&amp;quot;&amp;gt;
      &amp;lt;conversionPattern value=&amp;quot;%date [%thread] %-5level %logger - %message%newline&amp;quot; /&amp;gt;
    &amp;lt;/layout&amp;gt;
  &amp;lt;/appender&amp;gt;
  &amp;lt;appender name=&amp;quot;ConsoleAppender&amp;quot; type=&amp;quot;log4net.Appender.ConsoleAppender&amp;quot;&amp;gt;
    &amp;lt;layout type=&amp;quot;log4net.Layout.PatternLayout&amp;quot;&amp;gt;
      &amp;lt;conversionPattern value=&amp;quot;%date [%thread] %-5level %logger - %message%newline&amp;quot; /&amp;gt;
    &amp;lt;/layout&amp;gt;
    &amp;lt;filter type=&amp;quot;cvtBackend.Logging.DebugFilter&amp;quot; /&amp;gt;
  &amp;lt;/appender&amp;gt;
  &amp;lt;!-- Setup the root category, add the appenders and set the default level --&amp;gt;
  &amp;lt;root&amp;gt;
    &amp;lt;level value=&amp;quot;INFO&amp;quot; /&amp;gt;
    &amp;lt;appender-ref ref=&amp;quot;RollingLogFileAppender&amp;quot; /&amp;gt;
    &amp;lt;appender-ref ref=&amp;quot;ConsoleAppender&amp;quot; /&amp;gt;
  &amp;lt;/root&amp;gt;
&amp;lt;/log4net&amp;gt;
&lt;/pre&gt;

And the &lt;code&gt;DebugFilter&lt;/code&gt;:

&lt;pre class=&quot;prettyprint lang-csharp&quot;&gt;
    /// &lt;summary&gt;
    /// log4net filter which allows logging in DEBUG mode only.
    /// &lt;/summary&gt;
    public class DebugFilter : FilterSkeleton
    {
        public override FilterDecision Decide(LoggingEvent loggingEvent)
        {
#if DEBUG
            return FilterDecision.Neutral;
#else
            return FilterDecision.Deny;
#endif
        }
    }
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/7515117865165909742/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/7515117865165909742' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/7515117865165909742'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/7515117865165909742'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2015/05/log4net-configuration-for-desktop.html' title='Log4net Configuration for Desktop Application'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-9100954116594325076</id><published>2015-02-13T11:53:00.000+01:00</published><updated>2015-06-08T17:49:45.002+02:00</updated><title type='text'>Java: Fast Date Formatting in Web Containers</title><content type='html'>Java &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html&quot;&gt;&lt;code&gt;SimpleDateFormat&lt;/code&gt;&lt;/a&gt; is not thread safe. When it is used in multithreaded applications, like Java web containers, it&#39;s forbidden use it as a static constant
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;public static final DateFormat DATE_FORMAT = new SimpleDateFormat(&#39;YYYY.MM.dd&#39;); // DON&#39;T!
&lt;/pre&gt;
Most developers properly constructs new &lt;code&gt;SimpleDateFormat&lt;/code&gt; everytime they need&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;DateFormat dateFormat = new SimpleDateFormat(&#39;YYYY.MM.dd&#39;);
String str = dateFormat.format(date);
&lt;/pre&gt;
However, constructing new SimpleDateFormat is not so cheap operation. So, how to safe CPU and memory like in static final pattern in a multithreaded environment?&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Solution 1: clone&lt;/h3&gt;
&lt;br /&gt;
Fortunatelly, &lt;code&gt;SimpleDateFormat&lt;/code&gt; implements &lt;code&gt;clone()&lt;/code&gt; method, which is fasters than constructing a new one form a &lt;code&gt;String&lt;/code&gt;:
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;private static final DateFormat DATE_FORMAT_TEMPLATE = new SimpleDateFormat(&#39;YYYY.MM.dd&#39;); // Do not use directly

/**
 * @return every time a new SimpleDateFormat.
 */
public static DateFormat getDateFormat() {
 return DATE_FORMAT_TEMPLATE.clone();
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;Solution 2: ThreadLocal&lt;/h3&gt;
&lt;br /&gt;
A general solution for sharing non-thread safe objects is to have one copy for each thread. Java has a very nice &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html&quot;&gt;&lt;code&gt;ThreadLocal&lt;/code&gt;&lt;/a&gt;. Together with lazy initialization we got the proper thread safe and fast solution:
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;private static final ThreadLocal DATE_FORMAT_TL = new ThreadLocal&lt;DateFormat&gt;() {
 protected DateFormat initialValue() {
  return new SimpleDateFormat(&#39;YYYY.MM.dd&#39;);
 };
};

/**
 * @return SimpleDateFormat, new one for every thread.
 */
public static DateFormat getDateFormat() {
 return DATE_FORMAT_TL .get();
}

/**
 * Simple, optimized Date formatting.
 */
public static String format(Date d) {
 return format == null ? null : getDateFormat().format(d);
}
&lt;/pre&gt;

This solution has advantage over &lt;code&gt;clone()&lt;/code&gt; method, that calling &lt;code&gt;getDateFormat()&lt;/code&gt; multiple times in one thread (e.g. inside a loop) does not create a new object. I like to add &lt;code&gt;format()&lt;/code&gt; to simplify usage and handle null values.

Note: ThreadLocal may cause redeploy memory leaks when it&#39;s value from a WAR class. E.g. see &lt;a href=&quot;http://xmedeko.blogspot.cz/2010/02/java-web-container-hunting-redeploy.html&quot;&gt;Java Web Container: Hunting Redeploy Memory Leaks&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/9100954116594325076/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/9100954116594325076' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/9100954116594325076'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/9100954116594325076'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2015/02/java-fast-date-formatting-in-web.html' title='Java: Fast Date Formatting in Web Containers'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-5779526045762346173</id><published>2014-12-12T12:31:00.001+01:00</published><updated>2015-06-08T17:49:57.756+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="git"/><title type='text'>Git error: Short SHA1 is Ambiguous</title><content type='html'>Git allows using shortened SHA1 hashes. Default is to use 7 characters (see &lt;code&gt;core.abbrev&lt;/code&gt; config option). But sooner or later, you may hit problem, that short hashes are ambiguous. E.g.
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-bash&quot;&gt;$ git log 0ed98e5
...
error: short SHA1 0ed98e5 is ambiguous
&lt;/pre&gt;
&lt;br /&gt;
To find, what Git objects are referenced by the same short has, use
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-bash&quot;&gt;$git rev-parse --disambiguate=0ed98e | git cat-file --batch-check
0ed98e56dd43b172d438dba0aa6ea9ebed0554c7 commit 307
0ed98e50771521f9ec27314d49286fd54e989c87 blob 1478
0ed98e50771521f9ec27314d49286fd54e989c87 blob 1478
&lt;/pre&gt;
&lt;br /&gt;
In this case, only the first hash (0ed98e56) refers to a commit. The second hash (0ed98e50) refers to a blob - file. So, Git does check SHA1 for all it&#39;s objects, not only for commits, although the &lt;code&gt;git log&lt;/code&gt; command uses just commits SHA1 for it&#39;s arguments.
&lt;br /&gt;
&lt;br /&gt;
How to fix the error? Use longer prefix of the SHA1 hash. The bulletproof solution is to use all 40 characters. But if you still like abbreviates, then you can find the safe minimum length of SHA1 prefix for your project by &lt;a href=&quot;http://blog.cuviper.com/2013/11/10/how-short-can-git-abbreviate/&quot;&gt;Josh Stone&#39;s solution&lt;/a&gt;:
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-bash&quot;&gt;$git rev-list --all --abbrev=0 --abbrev-commit | wc -L
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/5779526045762346173/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/5779526045762346173' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/5779526045762346173'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/5779526045762346173'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2014/12/git-error-short-sha1-is-ambiguous.html' title='Git error: Short SHA1 is Ambiguous'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-6765236916913600199</id><published>2014-05-15T09:37:00.002+02:00</published><updated>2014-05-15T09:48:18.875+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Javascript"/><title type='text'>Simple console.log wrapper hangs IE10 and Chrome</title><content type='html'>Many programmers write a wrapper around &lt;code&gt;console.log&lt;/code&gt; to use logging only when it&#39;s available. I have reassigned log to my own variable &lt;code&gt;this.log = console.log;&lt;/code&gt; and it has worked fine in Firefox with Firebug plugin. However, it has caused Javascript to stop working in Internet Explorer 10 (IE10) and Chrome. Strange thing is, IE10 hangs only when it has &lt;b&gt;closed&lt;/b&gt; the developer tools F12. When I have opened the developer tools to see what&#39;s going on, the IE10 has proceeded well.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-javascript&quot;&gt;var mylog = {
    log : this._logNull,
    init: function() {
        if (typeof console == &#39;object&#39; &amp;amp;&amp;amp; typeof console.log == &#39;function&#39;) {
            console.log(this);
            this.log = console.log; // IE10 hangs bug
            //this.log = this._log; // IE10 ok
        }
    },
    _logNull : function() {},
    _log : function() { console.log.apply(console, arguments); }
    
};
&lt;/pre&gt;
Try &lt;a href=&quot;http://jsfiddle.net/xmedeko/Z9t4p/&quot;&gt;the code at JSFiddle&lt;/a&gt;.
&lt;br /&gt;
&lt;br /&gt;
After writing a proper wrapper function _log, the code goes well even in IE10 with closed developer tools.&lt;br /&gt;
&lt;br /&gt;
Note, this is just proof of concept code for IE10. The real console.log wrapper may become more complicated when considering various browser like Chrome, Safari, etc.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/6765236916913600199/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/6765236916913600199' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/6765236916913600199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/6765236916913600199'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2014/05/ie10-hangs-on-consolelog.html' title='Simple console.log wrapper hangs IE10 and Chrome'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-4437095762059049341</id><published>2013-10-21T16:01:00.003+02:00</published><updated>2013-10-21T16:07:24.834+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Swing"/><title type='text'>Java Swing: Change Global Action Key</title><content type='html'>Java Swing Look and Feel defines it&#39;s own actions and register them globally on key strokes. It may happen, such a global keystroke is the same you need to use for own action. E.g. you define an action hooked on F8 key in the JPanel. But when the user has focused a JTable inside the JPanel and hit F8, then it still triggers the &quot;focusHeader&quot; action instead your action. One solution is to put your action on every component in the JPanel. Or you can change default keys globally:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;private static void reregisterGlobalKeys() {
 // F8 to Ctrl+F8
 reregisterGlobalKey(&quot;Table.ancestorInputMap&quot;, KeyStroke.getKeyStroke(KeyEvent.VK_F8), KeyStroke.getKeyStroke(KeyEvent.VK_F8, KeyEvent.CTRL_DOWN_MASK));
 reregisterGlobalKey(&quot;SplitPane.ancestorInputMap&quot;, KeyStroke.getKeyStroke(KeyEvent.VK_F8), KeyStroke.getKeyStroke(KeyEvent.VK_F8, KeyEvent.CTRL_DOWN_MASK));
}

private static void reregisterGlobalKey(String uiManagerKey, KeyStroke oldKeyStroke, KeyStroke newKeyStroke) {
 Object inputMapObj = UIManager.get(uiManagerKey);
 if (inputMapObj == null || !(inputMapObj instanceof InputMap)) {
  System.err.println(&quot;reregisterGlobalKey inputMap not found: &quot; + uiManagerKey);
  return;
 }
 InputMap inputMap = (InputMap) inputMapObj;
 String actionCommand = (String) inputMap.get(oldKeyStroke);
 if (actionCommand == null) {
  System.err.println(&quot;reregisterGlobalKey actionCommand not found for inputMap: &quot; + uiManagerKey + &quot; keyStroke: &quot; + oldKeyStroke);
  return;
 }
 inputMap.remove(oldKeyStroke);
 if (newKeyStroke != null) {
  inputMap.put(newKeyStroke, actionCommand);
 }
}
&lt;/pre&gt;
&lt;br /&gt;
Keep in mind that these&amp;nbsp;uiManagerKeys and key strokes are Look and Feel dependend.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/4437095762059049341/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/4437095762059049341' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4437095762059049341'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4437095762059049341'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2013/10/java-swing-change-global-action-key.html' title='Java Swing: Change Global Action Key'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-8473923013324967819</id><published>2013-04-25T16:05:00.004+02:00</published><updated>2013-04-25T16:05:49.819+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Guava ClassPath Suite</title><content type='html'>Sometimes, one needs to run just a subset of&amp;nbsp;&lt;a href=&quot;http://junit.org/&quot;&gt;JUnit&lt;/a&gt;&amp;nbsp;test. The very good library &lt;a href=&quot;http://johanneslink.net/projects/cpsuite.jsp&quot;&gt;ClassPathSuite&lt;/a&gt;&amp;nbsp;(cpsuite) address this problem and is able to select test based on a class name, or another class properties (annotation, super class, etc.)&lt;br /&gt;
&lt;br /&gt;
In our project, we have needed a little bit more complicated filter&amp;nbsp;conditions than it offers. Afters studying ClassPathSuite and &amp;nbsp;&lt;a href=&quot;https://code.google.com/p/guava-libraries/&quot;&gt;Google&amp;nbsp;Guava&lt;/a&gt;&amp;nbsp;internals, I just figure out, some kind of more flexible ClassPathSuite can be created using Guava &lt;code&gt;Predicate&lt;/code&gt; and &lt;code&gt;ClassPath&lt;/code&gt; classes. So I have coded just one core file,&amp;nbsp;&lt;code&gt;GuavaClassPathSuite&lt;/code&gt;&amp;nbsp;which does what we need - scans the class path for JUnit tests and filter them by user defined Predicates. I have pushed the solution to GitHub as &lt;a href=&quot;https://github.com/xmedeko/gcpsuite&quot;&gt;gcpsuite&lt;/a&gt;, &lt;a href=&quot;https://github.com/xmedeko/gcpsuite&quot;&gt;Guava ClassPath Suite&lt;/a&gt;,&amp;nbsp;project. (Not very distinct name, but points to the Guava &lt;code&gt;ClassPath&lt;/code&gt;).

Either use it as a library, or simply copy&amp;amp;paste the &lt;code&gt;GuavaClassPathSuite.java&lt;/code&gt;, write your predicates and enjoy!</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/8473923013324967819/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/8473923013324967819' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/8473923013324967819'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/8473923013324967819'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2013/04/guava-classpath-suite.html' title='Guava ClassPath Suite'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-4531358988926724626</id><published>2012-09-13T08:40:00.004+02:00</published><updated>2013-07-15T13:22:39.563+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Swing"/><title type='text'>Java Swing: MigLayout, JScrollPane and JTextField Showing Horizontal Scrollbar</title><content type='html'>&lt;a href=&quot;http://www.miglayout.com/&quot;&gt;MigLayout &lt;/a&gt;is a great layout manager for Java Swing. However, it needs some time to discover it&#39;s nooks and crannies. One common problem is with components inside &lt;code&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html&quot;&gt;JScrollPane&lt;/a&gt;&lt;/code&gt;. &lt;br /&gt;
&lt;br /&gt;
We place all components inside the &lt;code&gt;JPanel&lt;/code&gt;, which has a &lt;code&gt;JScrollPane&lt;/code&gt; as parent. And we use MigLayout mostly with &lt;code&gt;fill&lt;/code&gt; and &lt;code&gt;grow&lt;/code&gt; parameters. Such layout ensures that a user can access all components by scrolling when she has a small screen.&lt;br /&gt;
&lt;br /&gt;
But I have a faces a problem with &lt;code&gt;JTextField&lt;/code&gt; containing a very long text. I usually use MigLayuot&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;b&gt;growx, width 50::&lt;/b&gt;&lt;/pre&gt;
&lt;br /&gt;
constraints for text fields. However, when a text files contains a very long String, then it forces JScrollPane to to enlarge it&#39;s width and show the horizontal scrollbar. The user usually need the text field to be as long as possible, but not to overflow the screen width, if possible. After a while, I have found a proper and simple solution, just set the preferred size as minimum:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;b&gt;growx, width 50:50:&lt;/b&gt;&lt;/pre&gt;
&lt;br /&gt;
Note: I have described the problem using component constraints, the solution is similar with row constraints&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;b&gt;fill, grow, 50:50:&lt;/b&gt;&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/4531358988926724626/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/4531358988926724626' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4531358988926724626'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4531358988926724626'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2012/09/java-swing-miglayout-jscrollpane-and.html' title='Java Swing: MigLayout, JScrollPane and JTextField Showing Horizontal Scrollbar'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-1610455629090755962</id><published>2012-01-20T09:27:00.000+01:00</published><updated>2012-01-20T09:27:29.792+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Eclipse"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Eclipse: Manual and Automatic Code Formatting</title><content type='html'>I like automatic code formatting in the &lt;a href=&quot;http://www.eclipse.org/&quot;&gt;Eclipse IDE&lt;/a&gt;. For Java see the settings in &lt;code&gt;Window -&amp;gt; Preferences -&amp;gt; Java -&amp;gt; Code Style -&amp;gt; Formatter&lt;/code&gt;. I Just hit &lt;code&gt;CTRL+SHIFT+F&lt;/code&gt; time from time and let the IDE do the boring job for me. &lt;br /&gt;
&lt;br /&gt;
Occasionally, I want the different result then the Eclipse Formatter produce. Especially when a method has many parameters. Sometimes I like to have all of them on one line, sometimes I like to break them into multiple times. Of course, the Eclipse  automatic formatter cannot be configured to meet wishes.&lt;br /&gt;
&lt;br /&gt;
The Eclipse formatter has a nice option &lt;code&gt;On/Off Tags&lt;/code&gt;:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;code&gt;@formatter:off&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@formatter:on&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;But it&#39;s tedious and ugly to write these tags into the code. (Even Code templates does not simply usage of On/Off Tags very much). Much more simpler solution is to append the Java comment &lt;code&gt;//&lt;/code&gt; ant the and of line, which you want to break. For example, I have configured the Eclipse formatter to have all method parameters on a single line. But when I write&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;this.someMethod(param1, //
    param2, //
    param3, //
    param4, //
    param5);
&lt;/pre&gt;Then the &lt;code&gt;CTRL+SHIFT+F&lt;/code&gt; reformatting let this piece of the code as is.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/1610455629090755962/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/1610455629090755962' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/1610455629090755962'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/1610455629090755962'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2012/01/eclipse-manual-and-automatic-code.html' title='Eclipse: Manual and Automatic Code Formatting'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-4506653009750963519</id><published>2011-10-06T14:24:00.002+02:00</published><updated>2011-10-06T14:37:01.154+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Javascript"/><title type='text'>Simple Javascript SCORM 2004 API</title><content type='html'>Just a very very simple implementation of SCORM 2004 API for LMS (RTS) developers. It implements just basic SCORM 2004 features. Hope, someone else may find it useful, see &lt;a href=&quot;http://sites.google.com/site/xmedeko/code/misc/simple-javascript-scorm-2004-api&quot;&gt;http://sites.google.com/site/xmedeko/code/misc/simple-javascript-scorm-2004-api&lt;/a&gt;.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/4506653009750963519/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/4506653009750963519' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4506653009750963519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4506653009750963519'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2011/10/simple-javascript-scorm-2004-api.html' title='Simple Javascript SCORM 2004 API'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-7056534406367848175</id><published>2011-05-31T09:51:00.001+02:00</published><updated>2011-05-31T13:39:35.624+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java zsync</title><content type='html'>The very first implementation of &lt;a href=&quot;http://zsync.moria.org.uk/&quot;&gt;zsync&lt;/a&gt; in Java: &lt;a href=&quot;http://sourceforge.net/projects/jazsync/&quot;&gt;http://sourceforge.net/projects/jazsync/&lt;/a&gt;. It has no release yet, check out the source code from SVN.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/7056534406367848175/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/7056534406367848175' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/7056534406367848175'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/7056534406367848175'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2011/05/java-zsync.html' title='Java zsync'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-4877160163397004413</id><published>2011-05-11T10:56:00.000+02:00</published><updated>2011-05-11T10:56:32.449+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="ZK"/><title type='text'>ZK-DL Released!</title><content type='html'>&lt;a href=&quot;http://zk.datalite.cz/zk-dl&quot;&gt;ZK-DL&lt;/a&gt;, an extension to the &lt;a href=&quot;http://www.zkoss.org/&quot;&gt;ZKoss framework&lt;/a&gt; (ZK 5), has been released under LGPL just a few weeks ago!. ZK_DL provides especially:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Integration with Spring.&lt;/li&gt;
&lt;li&gt;Flexible listbox, combobox and lovbox (a kind of combobox) components with automatic sorting and searching build upon the Hibernate Criteria API.&lt;/li&gt;
&lt;li&gt;And much more, see &lt;a href=&quot;http://zk.datalite.cz/zk-dl&quot;&gt;http://zk.datalite.cz/zk-dl&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
The documentation is at &lt;a href=&quot;http://zk.datalite.cz/zk-dl&quot;&gt;http://zk.datalite.cz/zk-dl&lt;/a&gt; and the source code can be found at &lt;a href=&quot;http://code.google.com/p/zk-dl/&quot;&gt;http://code.google.com/p/zk-dl/&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;I have used DL-ZK with EJB3 on JBoss 5.1 successfully.&lt;/b&gt; So I had not taken the advantage of ZK-DL Spring integration (IMHO better than original ZK Spring integration). Anyway the ZK-DL listobox, combobox and lovbox components has greatly simplified our development and provided a rich yet simple interface to our users. &lt;br /&gt;
&lt;br /&gt;
Many thanks to Jiří Bubník and Karel Čemus for their valuable help and cooperation. I wish DL-ZK long life and have many users!</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/4877160163397004413/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/4877160163397004413' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4877160163397004413'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4877160163397004413'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2011/05/zk-dl-released.html' title='ZK-DL Released!'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-6342751996174989330</id><published>2011-03-10T16:38:00.010+01:00</published><updated>2011-08-16T16:24:44.013+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>JPA Queries - A Few Methods to Simplify the Life with JPA/JPQL</title><content type='html'>The JPA &lt;a href=&quot;http://download.oracle.com/javaee/6/api/javax/persistence/Query.html#getSingleResult%28%29&quot;&gt;&lt;code&gt;Query.getSingleResult()&lt;/code&gt;&lt;/a&gt; method is usable only when the JPQL has exactly one result. Yeah, you can catch &lt;code&gt;NoResultException&lt;/code&gt; or &lt;code&gt;NonUniqueResultException&lt;/code&gt; exceptions, but the try ... catch block make the code less readable. So I have coded three static method to overcome this JPA deficiency:&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;public class Queries {
    /**
     * @param query
     * @return {@code true}, the query has one or more results, otherwise returns {@code false}.
     */
    public static boolean hasAnyResult(Query query) {
        query.setMaxResults(1);
        final List&amp;lt;?&amp;gt; list = query.getResultList();
        return list.size() &amp;gt; 0;
    }

    /**
     * @param query
     * @return The only one result or {@code null}, when the query has no result.
     * @throws NonUniqueResultException
     *             when the query has more than one result.
     */
    public static Object singleResultOrNull(Query query) {
        query.setMaxResults(2);
        final List&amp;lt;?&amp;gt; list = query.getResultList();
        final int size = list.size();
        if (size &amp;lt;= 0) {
            return null;
        }
        if (size &amp;gt; 1) {
            throw new NonUniqueResultException(&amp;quot;result returns more than one element&amp;quot;);
        }

        // return first element from the list
        return list.get(0);
    }

    /**
     * @param query
     * @return The first result or {@code null}, when the query has no result.
     */
    public static Object firstResultOrNull(Query query) {
        query.setMaxResults(1);
        final List&amp;lt;?&amp;gt; list = query.getResultList();
        final int size = list.size();
        if (size &amp;lt;= 0) {
            return null;
        }
        if (size &amp;gt; 1) { // should not happened, Hibernate bug?
            Logger.getLogger(Queries.class).error(&amp;quot;firstResultOrNull more rows returned. setMaxResults(1) does not work?&amp;quot;);
        }

        return list.get(0);
    }

    /**
     * Constructor. No instances.
     */
    private Queries() {
    }
}
&lt;/pre&gt;The &lt;b&gt;&lt;code&gt;hasAnyResult&lt;/code&gt;&lt;/b&gt; is the &lt;i&gt;boolean&lt;/i&gt; query method - you may test if something exists in database. The best is to use it with some JPQL query returning only id of an entity, e.g.:&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;Queries.hasAnyResult(em.createQuery(&amp;quot;SELECT id FROM Person WHERE ...&amp;quot;));&lt;/pre&gt;I think the usage of the &lt;b&gt;&lt;code&gt;singleResultOrNull&lt;/code&gt;&lt;/b&gt; is obvious. Use it, when you are sure the result returns one or no result. The exception &lt;code&gt;NonUniqueResultException&lt;/code&gt; should warn you that something is wrong, wither the query or the data.&lt;br /&gt;
&lt;br /&gt;
The &lt;b&gt;&lt;code&gt;firstResultOrNull&lt;/code&gt;&lt;/b&gt; returns just the first result of the query. Typically, use it with queries with &quot;ORDER BY&quot; statement, like &quot;get the Person with lowest/highest salary.&quot;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/6342751996174989330/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/6342751996174989330' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/6342751996174989330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/6342751996174989330'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2011/03/jpa-queries-few-methods-to-simplify.html' title='JPA Queries - A Few Methods to Simplify the Life with JPA/JPQL'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-1166732407029538805</id><published>2011-02-15T15:17:00.003+01:00</published><updated>2011-05-20T13:43:36.427+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Oracle Like Decode in JasperReports</title><content type='html'>Oracle SQL has a nice function &lt;a href=&quot;http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions040.htm&quot;&gt;DECODE&lt;/a&gt;. I am using Java Beans data source for &lt;a href=&quot;http://jasperforge.org/projects/jasperreports&quot;&gt;JasperReports&lt;/a&gt; and sometimes I have missed such a function in JasperReports expressions. One can achieve the same functionality by a &lt;a href=&quot;http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html&quot;&gt;ternary operator &lt;code&gt;? :&lt;code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/a&gt;. But it is not nice for more complicated conditions. So I have created my own Java decode:&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;public static Object decode(Object value, Object... arg) {
  if (arg == null) {
    return value;
  }
  int n = arg.length;
  for (int i = 0; i &amp;lt; n; i+=2) {
    int j = i + 1;
    if (j &amp;gt;= n) {
      // only the default remains
      return arg[i];
    }
    if (equal(value, arg[i])) {
      return arg[j];
    }
  }
  // no match found, no default is specified
  return value;
}
&lt;/pre&gt;Then import it into JasperReports &lt;code&gt;*.jrxml&lt;/code&gt; report by:&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-xml&quot;&gt;&amp;lt;import value=&quot;static com.mypackage.MyClass.decode&quot;/&amp;gt;
&lt;/pre&gt;And use it in the report like:&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;&quot;&quot; + decode(count, 0, &quot;no one&quot;, 1, &quot;one&quot;, 2, &quot;two&quot;, &quot;too many: &quot; + count);
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/1166732407029538805/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/1166732407029538805' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/1166732407029538805'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/1166732407029538805'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2011/02/oracle-like-decode-in-jasperreports.html' title='Oracle Like Decode in JasperReports'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-4728352107375014100</id><published>2011-01-25T20:28:00.000+01:00</published><updated>2011-01-25T20:28:00.020+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="ZK"/><title type='text'>ZK and Converting to a Negative Number</title><content type='html'>When I was working with &lt;a href=&quot;http://www.zkoss.org/&quot;&gt;ZK Web Framework&lt;/a&gt;, I have needed to convert some number to the negatives for the databinding. I have created a new type converter &lt;b&gt;&lt;code&gt;NumberNegativeConverter&lt;/code&gt;&lt;/b&gt;. Hope, someone else may find it useful, see &lt;a href=&quot;http://sites.google.com/site/xmedeko/code/zk-web-framework/numbernegativeconverter&quot;&gt;http://sites.google.com/site/xmedeko/code/zk-web-framework/numbernegativeconverter&lt;/a&gt;.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/4728352107375014100/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/4728352107375014100' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4728352107375014100'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4728352107375014100'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2011/01/zk-and-converting-to-negative-number.html' title='ZK and Converting to a Negative Number'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-4321369758907234459</id><published>2010-12-03T15:29:00.005+01:00</published><updated>2012-07-30T11:09:35.946+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java: A Simple Assert Class for Design By Contract Checks</title><content type='html'>Very often, the programmer are lazy to include design by contract checks into the code. That means argument checking in the beginning of the method, checking of a wrong state in the method body, etc. The Java &lt;a href=&quot;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html&quot;&gt;assert&lt;/a&gt; construct cannot be used for this purpose, since it is switched off by default (one has tu run &lt;code&gt;java -ea&lt;/code&gt; to switch on the Java asserts). To simplify the design by contract checks, I have made three simple static methods:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;/**
 * A static class for simple run-time checks. The methods from this class may (and should) be used to check arguments
 * and code for design-by-contract.
 * &amp;lt;p&amp;gt;
 * These are the methods for lazy programmers. If you like to write more complicated messages (and God bless you for
 * that), use the classic {@code if (...) throw ... } way.
 * &amp;lt;p&amp;gt;
 * It is not possible to switch off these checks, do not use them for any memory or CPU consuming operations.
 * 
 * @author xmedeko
 * 
 */
public class Assert {

 /**
  * Check, that the condition is {@code true}.
  * &amp;lt;p&amp;gt;
  * Usage:
  * 
  * &amp;lt;pre&amp;gt;
  * Assert.state(i &amp;amp;lt;= maxIter, &amp;amp;quot;iteration number exceeded&amp;amp;quot;);
  * &amp;lt;/pre&amp;gt;
  * 
  * @param check
  * @param message
  *            An error message.
  */
 public static void state(boolean check, String message) {
  if (check)
   return;
  throw new IllegalStateException(message);
 }

 /**
  * Check, that the object is not null. 
  * &amp;lt;p&amp;gt;
  * Usage:
  * 
  * &amp;lt;pre&amp;gt;
  * Assert.notNull(entity, &amp;amp;quot;entity&amp;amp;quot;);
  * &amp;lt;/pre&amp;gt;
  * 
  * 
  * @param o
  * @param message
  *            An error message.
  */
 public static void notNull(Object o, String message) {
  if (o != null)
   return;
  throw new NullPointerException(message);
 }

 /**
  * Check, that the argument fullfills the given condition.
  * &amp;lt;p&amp;gt;
  * Usage:
  * 
  * &amp;lt;pre&amp;gt;
  * Assert.argument(from &amp;amp;gt; 0, &amp;amp;quot;from has to be greater then zero&amp;amp;quot;);
  * &amp;lt;/pre&amp;gt;
  * 
  * @param check
  * @param message
  *            An error message.
  */
 public static void argument(boolean check, String message) {
  if (check)
   return;
  throw new IllegalArgumentException(message);
 }

 /** No instances. */
 private Assert() {
  throw new UnsupportedOperationException();
 }

}
&lt;/pre&gt;
&lt;br /&gt;
Note, to check argument for not &lt;code&gt;null&lt;/code&gt; value, do not use &lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;Assert.argument(obj != null, &quot;obj is null&quot;);
&lt;/pre&gt;
but use instead:&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;Assert.argument(obj != null, &quot;obj&quot;);
&lt;/pre&gt;
&lt;br /&gt;
Also, do not put any String concatenation or other operation into the &lt;code&gt;argument&lt;/code&gt;. These are the methods for lazy programmers. If you like to write more complicated messages (and God bless you for that), use the classic &lt;code&gt;if (...) throw ...&lt;/code&gt; way.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Update 29.3.2011:&lt;/b&gt; I have just found out that Spring frameworks has &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/util/Assert.html&quot;&gt;org.springframework.util.Assert&lt;/a&gt; class, too.
&lt;br /&gt;
&lt;b&gt;Update 30.7.2012:&lt;/b&gt; Google Guava has a very similar solution in the class &lt;a href=&quot;http://code.google.com/p/guava-libraries/wiki/PreconditionsExplained&quot;&gt;Preconditions&lt;/a&gt;.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/4321369758907234459/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/4321369758907234459' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4321369758907234459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/4321369758907234459'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2010/12/java-simple-assert-class-for-design-by.html' title='Java: A Simple Assert Class for Design By Contract Checks'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-91841026369532651</id><published>2010-12-02T15:05:00.003+01:00</published><updated>2011-05-20T13:44:59.329+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="ZK"/><title type='text'>ZK Calendar Colors</title><content type='html'>Unfortunately, colours for the &lt;a href=&quot;http://code.google.com/p/zkcalendar/&quot;&gt;ZK Calendar&lt;/a&gt; has to be specified in the Java. They cannot be set in a CSS file. Because writing hexadecimal colour codes directly in the code is error prone, I have created a simple enum constant with a set of ZK Calendar colors:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-java&quot;&gt;public enum ZkCalendarsColor  {
 GREY(&quot;#B4BAB7&quot;, &quot;#72706F&quot;),
 GREEN(&quot;#4CB052&quot;, &quot;#0D7813&quot;),
 KHAKI(&quot;#BFBF4D&quot;, &quot;#88880E&quot;),
 PURPLE(&quot;#B373B3&quot;, &quot;#7A367A&quot;),
 RED(&quot;#D96666&quot;, &quot;#A32929&quot;),
 BLUE(&quot;#668CD9&quot;, &quot;#3467CE&quot;);
 
 private String contentColor;
 private String headerColor;
 
 private ZkCalendarsColor(String contentColor, String headerColor) {
  this.contentColor = contentColor;
  this.headerColor = headerColor;
 }

 public String getContentColor() {
  return contentColor;
 }

 public String getHeaderColor() {
  return headerColor;
 }
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/91841026369532651/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/91841026369532651' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/91841026369532651'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/91841026369532651'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2010/12/zk-calendar-colors.html' title='ZK Calendar Colors'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-5076309392433218943</id><published>2010-10-14T10:24:00.004+02:00</published><updated>2011-05-20T13:49:13.698+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><title type='text'>Fixing Line Terminators for Cygwin CVS</title><content type='html'>I have to use a Windows operating system (unfortunately :-)) and CVS in my current job. Thanks to the &lt;a href=&quot;http://www.cygwin.com/&quot;&gt;Cygwin&lt;/a&gt;, I can work with Windows almost as effectively as in Linux. The CVS (CVSNT) installation in Windows and other CVS tools installed in on my Windows box (TortoiseCVS, Eclipse) stores the CVS files with CRLF line terminators (EOLs). The Cygwin CVS client requires the classic UNIX LT terminator. Fortunatelly, the Windows CVS clients can read the UNIX-way text files, too. So, I have creates a simple bash script to forx this problem:&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-sh&quot;&gt;#!/bin/sh
find . -type f -path &#39;*/CVS/*&#39; -print0 | xargs -0 dos2unix 
&lt;/pre&gt;Hope, someone may find it useful.</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/5076309392433218943/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/5076309392433218943' title='Komentáře: 1'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/5076309392433218943'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/5076309392433218943'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2010/10/cygwin-and-cvs.html' title='Fixing Line Terminators for Cygwin CVS'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-3667913374368191251</id><published>2010-08-31T11:22:00.001+02:00</published><updated>2010-09-03T10:50:17.505+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>JasperReports PDF Font Mapping</title><content type='html'>Are your font &lt;b&gt;bold&lt;/b&gt; and &lt;i&gt;italic&lt;/i&gt; font properties lost when you export your JasperReports report into the PDF? Do you wander, why PDF preview in the iReport has all fonts right? Check this link out, mate: &lt;a href=&quot;http://sites.google.com/site/xmedeko/code/misc/jasperreports-pdf-font-mapping&quot;&gt;http://sites.google.com/site/xmedeko/code/misc/jasperreports-pdf-font-mapping&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/3667913374368191251/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/3667913374368191251' title='Komentáře: 2'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/3667913374368191251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/3667913374368191251'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2010/08/jasperreports-pdf-font-mapping.html' title='JasperReports PDF Font Mapping'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-665270312096612395.post-302070515207552357</id><published>2010-08-05T08:13:00.004+02:00</published><updated>2010-09-03T10:50:38.709+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>JBoss and HSQLDB 2.0.0</title><content type='html'>&lt;a href=&quot;http://hsqldb.org/&quot;&gt;HSQLBD 2.0 GA&lt;/a&gt; has been released on 7 June 2010 with &lt;a href=&quot;http://hsqldb.org/web/features200.html&quot;&gt;a lot of new cool features&lt;/a&gt;. I have needed some of them for my JBoss 5.1.0GA, so I have just replaced the library &lt;code&gt;common/lib/hsqldb.jar&lt;/code&gt; and voilà, it runs almost without any problem (Hibernate/JPA usage) as before. I have made just one small change, see bellow. Even the HSQLDB Database Manager starts from JMX console. My personal feeling is that the version 2.0 is a little bit faster than 1.8.0, but I have not made any benchmarks.&lt;br /&gt;
&lt;br /&gt;
The HSQLDB 2.0 upgrade notes:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;In my &lt;code&gt;import.sql&lt;/code&gt; I had to change all timestamp formats from &lt;code&gt;&#39;2010-08-06 8:00:00&#39;&lt;/code&gt; to &lt;code&gt;&#39;2010-08-06 08:00:00&#39;&lt;/code&gt;, i.e. I had to zero pad the hours into two decimal places.&lt;/li&gt;
&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://xmedeko.blogspot.com/feeds/302070515207552357/comments/default' title='Komentáře k příspěvku'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/665270312096612395/302070515207552357' title='Komentáře: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/302070515207552357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/665270312096612395/posts/default/302070515207552357'/><link rel='alternate' type='text/html' href='http://xmedeko.blogspot.com/2010/08/jboss-and-hsqldb-200.html' title='JBoss and HSQLDB 2.0.0'/><author><name>Ondrej Medek</name><uri>http://www.blogger.com/profile/02465917905307330831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>