Showing posts with label Guava. Show all posts
Showing posts with label Guava. Show all posts

Sunday, 27 October 2013

Optional in guava

Optional is a way of replacing a nullable T reference with a non-null value. An Optional may either contain a non-null T reference (in which case we say the reference is "present"), or it may contain nothing (in which case we say the reference is "absent"). It is never said to "contain null."

Same concept Optional class is going to be added to the core java.util library in jdk8

More detalails here: https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained

Tuesday, 8 January 2013

Ease of creating immutable collection woth Guava

In case of need od creating immutable collections in java, that are nice created, safe and efficient look for guava implementations
Read examples here: http://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained

Tuesday, 27 November 2012

RateLimiter - limit amount of actions per seconds

Next useful utility from Guava library

A rate limiter. Conceptually, a rate limiter distributes permits at a configurable rate. Each acquire() blocks if necessary until a permit is available, and then takes it. Once acquired, permits need not be released.

Rate limiters are often used to restrict the rate at which some physical or logical resource is accessed. This is in contrast to Semaphore which restricts the number of concurrent accesses instead of the rate.

You can read more in guava javadoc:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/RateLimiter.html

Sunday, 4 November 2012

Guava light cache

If you have a need to declare a small cache which have to be cleared periodically you can easily do it using Guava Cache.
We have to define loader that will load cache element and a period after which it will throw an item away. And then next read will use a loader to load an element into cache again. We can also define a cache in the way it will throw away an item if it was not read for a period of time.
Here is an example:
private LoadingCache<String, MyClass> cache;
CacheLoader<String, MyClass> loader = new CacheLoader<String, MyClass> () {
      public MyClass load(String key) throws Exception {
        return ourService.get(key);
      }
    };
cache = CacheBuilder.newBuilder().
        expireAfterWrite(1, TimeUnit.MINUTES).
        build(loader);

More about this subject here: http://code.google.com/p/guava-libraries/wiki/CachesExplained

Tuesday, 17 January 2012

Guava library

Some useful classes and methods from Guava library:
  • Charset.UTF_8, Charsets.UTF_16, ... - helpers for creating Charset class
  • Defaults.defaultValue(Class) - default value of given class
  • ImmutableSet, ImmutableList, ImmutableMap - builders for collections types f.e. List list = ImmutableList.of("1","2");
  • Joiner - for concatenating Strings
  • Strings.emptyToNull, Strings.nullToEmpty - String converters
  • Objects.equal(val1,val2) - returns if objects are equal
  • Objects.hashCode(...) - generates hashCode from list of objects
  • Objects.toStringHelper - utility for creating toString methods
  • Preconditions - methods for checking conditions on objects, if not meet then an Exception is thrown
  • Files - utility for file operations. copy (coping files), equals (files content is equal), getCheckSum (file check sum), getFileExtension, move (moving file in the file system), newReader (creates reader object on the file), newWriter (creates writer object on the file), readLines (returns list of strings), toBytesArray (array of bytes for file), toString (whole file as string), touch (touch on file in filesystem)
  • Closeables.closeQuietly - close stream without throwing IOException
  • Flushables.flushQuietly - flush without throwing IOException
  • BiMapsm - bidirectional map
  • MultiMaps, MultiLists - many implementations of multimaps multilists
  • Hashing, HashFunction - utilities for creating hash functions, usage of md5 calculation
  • IntMath, LogMath, DoubleMath, BigIntegerMath - operations tested for overflow, using hardware details for efficiency
  • CacheBuilder - for easy creating caches that expire after given time, amount, with other parameters
  • Cache.stats - for getting statistics of the cache object

More details on Guava library home page: http://code.google.com/p/guava-libraries/