Posts Tagged ‘java’

[Google Guava] Guava goodness – loading a file from a directory hierarchy

Posted in java on March 25th, 2013 by Wojciech Soczy艅ski – 2 Comments

The case:

  1. we want to have an ability to override some static resources in the application
  2. we can have multiple overrides
  3. there is a hierarchy which decides which file is selected if we have multiple overrides
  4. the hierarchy is fixed from the most important to the less important directories

The code:

Could not embed GitHub Gist 5236723: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

The other code 馃槈 :

Could not embed GitHub Gist 5236774: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

Joy :)

[Google Guava] – One class to rule’em all – FluentIterable

Posted in java, programowanie on March 16th, 2013 by Wojciech Soczy艅ski – Be the first to comment

Google Guava has a lot of interesting utility classes, just to name a few: Optional, Joiner, Splitter, EventBus, Collections2 etc. But the essence of the whole library is condensed in just one single class – FluentIterable.

The FluentIterable class let’s you combine a set of operations on a collection in a single pipeline, plus it has some nifty methods too :).

To start a pipeline, you should use the simple FluentIterable.from(collection) method:

Could not embed GitHub Gist 5177971: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

Then you can perform typical collection operations. For the purpose of the example, let assume that we want to square all even numbers from 1 to 100 and select from the result the first number that is greater than 17 and smaller than 50.

Could not embed GitHub Gist 5178106: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

Note that for a range comparison I have used the Guava Range class which is a cool utility for dealing with ranges. One can also replace the “generateInput” method with a combination of a ContiguousSet and Range invocations:

Could not embed GitHub Gist 5178252: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

The last but not the least interesting method in the FluentIterable is the transformAndConcat method. It allows to perform a transformation of a collection of some objects and merge the outputs, assuming that the result of a single item transformation was also a collection. This is very handy for situations like making a flat structure from some kind of Tree or a nested List. A common example is a list of files in a tree structure of the file system.

Could not embed GitHub Gist 5178381: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

Note in the above example, that I have used an “identity function”. The identity function is a function that simply returns its input.

We are near the end, so for the close up I would like to tell a few words about performance. I have done some tests using the Google Caliper benchmarking library. The case was about squaring every item of a collection which was an even number. I have made two implementations:

…a vanilla Java one:

Could not embed GitHub Gist 5178414: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

… and a Guava based one:

Could not embed GitHub Gist 5178440: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

The test:

Could not embed GitHub Gist 5178445: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

The test was run 10 times (10 reps) for 20 trials. On the average the Guava solution took about 1,5x time of the Java one.

All in all I think that the performance penalty is not very big and it is beneficial to use the FluentIterable class for the sake of coding pleasure, readability and testability.

 

 

 

 

Between Java and PHP – Arrays

Posted in java, php, programowanie on December 21st, 2012 by Wojciech Soczy艅ski – Be the first to comment

Most PHP developers, that start learning Java face the same shocking issue – Java does not have arrays ! Well it is not completely true, Java has a notion of arrays, but not it the sense that most PHP developers are familiar to.

In PHP, the array is an universal container. It can be used as a map:

Could not embed GitHub Gist 2365217: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

and as a list:

Could not embed GitHub Gist 2365239: API rate limit exceeded for 213.251.182.110. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

In PHP arrays are used everywhere, because they are very convenient. There are plenty of聽awesome聽functions that operate on arrays, to name just a few:

  • array_map
  • reduce
  • sorting functions
  • array_filter
The last cool feature of a PHP array is that, it聽remembers聽the order in which the elements where added.
In Java, things are slightly different. There are several collection types:
  • array – not actually a collection in Java sense, it has a fixed length (similar to SplFixedArray)
  • Map – a typed hash map (similar to PHP’s array, when we use string’s as keys), similar to SPLObjectStorage
  • List – a typed list – a container which remembers the order in which the items was added, similar to SPLDoublyLinkedList
  • Set – a typed collection of unique items

Every collection type is iterable (which means that you can use the for-each loop to iterate over it). All collection types derive from the base abstract Collection class.

The types that I have mentioned above are just interfaces, every one of it has different implementations which have some slightly different properties:

  • Map – implemented by HashMap and LinkedHashMap, the LinkedHashMap retains the insertion order
  • List – implemented by ArrayList and LinkedList, both implementations differ mainly in the speed of the operations – the ArrayList is faster for reading and the LinkedList is faster for writing
  • Set – implemented by HashSet, TreeSet and LinkedHashSet – the LinkedHashSet retains the insertion order, the TreeSet is sorted and the HashSet is the fastests

To even further complicate the notion of collections in Java, every collection type has it’s immutable form :).

To sum it all up, transitioning from PHP to Java in case of Arrays is not that difficult, you must just resolve which collection type you should use in each case. I will show some examples in the next post of this series, so stay tuned.

The Optional type in Google Guava

Posted in java, programowanie on November 6th, 2012 by Wojciech Soczy艅ski – Be the first to comment

Hi folks ! There was some time ago when I was actively blogging (half of the year or so), so I felt a bit ashamed and decided to write about something interesting. As you probably know I’m working now as a Java developer. Because I’m not a big fan of Java (actually I do hate it) I look from time to time for solutions to over-go Java’s shortcomings.

read more »

Wiedz, 偶e co艣 si臋 dzieje…

Posted in architektura, java, programowanie on March 21st, 2012 by Wojciech Soczy艅ski – 6 Comments

Ostatnimi czasy, mam okazj臋 tworzy膰 do艣膰 du偶e ilo艣ci test贸w jednostkowych. Zwykle tworze je do cudzego kodu, co pozwala mi w pewnym sensie oceni膰 jego jako艣膰. Zapytacie mo偶e, jak proces pisania test贸w jednostkowych do istniej膮cego kodu pozwala oceni膰 jego jako艣膰 ?

Ot贸偶, pewna stara programistyczna fama g艂osi, 偶e gdy kod jest 艂atwo testowalny, to prawdopodobnie jego jako艣膰, a przynajmniej architektura b臋dzie wysokiej jako艣ci. Automatycznie na my艣l przychodzi takie proste rozwi膮zanie, 偶e skoro kod ma by膰 艂atwo testowalny, to najlepiej by by艂o zacz膮膰 jego tworzenie od napisania test贸w. Takie podej艣cie nazywa si臋 TDD (Test Driven Development). Pewnie wielu z Was s艂ysza艂o o takim podej艣ciu, natomiast je偶eli pracujecie w typowej firmie wytwarzaj膮cej “stronki”, to pewnie nie mieli艣cie wielu okazji by zastosowa膰 takie podej艣cie.

Oczywi艣cie nie koniecznie jest to grzech – w prostych projektach zastosowanie TDD mo偶e by膰 dyskusyjne ze wzgl臋du na narzut czasowy jaki generuje. Znaj膮c jednak 偶ycie, prosty projekt zwykle przeradza si臋 w projekt skomplikowany, kt贸rego skali nikt nie przewidzia艂.

Abstrahuj膮c jednak od TDD, chcia艂bym si臋 podzieli膰 z Wami, moimi spostrze偶eniami na temat typowych “wzorc贸w” w kodzie, kt贸re znacz膮co obni偶aj膮 jego testowalno艣膰. Przy okazji zaproponuje sposoby ich rozwi膮zania poprzez refaktoring kodu oraz om贸wi臋 konsekwencje jakie nios膮 za sob膮 poszczeg贸lne antypatterny.

Tak wi臋c, je偶eli spotkasz jeden z podanych poni偶ej przypadk贸w – wiedz, 偶e co艣 si臋 dzieje 馃槈

  1. Testuj膮c dany obiekt sprawdzam jego stan po wywo艂aniu danej metody:
  2. Testuj膮c dany obiekt tworzymy mocki obiekt贸w od kt贸rych jest zale偶ny w spos贸b 艂a艅cuchowy
  3. Testuj膮c dany obiekt 艂apiemy si臋 na tym, 偶e testuj膮c metod臋 publiczn膮 w istocie chcieliby艣my przetestowa膰 metody prywatne z kt贸rych ona korzysta