Google Guava, Mite Mitreski

Just another WordPress site

Google Guava, Mite Mitreski

Oh okay my welcome to my session my name is Smith emit risky I come from Macedonia I’m the jock leader of joeys a group Macedonia for the past six years I work for this little Dutch consultancy company in in Netherlands and also I try to contribute to various open source project lately I’m part of the expert group for GSR 3 7 for the JSON processing API today I wanted to share something that I use quite often in almost every project that I work for and that’s something called cool guava the title of the talk is the core libraries you always wanted it’s something I believe it’s quite essential for for most of the enterprise projects that we have today okay so what’s going so it’s a plant it’s not really common in our area a lot of vitamin C but of course I won’t be talking about the plant are we talking about Google guala Google always set of various utilities we have probably written most of this in your projects but these are actually something that comes as a library as you can notice most of these are quite similar to the ones we can find in the in the JDK as you can see the vk5 notation we have some of the base classes we have utilities for input/output and so on there were quite a lot of code in the slides I’m always like the demo night nightmare so I didn’t want to risk it as it turns out the power is not working at the moment but yeah we’ll see how long the battery lasts yeah so really few facts about Guam the latest released is a teen dot oh it’s in August 2014 the product is quite alive so there is another Lisp and this year if you open up the github propose three of the projects you can see that they’re committed from this past week so it’s quite a live project it’s something that Google has provided to the public combination of sort of internal utilities they they used and they made it public it’s a party to license so it’s quite permissive to use in pretty much any project that you want to use the presentation that I’m going to be talking about certain aspects of the guava library that I find it useful so it’s not going to be every possible detail because there is a lot of it and we will see some of the problems this library helps us with one of the problems in Java is not really if you see some code from Doug Leier dakahliya for those of you who do not know is one of the guys behind most of the concurrency concurrency classes in in the JDK he’s also professor in one of the US universities it’s really direct quote from him it’s like no sucks really it does suck if you also see the first person who actually introduced null in any of the languages that’s Toni Hara he’s the same guy that has invented quicksort so he says the test is billion dollar mistake what really is the problem with no the main problem for me actually for now is it that it’s not clear what what now means section if you take a look at boolean for example we have a boolean variable it’s awesome it can be now true and false so what now is it false is it really not through is it not false is it it’s not really clear just by looking at the variable you do not know what what the null value means for for for boolean it doesn’t make any sense we have all actually written a code like the second part we have all written code where we say I don’t know if X is not now and X dot some method is not now then you can actually call X dot some metal that’s one of the ways to to take au turn ulness we always check the other

way is to do this like defensively like called defensively and at the beginning of the method we can say like assert not now assert not now this and so on and throw certain exception or check the state that’s also okay but it can happen that we have already checked in a previous method that this value is not null and now we are checking that again and again and again and did you can see this throughout any any code base of any any major application um one way to tackle this is something called the optional clause the optional class is kind of like wrapper around a value at Inc so it must be something like an object at least for now so an optional gives the intention that my method is returning something that may not be there so it’s quite clear actually it’s something that may may be absent it’s not no it’s something that may be absent it might you may need to check the value of this this might be absent it gives it quite clear to the user of our method that the thing that we are returning must be checked for Nona’s so the one way to structure your application is to say everything is not now there is no null in our code except we can return optional order where we actually need to return an absent value in that way we don’t need to have this defensive coding in each of the methods one sort of bad practice is to have optionals in in meta parameters so if you have optional in meta parameters then you’re sort of like masking the problem once again so someone may actually accidentally person all right so that that is a problem basically before getting any um value we must check either its present so if we are using dog get on an optional we must check his present but we shouldn’t really use dot get that often we should use some possible value or something else or something else this is one way to sort of fix the numbness guala has lot of other utilities that that help round gnomes we have all written code like this like the first example where we actually go through through a method and say like if this method is different than now then call the method and otherwise return some default value this is quite simply done in in guava like objects first not no return the first value that’s not no it’s quite simple actually we say if objects first not now of null and three it will be evaluated three first not not of nine and three it will be nine it’s a subtle change but it’s lot clearer than doing an if statement or I don’t know tannery operator in java there are all of these small utilities in go we also have the precondition class if we do still decide to use the defensive according also we saw for some sometimes actually quite often we need we need to check the some impossible state like say in our method if this method is content with this state then don’t even evaluate it so they we can have this check not null check state methods that are available in the preconditions library these are quite similar to them to the Apache Commons a library that that that’s quite used in the quite older project let’s say even though the Apache comments are still actively developed as well another option to take hold on null nests in our application is to use the GSR qo5 this this provides annotations for software fault that at the moment this at the moment actually in the past three or four years this is not an active GSR even though the annotations are quite simpler and widely used they can be found as the standard Java ex-jocks a notation notnot that are the ones from the GSR but there are also various other forms like the one from IntelliJ we have also does the ones that are used in hibernate for validation of numbness and so on for building validation there are also notations from fine bucks but then essentially they do the same thing they provide us a mechanism to test the numbness so we can test the nihilist at compile time using some annotation processor so fine box has

this it provides automated testing of No so it can say yeah okay in this area you may have no value these are actually the GSR 305 is the only dependency on Gua the DRA has this internally so for for method parameters they use the annotations they use now nullable and not now but for return types it’s always an optional if we actually have an optional realistically we don’t always need to have an optional values we don’t always have to to rely on something that that may or may not be absent we can always kind of return something it doesn’t have to be something that’s absent but this is also another another way to tackle problems actually most of the ideas at the moment have automated checks for this Eclipse IntelliJ and edwin’s here have automated checks that pick up these annotations on some of the ideas you might need to configure this and install additional plugins but still there is something that automatically checks the notice of certain areas so this will allow you to have less null pointer exceptions but wait like JDK of 8 also has optional that’s true but go I actually supports JDK 6 all right supports also additionally GWT and also supports Android so best of my knowledge I’m not a net Android guy but best of my knowledge for for for Android there is still no JDK 8 right so at the moment we are stuck on older versions so if you want to have certain functionalities like this you still need to use something like this um really guava evolves as the language evolves as well so in the later versions where they predicted that this thing is gonna be added in JDK 8 they created a class called more object and they sort of put a label on the old objects class like don’t use this one use the one from the JDK because this will be eventually removed when we choose not to support JDK 6 so basically whenever there is class in JDK actually JDK 8 mostly like use the JDK cause if the same concept completely the same concept actually the JDK crosses were inspired by guava if you were leading the mailing lists it’s it’s there actually it’s been there for quite some time the one thing I really like about guava is the API deprecation it’s something that allows them to sort of evolve the codebase one of the biggest problems with Java that everyone is complaining about nowadays is that there are tons of deprecated methods since 2000 even before 2000 from Java one one that are still not removed if we take a look at the date classes right I mean to create a new date most of the methods are deprecated because they’re quite really ambiguous and and have dual meanings and but there have been deprecated for 10 years 15 years actually even more and they’re still not removed go go a-team Kraft removes this type of stuff when they do a mistake they remove part of the code but this is like a problem if this is a utility library and your using it in every project then it means that you also must change everything that they have removed well they actually have warnings for this they are not just like removing random stuff um basically any part of the API edits annotated with better it’s subject to change at any time this has happened to me several times that I have used something that was annotated with meta and then I’m migrating to a new version of Java and then it’s either missing or has some completely different signature and that’s quite expected but then you you are aware that this might happen for the bit on a day to day be not not much of the codebase is done it is a bit better but still most of the newer functionalities are like that there is also the deprecated word so in Java is a lot of stuff are deprecated but there are removed error here there is a warning like 18 months that’s it after 18 months you had enough time to actually migrate if not this will be removed it’s not always removed exactly in 18 months but this is the wording they say 18 months it’s month we guarantee this will be the supported time period of the library which is kind of fair the other sort of big thing that that allows them to evolve the API is

that serialization is not completely backwards compatible brand gets was asked on devoxx this year what’s the biggest problem was the worst feature that he thinks of in Java and he said that that’s the solution oh why is that actually it technically restricts them to evolve the API they cannot do certain changes on the API just because of the serialization because if they do certain change the older version may not be compatible with the newer version of the same class so they actually cannot do lot of changes in the code base just because of this and it is the most hated feature there and go ah basically says we have no guarantee they usually give out warnings for certain classes that you should not use them in serialization or you should write your own custom way of doing this but yeah it’s kind of fair enough for at least four for my use it’s fair enough that that civilization is not not guarantee one other sort of thing that that can be quite difficult to write it when we weigh a writing comparator actually the writing of the comparator it’s quite simple right enjoy you write the comparator I don’t know if it’s zero then there are equal if one if you determine positive value then it’s like bigger if it’s negative then it’s smaller but yeah you’re gonna write it down then someone else comes in he needs to read that you’re returning some number like it’s really it can be really difficult to sometimes to understand what the comparator does God provides something called or durables which which kind of provide the same functionality but it’s a lot more clear in this example we have ordering of customers so it’s natural ordering so it means that it’s all fab ethical the nose will be moved on the top because in your comparator you may not have covered the scenario with the notes right usually that’s one of the mistakes when writing comparator we do not cover the scenario what happens if both of the objects are no do we put them on the top we put them on the bottom and the sorting and result of certain function and at the moment we are really comparing by name right we are already linked by name because name is a string you know some kind of customer object and yeah name it has some kind of natural order really quite simple and obvious whoever comes and reads this it’s quite obvious what it does the ring there aren’t any hidden mechanisms for the nulls or or so on if someone wants to change certain functionality like reorder reorder them and reverse them it’s quite simple actually it’s really straightforward there also tons of utilities regarding the wrapper clusters so I mean with the wrapper clusters in Java we are pretty much covered with most of the functionality that we need unfortunately in Java there isn’t any support for unsigned nom in most of a business application nowadays really we don’t need unsigned numbers right but if you’re doing something that connects to a certain device or connects to some kind of hardware mechanism or either know some driver usually you get stuff that are written in C and then you need to provide some kind of mechanism to use this unsigned numbers there there is utilities for that they pick the number in larger numbers so if it’s an integer they pack it in wrong but then then they have these additional operations that provide you the functionality of having unsigned numbers in Java analog to the wrapper classes there are other utility classes so if it’s integer it sends if it’s I don’t know short it’s short it’s always with an S on the end all of the additional utility classes in guava are always like with additional s so you can hear you can always search for something that you have in the JDK and you’re missing one method in the JDK just try add an S see if it’s in grow it’s most likely there tons of other stuff that are quite useful or carrot the character measures we really need to always always ladies often to check when we were doing some validation and so on that I don’t know the number that we have input that is valid to ASCII or remove all the Unicode characters or to check if it contains just digits and so on what we usually do is use the regs right but when you read reg X it’s really difficult to understand at the moment for me at least when I when I see I know /w I always wonder was it the bigger what w was a white character no

no phone Merrick was it the other one right always gets kind of hard really you need to always look up I always look up whenever I’m reading a bigger array X I always look up what it does you don’t need to do that you have these values of built-in character metrics you can actually write your own character measures here this building ones that cover major portion of the functionality and they’re quite composable you can have negated negated value of what the character Melcher matches and it’s quite straightforward when you’re reading it to notice what it actually does Oh for example I don’t know if you want to remove the digits from a string like character major digit it will match whatever digits are in the string and just remove the character make sure digit retrain form retain from that string oh I don’t know put it to lowercase to remove just the lowercase remove just the uppercase make it I don’t know it’s so compatible make it UTF compatible all of these functionalities are built-in in these kind of measures you don’t have to bother yourself with it another big thing actually that we usually need is in most application is caching now what guava provides it’s something called cache builders that have different types of caches the most common ones are something called loading caches now just to be clear these are not distributed caches these are just your local application caches if you need distributed caches and take a look at hazel caster’s tons of other products out there ok so let’s get back on the cache it’s quite simple to build a cache in inside inside using go we have cache builder we’d say like maximum size 10,000 just build it we have the cash inside there ok but that’s like the big most basic thing with a cache I can use some map for that right I don’t really need this there are tons of other stuff that we can add during the building of the cache one of the things that we are usually concerns ourselves with is the eviction right when when the objects will get removed from the cache we have this utility sink all we have like expire after this much of period expired after I know two minutes expire after accessing when you access and then remove it so on you can add notifications for removal you can add listeners for for all of this there are tons of other stuff that you can do on top of the standard standard wrappers there is the option to add custom size dependent on a custom property from within the map so you can set like a waiter Witter is something that that gives a value of weight and then we actually must provide also something called maximum weight in this case it doesn’t really make sense but when we have a customer that has bigger bigger what’s in this case name then 20 characters then something will be removed from the cache because we already sort of maxed out depending on our custom function so I don’t know if we put 0 then it’s okay if we put something that has length customer the has name length to it more than 21 then something else will be removed from the cache and this new customer will be placed inside there this provides us a lot of flexibility to sort of fine tune our caching um one other cool feature of the guava cache is that we can enable Vicki’s so wiki’s means that there will be weak references within the internal presentation this means that if we put something inside the cache and the garbage collector needs to remove it it will remove it why is it this useful this is useful for not for us not to create memory leaks let’s say you are in a web application your caching requests for some reason you want to catch them as a key right put them in the in the key for some reason if you if you still contain the reference to the key that’s in the cache even though nobody else uses this reference the garbage collector will not remove it because your cache is holding a reference to an object that nobody else uses but even though it’s not really needed so actually you need to be quite careful that you know what actually this does but it’s actually quite simple if you if you need value or our key actually there is also a weak values if you need a value or Viki that that will not be used by anyone else and you want the garbage

collector to remove it use this garbage collector will automatically remove this from your own cache okay so we build this cache now we not wanna know actually if it does any good to us right we want to know if if we are actually getting some some cache hits or we are just like caching new stuff and removing all stuff if someone is actually if any of the stuff are used now there is something called cache statistics this is usually just for debugging purposes or maybe some kind of analytics that you want to do the Builder provides something called record stat so when you are pressing record stat then on the cache you can call dot stats method this will give you cash statistics method there you have I don’t know he trade a number of hits number of misses and so on you can get a nice overview of how well your cache operates this is very very nice section now previously actually in the past I usually used map to the most of the stuff but for the most basic functionality yes map maybe sub session but then you get into concurrency issues they need to implement all this and reinvent actually something that’s available and quite well tested really you need to concern yourself about concurrency right map get is not really leaves a hole in between the operation when you get a map get then you do some processing that whole part is to be synchronized right also there is the problem where the map is rising the equals method is not really working as the way you usually think that it does work okay so that was about the cache there is this whole package called collect historically it originated from from something called Google collections but it was integrated within the bidding guava actually reduced version of CRO collections so it provides additional implementations of the standard collections API up there are no multi Maps immutable collections tons of other utilities related to collections there is also quite a lot functional programming support technically we do have that in in Java 8 but a lot of this is done actually for Java 6 even though it’s done for Java 6 of course it still works on Java 8 um quite often we have written like list that contains string sorry not released a map that contains key of string and a value of West right and then you get this huge generic blobs that that’s earlier completely understandable there are tons of it built in a multi maps so for example array list multi map that means that we actually have a hash map it is a hash map but inside the values are actually a realist the naming convention is quite similar hash multi map right hash map but it has a set inside a link hashed multi-man linker set and so on they’re quite analogues to the standard Java collections and provide a lot of more utilities because these structures are slightly more complex and you need sometimes to have bigger operation of this the one I quite often need actually it’s something called bean map it’s like bi-directional map it you cannot have duplicate values but you also cannot have duplicate keys and additionally it provides you a way to sort of invert it so the values to become keys and the keys to become values I don’t know for some reason I often need this guy the other way that it behaves that’s different than the other maps in that it throws illegal argument exception if you attempt to put a duplicate value of something that that’s because it’s not possible to put duplicate values because of the inverse inverse mechanism okay so there are quite a few functional aspects in in guava there are things called functions and predicates they kind of simulate to what the one does do today there is like a function that is applied on a string and an integer so the first argument is always the return argument so the first argument is the the the return value and

the second argument is actually the other way around sorry the first argument is the method argument and the second argument is the return value the predicates on the other hand are something that we apply only to one one type and it they return a boolean always return a boolean because they are predicates obviously in Java 8 this looks a lot simpler actually this this same code would look like this we would just use direct method references right it’s lot simpler this way and additionally we don’t even need to grow our classes there we can use the built in function and predicate clauses but of course that means that we are working on JDK 8 a lot of the functions if not all of the functions in guava are really something called single abstract method actually something that’s susceptible to be in Calamba so almost all of the methods and interfaces within guava are released more or less stamps they are there are single extract methods if you were to compare what what methods from guava ended up in in JDK you would we would see that we have the predicate we have the optional we have also the the joiner splitter classes the supplier and so on these are just a few of those actually but there are more or less the same I mean the packages are different but they are more or less the same they provide the same functionality on the other hand the GDK classes are quite better because they are integrated and well thought of to be work within the whole platform so they are there’s a lot more methods lot more functionalities surrounding those so just use the JDK it causes if they are available to you ok so quite often we have some collection and we want to remove certain values depending on some condition for for example we have this map and we want to remove from that map whatever values that whatever entries that have no values one way to do this is you use the maps you notice the s so it’s a map maps in int is the guava class maps is do have a class some Maps filter values we filter the values of that map depending on some condition so the predicate is not know so this will remove we will filter out all the no values at the end we have the three number three leftover values that are not not now there are tons of these filters that there are usually in the iterables iterators classes but they’re also in the sets in the maps and so on you have this filter that accepts there something that’s eatable and then you have the predicate quite simple actually for for all collections that are provided within guava and in the JDK you have this method additionally on top of that you have the transform methods but the transfer methods do not accept predicate but they accept a function because the function is something that can be applied to to certain value and then give you a different value so the resulting collection is different than the inputted collection again JDK 8 right we have streams the streams provide the same functionality of course the same functionality works on most of the collections and to some extent it works on the guava collections but as I said guava is built for JDK 6 to be JDK 6 compatible so now not everything is really well fitted in within that frame for example we have this roster right we can stream filter it out by filter out the mails there and for each of them print them out so these are kind of operations that we can also do it guava but when we have jeddak hate it makes no sense really just use the the GDK 8 operations or we also have like the aggregate operations again the same operations are available in guava you can still aggregate something but here it’s actually a lot nicer like we have the stream filter out we can map the age get an average age for each of this persons what cleaner then using the

guava libraries again you need JDK 8 there are lot of collection goodies I use this all the time actually unfortunately unlike some other languages Java doesn’t have certain literals for creating maps and also lists actually so there was a huge discussion regarding the the language design whether or not collection literals should be added in the language and the end discussion was like why bother it will kind of mess up the the whole compiler it would be to require a lot of work and the sick tax benefit it’s too small when you can use just utility methods and everyone was pointing at guava because we’d go ah you can create I don’t know a list quite easily from various of keys you can create a value using like key one you want key to value its release really really simple of course this doesn’t work because doesn’t work for a lot of keys and values because these are if you see in the background is like operator overloading with a nano 9r9 arguments ten arguments but it’s not more than ten I use this whole time it’s lot simpler than writing this huge blob of generic things which is right really too much of course the time on top Reiter in JDK 7 helps around this to certain extent but then you remove the dependency from from the hashmap and you’re using directly the map interface which is what nicer than then then using the specific specific implementation otherwise um otherwise in the background this is creation of the hashmap is exactly the same as if we have called new hash map ok there are tons of utilities for pretty much every common task that you want to do one of the common things that we want to do is load a file as a as a stream as a resource from the clasp at the proper way in Java to do this is to get this class to get the class loader of this class and to get the resource on that specific location that’s really the proper way because in in if the class was loaded by different class loader you might end up being in a in a different class pet that you would have expected otherwise so even though this this thing is there on the class pet it may not work this blobby syntax is just replaced by the resources get resource class what simpler what cleaner and not nice yet another common test that that we have during programming is cash ok MDP 5 it’s like quite common everyone uses it it moving also in the JDK so it’s not really quite big of a problem go operate something called the funnel so this here is a funnel so it’s the final funnel is a thing that converts your object into something that’s a primitive representation of that object so it converts it into something called primitive sink so into is something it’s an object of type primitives sink it basically gives you a value that value representation like a hash code of an object ah there is also like a hash code class that provides the same functionality but for objects you must use a funnel that that will kind of explain your object into a hash code why would we need this why isn’t like that standard hash code enough if we need to to have different hash codes depending on certain parameters for usually it’s for performance reasons then we need to provide the separate hash code representations for different objects let’s look at an example like this there is actually a class a probabilistic row ballistic data structure called the bloom filter so let’s suppose we have this friend list of customer we create this bloom filter using this customer funnel so customer phone is again the thing that converts our customer to uh number so to an amber oh this parameter is the expected number of entries and

this is the the probability of which we have certain expectancy we will get to that in a little bit so if we go to the friends list and put them inside the bloom filter then we have a populated bloom filter and on that populated bloom filter we can actually check if something might be contained into our collection so we say awesome customer might contain that strange guy so this customer here if that method is fulfilled that means that this strange guy is not really a customer and we need to it’s not really custom it’s not a customer and that we Knight need to test if he really is why would we actually use something like might contain performance reasons quite simple actually performance reasons if we have like a huge list of course four to two customers it doesn’t make sense but if we have like huge list of customer store we have had data that cannot fit into a memory it’s a lot simpler to actually check if something that that’s not there so the bloom filter will provide us with something with a value it that it’s not there quite fastly because it provides it provides it contains only the hash code it contains the hash code representation of each of the objects let’s see it in a diagram it it probably will build clearer there so we do a check we say do you have the key no then we are sure that this customer is not part of our customer base but if the bloom filter says yes then it’s there but sometimes for yes that also can be a false positive so that’s the probability per aspect of it so if the bloom filter says yes then we actually have to do the expensive operation we have to do the database call or the network call if you use this quite often for for Network calls and it provides like huge performance benefit you need a use case where where you need to test against something that’s huge and to check if something is not there it will provide you a huge performance benefit just because of the hash code but you really need to be careful writing the hash code that the the hash code representation the final representation of the object it’s really representative and unique for that object it’s a huge huge performance benefit okay so when to use go like collections utilities always for me for most of the business applications first thing I do is set up the logging the other part is letting go because most likely like the religion all chance that you’re not going to need some of this of course I don’t know if you’re doing some application that’s embedded or has some specific memory requirements or you really want to stay away from dependencies then you can actually they implement most of this like actually 90% of this it’s really easy to implement but it’s really nicer there because it’s well tested and so on which brings me to the point really why why you use go you can do it yourself right you can do most of the stuff yourself but will go ah and actually other utility libraries you have something that open-source other developers has already solved the issues that you have you have fixed and even if you fix an issue you are effecting a huge community unfortunately growl is a Google project so there are really handful of contributions from the outside most of the contributions are in in I don’t know the issue tracking system where there are certain discussions there so if you decide to contribute something it’s most likely not gonna end up being a code but it would be a complaining of something or finding a bug in some of the some of the libraries there is actually tons of stuff that I didn’t mention or there is a whole event bus I know publish/subscribe mechanism there are a whole bunch of input opportunities reflection Matt there is a representation of ranges that that I skipped entirely because really it’s too much to handle even like this it’s too much to handle but yeah they you need some kind of library like this and this is really nice layer on top of JDK that you you need in most of the projects okay and I believe we have about 20

seconds for questions okay so um just one thing um beginning beginning end of last year there was a small event that we do you know Java is a group and three of the the bulgarians Java is a group guys visit the TAS and he had a great presentation and I just want to say that it’s great that we have a joint upper joint oppression cooperation and I hope that we gonna do some event together there are already some discussion like that and if you’re not really members of the Bugatti and Java is a group like join it right away it’s great for the community it’s great for your career it’s also great for the companies that you work for it it builds up the culture within within the city and you get a lot better developers just by having this type of events okay that’s it thank you