-31-
12
2009
12
2009
Mehrere Werte in Java-Methoden typsicher zurückgeben
Trackbacks
Trackback für spezifische URI dieses Eintrags
Keine Trackbacks
private Object[] doItUnchecked()
{
String s = "foo";
Date d = new Date();
return new Object[] { s, d };
}
// unchecked variant: dangerous! Object[] returnValues = t.doItUnchecked(); String s = (String) returnValues[0]; Date d = (Date) returnValues[1];
/**
* Immutable holder type for two values.
*
* @author Benjamin Erb
*
* @param <F> type of first value
* @param <S> type of second value
*/
public class PairHolder<F, S>
{
private final F first;
private final S second;
public PairHolder(F first, S second)
{
this.first = first;
this.second = second;
}
public F getFirst()
{
return first;
}
public S getSecond()
{
return second;
}
}
private PairHolder<String, Date> doItChecked()
{
String s = "foo";
Date d = new Date();
return new PairHolder<String, Date>(s, d);
}
// check variant: safe already at compile-time PairHolder<String, Date> h = t.doItChecked(); String s = h.getFirst(); Date d = h.getSecond();
© 2002 - 2009 Benjamin Erb