Schlagwort-Archive: Guava

Guava 19.0-rc1 herausgekommen

Die Änderungen führt https://github.com/google/guava/wiki/Release19 auf.

Von meiner Seite bin ich nicht mehr so scharf auf Guava, ich komme mittlerweile seit Java 8 ganz gut ohne aus. Klar, könnte ich mehr Guava nutzen, doch ich kann auch ohne, auch wenn ich einmal eine Argument mehr an eine Methode setze.

Heiß her geht auch die Diskussion auf einen Java 8-Umstieg (https://groups.google.com/forum/#!topic/guava-discuss/fEdrMyNa8tA). Die Google Entwickler wissen nicht so recht, was der beste Weg ist, da man bemüht ist, mit einer neuen Java 8-Version keinen alten Code zu brechen.

Google Guava Closeables

To shorten things and not to repeat ourselves (usually the closing always looks the same) Google Commons offers the utility class com.google.common.io.Closeables. In this class you can find two static helper methods close(Closeable) and closeQuietly(Closeable). Both take an argument of type Closeable—like a FileInputStream—and call the close() method on this object eventually. If the argument is null, nothing happens; in our hand-coded version we use an extra if (out != null) to prevent a NullPointerException from out.close() if out is null.

The following example uses Closeable.closeQuietly(Closeable) to close a stream. Any potential IOExceptions caused by close() is swallowed by closeQuietly().

package com.tutego.googlecommon.io;

import java.io.*;

import java.util.logging.*;

import com.google.common.io.*;

public class CloseablesDemo {

private static final Logger log = Logger.getLogger( CloseablesDemo.class.getName() );

public static void main( String[] args ) {

InputStream in = null;

try {

in = Resources.getResource( CloseablesDemo.class, „test.txt“ ).openStream();

BufferedReader br = new BufferedReader( new InputStreamReader( in, „utf-8“ ) );

System.out.println( br.readLine() );

} catch ( IOException e ) {

log.log( Level.SEVERE, „IOException thrown while reading line“, e );

} finally {

Closeables.closeQuietly( in );

}

}

}

Using closeQuietly() shortens a program but it does not notify about any exception caused by the inner close() method. A lot of programmers ignore this exception and shortens there closing block to

try { out.close(); } catch ( Exception e ) {}

This style is dangerous, usually not for readers but for all modifying writers. If a writing stream can’t be closed the IOException shows a severe problem that probably the output is not complete and data is missing. For that reason the utility class Closeable offers a second message, close() which, in contrast to closeQuietly() first is denoted by an IOException clause and secondly controls with a boolean parameter if in case of an IOException caused by close() this exception should be swallowed or rethrown.

To summarize these methods:

class Closeables

static void close(Closeable closeable, boolean swallowIOException) throws IOException

Closes the closeable if it is not null. The boolean parameter controls whether an exception will be rethrown (pass false for swallowIOException) or swallowed (pass true).

static void closeQuietly Closeable closeable)

If closeable is not null this method calls closeable.close(). If close() throws an IOException this exception is swallowed by closeQuietly().Because closeQuietly(closeable) swallows the exception it is internally written as close(closeable, true).

Google Guava 10

Nach längerer Wartezeit gibt es nach Guava 0.9 die Version Guava 10 – die Version 1.0 wollten man vermeiden, das klingt so fertig (ja, Google mag Beta-Status). Es ist einiges im io-Paket rausgeflogen und dabei neue Sachen reingekommen, wie ein Caching und ein eigener EventBus, sodass jetzt auf den java.net EventBus jetzt verzichten kann. News siehe