Guava FluentIterable - Get only first object or null from list

by GarciaPL on Tuesday 23 February 2016

A few days ago I had issue, that I need to get first object from some list which meet my requirements. Of course I can create some loop, write some if statement. If I will find interesting me object, then I am gonna return it. If I will not find any useful in whole list, then I am will return null.

Above solution is done in quite old-style approach. Now it is time for functional programming! I found very useful a library called Guava and it's utility class called FluentIterable. It supports you to manipulate Iterable instances in a chained fashion. This class has a lot of functionalities, but I would like to focus only on one case - get first interesting object to my pattern or just return null.

MyClass interestingObjectOrNull = FluentIterable.from(myList)
    .firstMatch(new Predicate<MyClass>() {
      @Override
      public boolean apply(MyClass element) {
        return element.getId == 125L
      }
    })
    .orNull();


And that's it! Simple, isn't ? All you need to remember to check returned object if it is null or not. Of course you might write as much complex apply pattern as you like.
Reference :
[1] Stackoverflow - Get only element of a collection view with Guava, without exception when there are multiple elements
[2] Pastebin - Source code

GPG error: http://downloads.hipchat.com Public key is not available

by GarciaPL on Saturday 6 February 2016

Recently I had an issue during installation of HipChat from Atlassian on my Ubuntu. I mean that installation went well, but I had issue with GPG keys which was :

"GPG error: http://downloads.hipchat.com stable InRelease: The following signatures couldn't be verified because the public key is not available"

So, I managed to find that WebUpd8Team provides tool called y-ppa-manager. All you have to do is install y-ppa-manager like below :

sudo add-apt-repository ppa:webupd8team/y-ppa-manager
sudo apt-get update
sudo apt-get install y-ppa-manager

and then launch this tool. Select "Advanced" and then "Try to import all missing GPG keys". This operation might take a one or two minutes, so be calm and wait for notification. After that you can run sudo apt-get update to refresh repositories.



Reference : [1] Y PPA Manager - WebUpd8Team