Posts Tagged google

Google Guava – almost everything you need to know

Tags: , , , ,

Some time ago at  SoftwareMill during our weekly Friday meeting to share knowledge I’ve presented Google Guava API. And as it ended with quite long presentation and quite many lines of code I thought that this might be interesting for some Java Developers outside our company.

So below there are slides presenting most useful and interesting features of Google Guava (v.11). I didn’t cover every class from this library, there are no examples from cache and concurrent packages as I don’t find this part of Guava as a really, really useful for every Java Programmer. If you need caching or concurrency support, there are some interesting stuff for you in packages I mentioned earlier.

Slides are available here:

Source code is available on GitHub – https://github.com/tdziurko/Guava-Lessons and PDF can be downloaded from here.

If you have any feedback or suggestions, please share them in the comments. I am more than happy to learn something new.


Be Sociable, Share!

Google Guava EventBus – an easy and elegant way for your publisher – subscriber use cases

Tags: , ,

Google Guava in version number 10 introduced new package eventbus with a few very interesting classes to deal with listener (or publisher – subscriber) use case. Below I present my short introduction to EventBus class and its family.

Basics

To listen to some events we need a listener class. Such class created in google-guava-way doesn’t have to implement any particular interface or extend any specified class. It can be any class with just one required element: a method marked with @Subscribe annotation:

public class EventListener {

    public int lastMessage = 0;

    @Subscribe
    public void listen(OurTestEvent event) {
        lastMessage = event.getMessage();
    }

    public int getLastMessage() {
        return lastMessage;
    }
}

lastMessage property is used in tests below to check if events were received successfully.

And of course we need an event class to send it around:

public class OurTestEvent {

    private final int message;

    public OurTestEvent(int message) {
        this.message = message;
    }

    public int getMessage() {
        return message;
    }
}

How it works

The best way to show something in action is to write some tests, so let’s see how simple usage of EventBus looks like:

Continue reading this post …


Be Sociable, Share!