Saturday, April 2, 2016

massive directory name case change

Some time ago I had a disk failure. One project code was affected. I managed to restore almost all data, but result was often a file with name changed to upper case. As this happened to a windows machine, I could probably live with that, but it turned out package name in java is case sensitive, and Idea was not able to do quick fix as quickly as I would expect. Manual change is not an option for a developer.
So let's automate that. Solution would be to recursively visit each directory and in case its case is wrong - rename. You can have that in java, I would rather be happy to have that in script, however I am not sure whether scripting cmd or bash port on windows will actually handle that rename sanely.
Fortunately there is nice tool in python that accomplishes the same goal - os.walk. Usage is trivial and script is portable to any platform. File system paths can be also abstracted easily with os.path.join. There is even a method in python string that checks case of whole content - so you will not write any extra loop iterating over characters. Python handles nicely filtering in list comprehensions - so it is possible to use the condition in for statement.
from os import walk, rename
from os.path import join

for (dirpath, dirnames, filenames) in walk( join('src', 'main', 'java') ):
  for d in [f for f in dirnames if f.isupper()]:
       rename(join(dirpath, d), join(dirpath, d.lower()))
As you can see some APIs may be even more developer friendly than what is in Java standard. Now came the difficult part - for some reason git was unable to recognize character case change in filenames. It happens that some default on windows is aligned with os approach to case sensitivity. Making git see the difference is changing its configuration:
git config core.ignorecase false

Monday, March 21, 2016

Guava's Optional defficiencies

TL;DR: There is no reason to use Optional from Guava, Java 8 is better.

Guva used to be a most handy toolbox for modern (post-Java 5) projects. It was able to leverage new features and quickly became even more popular than old, good apache-commons (in my opinion some commons classes are still indispensible). Lot of ideas introduced by googlers behind Guava were incorporated into  official Java releases (mostly 8, however in 7 you may find some pieces of API visibly inspired by spirit of Guava helpers).
One of most popular innovations was Guava's approach to bringing Scala's Option[T] into world of Java. I assume idea is known and I hope also readers would agree with me that derogating `null` values would be a benefit for each developer as well for end users. After very enthusiastic reception, some critical voices could have been heard. Namingly Guava's Optional was missing important feature to call it a monad. That was an offence to real functional programmers.

Copernicus agreed with Guava guys - no Flat Map!

Is that problem really that severe? Well, Optional is by now integrated very well with other nice Guava's classes. This is still tempting to use it along with eg. FluentIterable, which has brought Stream-alike API into pre-Java 8 development. It can greatly be used to address deficiency mentioned above. Consider following code:
public static <In, Out> Optional<Out> flatMap(Optional<In> in, Function<In, Out> function) {
  Function<Out, Iterable<Out>> asIterable = asIterable();
  return FluentIterable.from(in.asSet()).transform(function).transformAndConcat(asIterable).first();

private static <In> Function<In, Iterable<In>> asIterable() {
  return new Function<In, Iterable<In>>() {
    @Override public Iterable<In> apply(In input) {
       return Optional.fromNullable(input).asSet();

Using the function above we managed to implement quite succint version of `flatMap` for Optional. We could now conclude, everything's fine, we have our monad now, and everyone's happy.
There is more problematic issue with this implementation.
The issue that degrades Optional just to simple null pointer warning, built into static code inspection, similarily to what would we gained by declaring checked exception. It however is useless as a flow control tool. Something that we are used to when using Scala's Option, and also much better implemented in java.util.Optional. The very problem is that our `map` function (known here as `transform`) is not really working. Let's verify what would happen when passed an null value to a transformation.

public class BrokenGuava {
    public static void main(String[] args) {
        Optional<String> optional = Optional.fromNullable("aaa");
        System.out.println("optional = " + optional);

        Optional<String> transformed = optional.transform(x -> null);
        System.out.println("transformed = " + transformed);

which brings us the result of:

optional = Optional.of(aaa)
Exception in thread "main" java.lang.NullPointerException: the Function passed to Optional.transform() must not return null.
 at options.guava.BrokenGuava.main(
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at java.lang.reflect.Method.invoke(
 at com.intellij.rt.execution.application.AppMain.main(

NullPointerException! The same one that we expected to get eradicated by using Optionals all around our code!
The standard has got it implemented in a much better way:

import java.util.Optional;

public class NotSoBrokenUtil {
    public static void main(String[] args) {
        Optional<String> optional = Optional.of("aaa");
        System.out.println("optional = " + optional);

        Optional<String> transformed = -> null);
        System.out.println("mapped = " + transformed);

this time you can see:
optional = Optional[aaa]
mapped = Optional.empty

This time it worked much better. Maybe not perfect (as we have empty as result - this is good topic for some next post), but at least not exploded in runtime.
So... Price is the same, why whould I choose suboptimal solution? Time to mark Guava's Optional deprecated.

Wednesday, January 27, 2016

Idea: Ctrl+w considered harmful

After some years of using Eclipse as my IDE I have found myself being quite efficient in code production using that environment. My typical flow was like:
- code
- select
- extract variable/method
For selection of expressions I have been using Shift+Alt+UpArrow. It selects expression under a caret position, and each repetition will cause expanding selection to next lexical unit. Very useful.  For some time I could not find it in any other editor or IDE.
I used to be a bit affraid to loose my typing efficiency when some colleagues suggested me trying IntelliJ Idea. So I refused. Learning keymap of new IDE actually is a time consuming process and you may not feel instant gratification. And Idea's kyes seemed so weird to Eclipse guys. Also IDEA's supporters arguing that F6 changes name of file in Total Commander were... not really helpful.
I remember my first ride with IDEA during a code randori session. I switched to 'Eclipse keymap' just to edit code. It should just work like in Eclipse, right? So let's start with running some tests. Ctrl+x t. So easy. Wait! It deleted something. Damn! Code no longer compiles and public is getting bored. Not a nice first impression.
Well, today really lot of companies are using Idea, so if you want to smoothly fit into team's coding manner, probably you will end up using IntelliJ. It makes also sense to learn basic commands and shortcuts available out of the box, as non-default keymaps are not that well documented (refcards etc) and as I mentioned you will not feel like in Eclipse anymore.
And here comes the surprise: Idea also has my favourite word selection feature built in. It's named 'expand selection'. A nice surprise. The bad part is how this shortcut is located. Namely Ctrl+w. Sounds like a crime, doesn't it? This very shortcut is something that was closing current editor tab at your previous IDE is now giving you a selection. Try using both IDEs (which I think initially everyone does when switching). What you have is getting some words selected when trying to close, and (while getting back to Eclipse) situation is even worse: you close your current tab when trying to select.
 You can think about the shortcut policy in Idea as a 'vendor lock in'. You just cannot switch to any other IDE after getting used to IDEA's native keymap. I believe at IntelliJ they call it feature.
But situation is even worse: this is not only about other IDE.
Every other program you use, and it supports tab view, probably uses Ctrl+w to close current tab. Last week when trying to select few words in Chrome browser I accidentally closed window I was working with. Such things are really disturbing and cause programmer to leave their 'zone' immediatelly. Too much for me.
But nice thing about IDEA is that you are able to turn the shortcut off. Today I visited Settings | Keymap, found 'Expand selection' and remapped to old, good shortcut from Eclipse. I should just have done it when starting with IDEA two years ago.

Wednesday, November 11, 2015

I like lombok

 public final class UserId {
     private final String userId;

    public UserId(String userId) {
        this.userId = userId;

    public String getUserId() {
        return userId;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserId userId1 = (UserId) o;
        return !(userId != null ? !userId.equals(userId1.userId) : userId1.userId != null);

    public int hashCode() {
        return userId != null ? userId.hashCode() : 0;

    public String toString() {
        return "UserId{" + userId + '}';
import lombok.Data;

 public final class UserId {
     private final String userId;
feels almost like scala...

Thursday, June 4, 2015

using ed

Having a moment of spare time I decided to go back to my approach to 99 scala problems. When I started I have used pure scala scripts, so I needed eg. to call them like this:
scala -i p05.scala -i p04.scala -i p03.scala p06.scala
Now I've learnt that it may be easier to go on using
sbt console
However my code was not sbt-friendly, as it caused complaints regarding missing object or class definition. You cannot just define methods like in REPL. So I ended up with urgent neccessity to include my functions into some nice objects. As I am currently playing with rather small machine full IDE like IntelliJ is not an option. So basically I needed to use my command line to:
  • add object definition line before content
  • add closing brace after content
  • where the latter is trivial append to file, usually
    echo "}" >> P01.scala
    How to append a line of text at beggining of file? Enter ed.
    Ed is an editor with tradition, used to scarry out younger programmers in team. I have read a fantastic intro to ed not so long ago. This is the ed script for making the changes with line by line explanation what it is doing (note ed will not get this comment):
    1                                    //goto first line 
    i                                    //start inserting
    package konopski.skala.ninety.nine { //actual text
      object PP00 {                      //more text
    .                                    //single dote
                                         //ends writing
    $                                    //goto last line
    a                                    //append 
      }                                  //some  
    }                                    //text
    .                                    //ok, that's enough
    w                                    //wq seems familiar
    q                                    //have you left any program this way?
    After that it took one line in shell I use to complete the task:
    for p in p*.scala; cat edme | sed -e s/PP00/$p -e s/\.scala// | ed $p ; end
    Note I have used ed's famous cousin - sed to replace placeholder value PP00 to actual file name without extension. That's it, the massive work is done and I have not used copy/paste or even any vim macro. I must say it was amusing equally to playing with scala, however seems to be even more practical than next way to reverse a list ;>

    Wednesday, April 15, 2015

    jq one more time - see only what really matters

    Sometimes you can find lot of information noise. Say I want to see only document id and it's content. Check this:
     curl -XPOST "http://es1.grey:9200/settings/d/_search?limit=50" -d'
                             {"query": { "match_all": {}}}' | jq ".hits.hits[] | [ ._id,  ._source ]"

    Friday, January 23, 2015

    new favourite command

    A must have for console addicted git users:
    git checkout -
    This jumps back and forward between two last branches. Really awesome.