Open Source: core library

Few months ago, after I moved to GitHub, one of my util projects was open sourced. It was, simply called, core: I have written this library few years ago and was actively using on Java projects. In some places it crosses with Apache Commons Lang library, but I, actually, like my child more :)

I'd like to describe some key classes and show samples of use. Some of them are enumerated below.

StringUtils contains few oftenly used methods, like:

  • isEmpty(), isBlank(), isNotEmpty(), isNotBlank(), isBlankTrimmed(), isNotBlankTrimmed() - used to check whether string is empty or not. The difference between empty and blank, is that empty is either null or empty string, while blank is always blank string and can't be null.

  • cut() - used to cut string if length is more than specified and appends with specified suffix. The only difference is that this method tries to split by space, so don't cut the word. Sample:

    String string = "some string goes here";
    assertEquals "some string...", StringUtils.cut(string, 15, "...");

  • trimIfNotNull() - if input string is not null, then trim it and return result:

    String string = " Hello ";
    assertEqual "Hello", StringUtils.trimIfNotNull(string);
    assertNull StringUtils.trimIfNotNull(null);

  • replaceNotAlphaNumeric() - replace all characters that are not letter or digit with specified one or "_" by default.

ConversionUtils contains some simple but useful methods to convert string value to numeric and boolean types. Contains methods getInteger(), getLong(), getBoolean(), getDouble(), getDate(), getFloat():

assertEquals 1, ConversionUtils.getInteger("1");
assertNull ConversionUtils.getInteger("hello");
assertEquals 5, ConversionUtils.getInteger("hello", 5);

KeyGenerator was created to generate API keys, passwords and other random stuff. It has one highly configurable method and few helpful methods that uses it. There is a way to generate keys with alpha and/or numeric and/or special symbols.

That would be hard to write assertions for samples, but here are simple use cases:

KeyGenerator.generateKey(10, KeyGenerator.WITH_ALPHA_LOW | KeyGenerator.WITH_ALPHA_UP);

After I found some issues with Apache Commons-Lang ToStringBuilder, I wrote my own replacement, and called it... ToStringBuilder :) It is very simple in use:

class Blog {
private String name;
private String author;
private int year;

public String toString() {
new ToStringBuilder(Blog.class)
.field("name", name)
.field("author", author)
.field("year", year)

Blog blog = new Blog();
blog.setName("Java UA");
blog.setAuthor("Ruslan Khmelyuk");

assertEquals "Blog[name=Java UA, author=Ruslan Khmelyuk, year=2010]", blog.toString();

There are much more interesting tools, like ArgumentAssert and StateAssert used to assert arguments and program state respectively.

CollectionUtils also contains few useful methods, and I'm not going to describe them here.

Library is open to review and use. Still it's definitely not the best one and, I think, has value only for me.

Neverending Groovy: map with default values

More and more Groovy openings for me every day!

Just today, I found that there is a way to setup default value for map entry, if absent. In some cases it may be very handy.

For example, I need to prepare a map with words counts. This simple task should be coded simple too, like:

def map = [:]
words.each { word -> map[word]++ }

But it's not working and fails with NPE exception. Why? Because it's hard to increment null.

Although, there is simple way to avoid this problem even using Groovy API:

def map = [:]
words.each { word -> map[word] = map.get('word', 0) + 1

Here 2nd parameter of map.get() method is the default value, returned if there is no value for key 'word'. And now it's working fine!
Still there is another way, and, as for me, it helps to make code clean and safe. Safe because default logic can be used elsewhere we want to use our map object.

def map = [:].withDefault { 0 }
words.each { word -> map[word]++ }

I bet there is a better way and I'm going to find it...

MySQL pager

I very liked to use psql command. Among different usability things, it uses less command to show query results. That was the thing I always wanted to have in mysql command. And was sure that's not possible, and till today was suffered in silence.

But today, quite unexpected for me, I found option pager for mysql command. This option let me set the program, I want to use to see query search results. So, to use less program, I was just need to start mysql with --pager parameter:

$ mysql --pager=less

or in mysql console:

mysql> pager=less

Now I'm happy to use it! And you should try too :)

Neverending Groovy

Вже кілька місяців коджу в основному на Groovy. Прекрасна мова, прекрасна підтримка від IntelliJIdea. Часто знаходжу все нові і кращі спрособи реалізації деколи тривіальних речей.

Для прикладу, необхідно отримати список із значень певного поля кожного елементу списку. Спочатку, я писав доволі простий і зрозумілий код, ясно ще під впливом Java:

List names = []
for (Person person : persons) {
names << person.firstName

З часом я удосконалювався і коду ставало менше:

def names = []
persons.each { names << it.firstName }

Виглядає дуже навіть зрозуміло, пишеться швидко і ще швидше читається.
Але і це не був край досконалості, і трохи пізніше я почав писати вже:

def names = persons.collect { it.firstName }

І цей кусок коду поражав мене тим, що був тільки 1 рядок, назва методу collect є зрозумілішою, ніж each у цьому конкретному випадку. І так, може, і писав би й дальше, якби не вирішив спробувати використати для цього spread оператора (*.). І тепер коду стало ще менше:

def names = persons*.firstName

Відмінністю від collect є не тільки простота написання, але і краща швидкодія.

І ось, в процесі написанні цієї статті, я відкрив для себе ще один спосіб. Коли починав писати цю статтю кілька хвилин тому, я вже не вірив, що може бути щось простіше, ніж використання spread оператора для вирішення моєї задачі. Але як виявилося, для отримання значення полів, використовувати spread оператор не є необхідним, і можна написати банально і просто:

def names = persons.firstName

Тож сиджу і думаю, а як можна ще простіше. Боюся зарікатися, що простіше вже бути не може, як вже зрозумів свою помилку кілька хвилин тому.

Groovy цікава мова, з якою все простіше і веселіше. Тож копати в Groovy, Python чи Ruby!

Book Review: 97 Things Every Software Architect Should Know

More then year ago I bought book 97 Things Every Software Architect Should Know for myself. I was expecting to get a book with some useful technical advices how to create cool software architecture,- that's what was always interesting for me.

Instead I've got the book with 97 small articles, each 2 pages long. No technical advices, no silver bullet, no advices on what and how use design patterns or modern technologies. Truly say, after reading few articles, I was upset. And leaved the book on my bookshelf and move on.

A year later, ie a month ago, I've took this book into my hands and decided that it's time to try to read it again. (Yes, I have a habit to re-read books again a year or two later, as found that it helps to understand much more and see the forest behind the trees).

I'm still reading this - few articles per week,- but decided to note my thoughts as review in this article.

In this attempt, book is very interesting. A year later my vision was changed cardinally again and now, I think, I can understand what those guys mean when they were writing their posts. These all can be said simply in one sentence:
Software Architects are bridges between business needs, software needs, technical needs and team needs.

Especially interesting were next articles for me (remember, I'm still reading):

  • Don't Put Your Resume Ahead of the Requirements

  • It Is All About The Data

  • Pattern Pathology

  • If You Design It, You Should Be Able to Code It

  • Empower Developers

  • Chances Are, You Biggest Problem Isn't Technical

  • Business Drives

  • Great Content Creates Great Systems

  • Start with a Walking Skeleton

  • Communication Is King; Clarity and Leadership, Its Humble Servants

I'm sure articles' titles are saying enough, so will not describe them. Want know more - just go and buy (as I did :)

A week ago I was reading "Business Drives" article. And especially remembered the summary of this article: "The long-term interests of the software development team are best served when business drives". And I agree with this summary. It's sad to know, that many software developers are still in the race for better skills and modern technologies if this is in damage for the project and business.

It's one of that books, that reminds that software is making by humans for humans.

So, my advise is to get and read this book; hope you're ready for it and hope I'm too :)

PS: Just found another review of this book

Groovy annotations

As I wrote in my last post Groovy 1.8 is still in progress. One of the new features I liked a lot is annotation support for AST transformations (i.e. @ToString, @Canonical etc.).

As I'm still acquainted with Groovy, I've decided to look what useful annotations it has already. So this post is about what was found.

There are next annotations in the groovy.lang package:

  • @Lazy
  • @Immutable
  • @Newify
  • @Singleton
  • @Mixin
  • @Category

Also want to describe them with few simple samples.


This annotation can be applied to class field. As result new instance for field value is created on get method call for field property. In this case Groovy generates getter method for you, that checks whether field is not initialized yet and initialize it in this case, otherwise returns its value:

@Lazy T x

generates next code:

private T $x

T getX() {
if ($x != null)
return $x
else {
$x = new T()
return $x

You also can do lazy field initialization with specified value, just assign this value for field with @Lazy annotation:

@Lazy T x = new T(code: 'S')

That will generate next code:

private T $x

T getX() {
T $x_local = $x
if ($x_local != null)
return $x_local
else {
synchronized(this) {
if ($x == null) {
$x = new T(code: 'S')
return $x

Absolutely useful thing with only a few of keypresses. That's awesome.

Read more in GroovyDoc


Used to mark classes and make them immutable. That's mean that your class becomes final and nobody can change value field value and you get tuple and map based constructors. Also Groovy generated toString(), hashCode() and equals() methods for you. You should also need to be sure that fields types are immutable, primitives, strings, enums or are annotated with @Immutable.

Read more in GroovyDoc


This annotation is perfect for DSL expressions. With it you can change creating new objects expression, for example, use

def b = Book('The Best Book Ever')

instead of

def b = new Book('The Best Book Ever')

Read more in GroovyDoc


Well, another annotation that makes your life easier. As you maybe already understand, this annotation is for types and make them singletons.
getInstance() method is generated for you, so simply call it when you need new instance. If you set annotation lazy parameter to be true, your singleton instance will be created on first call to this method. As for this case method supports double checking locks, you may be sure that only one instance will be created. Here is some sample:

class Player {
def play() {}
def stop() {}



Read more in GroovyDoc and Singleton transformation.


Used to mix one or few types into annotated type. As result, you can have a class that have members (fields and methods) from other classes, specified in the annotation. And here is sample:

class Book {
String name
String author

def find(text) {

class HardBookMetadata extends Metadata {
int pages
String publication

def metadata = new HardBookMetadata()
metadata.title = 'Some cool title' = 'Ruslan Khmelyuk'
metadata.pages = 400

println metadata.find('cool')

As you see, using variable metadata of HardBookMetadata type have access to Book members.
Yeah, this also can be used with @Category.

Read more in GroovyDoc.

Waiting for Groovy 1.8

Groovy 1.8 is going to be released in December 2010. First beta release is already open to download and second beta release will be ready in September.

Groovy developers team preparing for us a few important surprises.

They added more AST transformations, for different cases like @Canonical, @EqualAndHashCash, @ToString and many others. You may use such annotations to generate toString(), equals(), hashCode() methods on compilation. @InheritConstructors helps to generate inherit all parent constructors, and here is link to task.

Another interesting feature is Closure composition, so now it's possible to use special syntax to compose multiple closures, like:

def log = { a -> println(a); a }
def save = { a ->; a}
def notify = { a -> notificationService.notify(a); a }

// so now instead of calling notify(save(log(user)))
// you may call it like
def action = log >> save >> notify

// or even like
notify << save << log << user // is that awesome or awful?!

Also they prepared few performance improvements: avoiding calling methods when it's not necessary and processing operations on integers as primary type values but not objects (like, incrementing or adding). As result Groovy can be used for mathematical calculations without performance issues. In this presentation you can read much more about this. And here are related jira tasks so anyone can watch the progress.

This issue is fixed so they can't say that "Ruby is right, Groovy is wrong" anymore :)

Annotations now can accept closure as argument:

@Validator { password.size() > 6 }
String password

This task contains more information and links.

Closure currying is a powerful thing. And now it's possible to curry right and 2nd (of three) parameter. Moreover no need to wait Groovy 1.8 to try this, because it's available right now and right here, starting from version 1.7.2 of course ;)

Well, that's all about this for now. Waiting for Groovy 1.8 and for more interesting things to be released this December.

SpringWS and Jaxb

Well, truly say this small post is not about how to use SpringWS and Jaxb together. It's more about potential power of this combination. I know, that combination is in release for few years alredy and used by thousands of developers. But this article is more for guys in doubt if they really need SpringWS rather than for experienced Spring WS developers.

First of all, I want to say that Spring WS is very powerful framework. I've noticed it when version 1.0 beta was released. As everything in Spring it has good API, simple implementation model and as result it was very flexible. And this mean that you can easily introduce need changes and extensions without changing Spring WS source code.

From the beginning, the Spring WS team was saying that SpringWS should support not only low-level request processing, when you have some object that represents input XML document or build output XML document, but can use different mapping and translation libraries to make it for you. And here is a place for Jaxb.

To enable marshaling you need just to do few simple changes:

  1. Add marshaller bean of org.springframework.oxm.jaxb.Jaxb2Marshaller type:
    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">

  2. Add Marshaling Method Endpoint Adapter to yours spring.xml:
    <bean class="">
    <property name="marshaller" ref="marshaller"/>
    <property name="unmarshaller" ref="marshaller"/>

  3. Well, that's all :)

And then, after some testing, I found that it would be nice to trim input text fields and do other post-marshalling operations. And I was sure there was a normal way to do that with rather Spring WS or Jaxb. After a few minutes of searching, I have found the way and had completely working code that was doing what I was needed.

The catch is that Jaxb Unmarshaller supports listeners and Spring's Jaxb2Marshaller has a property unmarshallerListener, that points your listener to Unmarshaller's listeners.
With only 3 lines of configuration we have own listener called on each unmarshalling:
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="unmarshallerListener">
<bean class="TargetHandlerUnmarshallerListener"/>

Here is TargetHandlerUnmarshallerListener code:
import javax.xml.bind.Unmarshaller;

public class TargetHandlerUnmarshallerListener extends Unmarshaller.Listener {

@Override public void afterUnmarshal(Object target, Object parent) {
super.afterUnmarshal(target, parent);

if (target instanceof PostPopulatedHandler) {
((PostPopulatedHandler) target).postPopulated();

In this class I override only afterUnmarshal() method, that is called after XML is transformed to the Java object. You can also override beforeUnmarshal() method if you need.

PostPopulatedHandler is my own interface that expose only postPopulated() method. Each class knows what to do after unmarshalling, and declares it in the postPopulated() method. Of course, there can be used some annotation, but interface option is the most simple, easier and obvious way.

So, some summary is next: Spring libraries are awesome, because they always provide very good API and you always can do what you need without changing library source code. Jaxb is awesome as well. I'm noticing this, because it's always hard to write good API, especially in multifunctional libraries.

Migrate from SVN repository to Git

As I was saying in my previous post it is easy to migrate you project from Subversion to Git.

Really, you will need just a few commands to run.

First of all you need to make a clone of Subversion repository using git-svn tool:

$ git svn clone --authors-file=authors.txt https://example/svn/MyRepo/trunk/project project

On this command git will go through the history of the project and fetch it into local git repository. If your subversion repository is using standard layout (i.e. trunk, branches and tags paths are present in the SVN repository root tree) than you may be interesting in --stdlayout option. You may also specify custom path for trunk or branches or tags using -T or -t or -b option respectively.

Option --authors-file contains the path to the file with committers mapping.
This file contains SVN committer mapping to git user per line.
For example:

ruslan = Ruslan <>
john = John Smith <>

So now you have local working copy that is managed by Git and also you have all project history in your local repository. It's easy to check what branches and tags you have using next commands:

$ cd project
$ git branches
$ git tag -l

If you have remote Git repository (maybe in GitHub), than you may want to push source code with all history to that repository. And it is not a problem, as you have already everything you need.

1. So add remote repository:

$ git remote add origin

2. And push to the remote repository:

$ git push origin master

3. and be happy - now you have migrated to the git repository.

Remove directory with SVN repository clone and make another clone but from git repository and continue using it:

$ cd ..
$ rm -rf project
$ mkdir project
$ cd project
$ git init
$ git remote add origin
$ git pull origin master

Thats how it worked for me 2 months ago.

Git and GitHub

Completely moved to Git and it's awesome. Feeling like someone unleashed my hands. Git works much faster than Subversion in everyday use. Using branches is easy and NORMAL and I'm using it day by day. No more need to have many directories with strange names and different project versions. No more fear before merging. No more failures on merging.

My opinion is that everyone must try and use some of distributed version control system, like Git or Mercurial or Bazaar. I've started using Git intensively with projects under Subversion. It was possible thanks to the git-svn project and I was working on Windows in that times. Now I'm on Linux and using git is much easier and natural. git-svn also is very useful to migrate projects from svn repository to the git repository. On checking out project you need to wait some time, because it reads the all history of repository with all changes, that is pretty long in subversion for large projects. As soon you have clone of the project svn repository, you push your changes to central git repository if any. If you need you can do commiters mapping. If you need, you can continue work with both repositories.

And what about GitHub? It's another awesome software. A lot of open source projects are using GitHub. You easily can have and use private repositories and it's pretty cheap - only $7 per month for 5 private repositories.
Each repository goes with wiki, basic issues tracker, useful reports and many others. Especially, I like Network graph, because it helps to visualize commits and merge history.

Some interesting links:
1. ProGit online book
2. Git with Subversion
3. GitCasts
3. The way GitHub helped Erlang and the way Erlang helped Github

Other related posts:
1. Git and Subversion work together

Ruslan and Linux

Давненько я нічого не писав...

За такий здавалося нетривалий період часу, багато чого встигло помінятися, тому, думаю, буде що розсказати.

Мабуть, найбільшою зміною для мене був цілковитий перехід на Linux. Ось уже від початку травня я є гордим користувачем Ubuntu 10.04. Перехід видався значно простішим, ніж можна було очікувати, але це скоріше за все результат моїх попередніх поривань в цю сторону, а також нетривалого, але плідного використання Slackware.

Важко далося використання MS SQL Server на локальній машині під Linux. Не можу сказати як я вирішив цю проблемо, тому що я її не вирішив. Використовувати віртуальну машину було нереально, адже в результаті тормозило все і вся. Тому довелося використовувати віддалений сервер і в результаті працювати тільки в онлайн. Благо, тепер потреба в ньому мінімальна.

Декілька слів про Ubuntu. Зручно, приємно, вражаюче і крім цього є консоль із всіма її потугами. Через кілька тижнів використання, я вже не міг собі уявити себе без Linux, а Windows 7 почав виглядати поцікавому і, навіть можна сказати, екзотично.
А ще недавнo я для себе відкрив, що в mc можна використовувати мишку. І це могло би перевернути мій світ, але не стало ;)

Люблю консоль. Консоль та набір скриптів дозволили оптимізувати роботу в Linux та роботу по проектах.

Screen cast on linux with ffmpeg

Few days ago I was happy to found the link with 10 useful Linux commands at

The last one command was the most interesting for me, as it helps to make screen casts quickly and easy with only one command:

ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq ~/out.mpg

Moreover ffmpeg helps to setup video quality, convert it to FLV format, so you can publish video online with a few clicks!

Beautiful Code

Зовсім недавно повернувся зі Львова. Купив собі в Гіаді декілька цікавих книжок. Серед них і Beautiful Code. Тільки от почав її читати.

В книзі міститься 33 історії від відомих програмістів, в яких вони діляться враженням від певного коду чи технології. Кожна історія проникнена поглядами і думками на цікаві речі, що розглядаються з призми красивого коду. Інколи код не зовсім-то й красивий на перший погляд, і автори намагаються відкрити ту красу, що побачили вони.

А ще автори не відчувають жодних рамок у виборі красивого коду, і в книзі можна зустріти C, C++, Perl, Java, Python.

Так що раджу почитати.