úterý 25. ledna 2011

ZK and Converting to a Negative Number

When I was working with ZK Web Framework, I have needed to convert some number to the negatives for the databinding. I have created a new type converter NumberNegativeConverter. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/zk-web-framework/numbernegativeconverter.

pátek 3. prosince 2010

Java: A Simple Assert Class for Design By Contract Checks

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 assert construct cannot be used for this purpose, since it is switched off by default (one has tu run java -ea to switch on the Java asserts). To simplify the design by contract checks, I have made three simple static methods:

/**
 * 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.
 * <p>
 * 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.
 * <p>
 * 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}.
  * <p>
  * Usage:
  * 
  * <pre>
  * Assert.state(i &lt;= maxIter, &quot;iteration number exceeded&quot;);
  * </pre>
  * 
  * @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. 
  * <p>
  * Usage:
  * 
  * <pre>
  * Assert.notNull(entity, &quot;entity&quot;);
  * </pre>
  * 
  * 
  * @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.
  * <p>
  * Usage:
  * 
  * <pre>
  * Assert.argument(from &gt; 0, &quot;from has to be greater then zero&quot;);
  * </pre>
  * 
  * @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();
 }

}

Note, to check argument for not null value, do not use
Assert.argument(obj != null, "obj is null");
but use instead:
Assert.argument(obj != null, "obj");

Also, do not put any String concatenation or other operation into the argument. These are the methods for lazy programmers. If you like to write more complicated messages (and God bless you for that), use the classic if (...) throw ... way.

Update 29.3.2011: I have just found out that Spring frameworks has org.springframework.util.Assert class, too.
Update 30.7.2012: Google Guava has a very similar solution in the class Preconditions.

čtvrtek 2. prosince 2010

ZK Calendar Colors

Unfortunately, colours for the ZK Calendar 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:

public enum ZkCalendarsColor  {
 GREY("#B4BAB7", "#72706F"),
 GREEN("#4CB052", "#0D7813"),
 KHAKI("#BFBF4D", "#88880E"),
 PURPLE("#B373B3", "#7A367A"),
 RED("#D96666", "#A32929"),
 BLUE("#668CD9", "#3467CE");
 
 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;
 }
}

čtvrtek 14. října 2010

Fixing Line Terminators for Cygwin CVS

I have to use a Windows operating system (unfortunately :-)) and CVS in my current job. Thanks to the Cygwin, 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:
#!/bin/sh
find . -type f -path '*/CVS/*' -print0 | xargs -0 dos2unix 
Hope, someone may find it useful.

úterý 31. srpna 2010

JasperReports PDF Font Mapping

Are your font bold and italic 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: http://sites.google.com/site/xmedeko/code/misc/jasperreports-pdf-font-mapping

čtvrtek 5. srpna 2010

JBoss and HSQLDB 2.0.0

HSQLBD 2.0 GA has been released on 7 June 2010 with a lot of new cool features. I have needed some of them for my JBoss 5.1.0GA, so I have just replaced the library common/lib/hsqldb.jar 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.

The HSQLDB 2.0 upgrade notes:
  1. In my import.sql I had to change all timestamp formats from '2010-08-06 8:00:00' to '2010-08-06 08:00:00', i.e. I had to zero pad the hours into two decimal places.

středa 21. července 2010

ZK 3.6 CSRF Protection

Unfortunately, ZK Web Framework version 3.6 and earlier lack any CSRF protection. i have created a very basic event filter to tackle this issue. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/zk-web-framework/zk-3-6-csrf-protection. It is just a very first version, leave me a comment, if you have some suggestion.

Update 15th Sep 2010: Improved protection and logging.