brooklyn.util.JavaGroovyEquivalents Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-utils-common Show documentation
Show all versions of brooklyn-utils-common Show documentation
Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else
package brooklyn.util;
import groovy.lang.Closure;
import groovy.lang.GString;
import groovy.time.TimeDuration;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brooklyn.util.time.Duration;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
public class JavaGroovyEquivalents {
private static final Logger log = LoggerFactory.getLogger(JavaGroovyEquivalents.class);
public static String join(Collection> collection, String separator) {
StringBuffer result = new StringBuffer();
Iterator> ci = collection.iterator();
if (ci.hasNext()) result.append(asNonnullString(ci.next()));
while (ci.hasNext()) {
result.append(separator);
result.append(asNonnullString(ci.next()));
}
return result.toString();
}
/** simple elvislike operators; uses groovy truth */
@SuppressWarnings("unchecked")
public static Collection elvis(Collection preferred, Collection> fallback) {
// TODO Would be nice to not cast, but this is groovy equivalent! Let's fix generics in stage 2
return groovyTruth(preferred) ? preferred : (Collection) fallback;
}
public static String elvis(String preferred, String fallback) {
return groovyTruth(preferred) ? preferred : fallback;
}
public static String elvisString(Object preferred, Object fallback) {
return elvis(asString(preferred), asString(fallback));
}
public static T elvis(T preferred, T fallback) {
return groovyTruth(preferred) ? preferred : fallback;
}
public static T elvis(Iterable> preferences) {
return elvis(Iterables.toArray(preferences, Object.class));
}
public static T elvis(Object... preferences) {
if (preferences.length == 0) throw new IllegalArgumentException("preferences must not be empty for elvis");
for (Object contender : preferences) {
if (groovyTruth(contender)) return (T) fix(contender);
}
return (T) fix(preferences[preferences.length-1]);
}
public static Object fix(Object o) {
if (o instanceof GString) return (o.toString());
return o;
}
public static String asString(Object o) {
if (o==null) return null;
return o.toString();
}
public static String asNonnullString(Object o) {
if (o==null) return "null";
return o.toString();
}
public static boolean groovyTruth(Collection> c) {
return c != null && !c.isEmpty();
}
public static boolean groovyTruth(String s) {
return s != null && !s.isEmpty();
}
public static boolean groovyTruth(Object o) {
// TODO Doesn't handle matchers (see http://docs.codehaus.org/display/GROOVY/Groovy+Truth)
if (o == null) {
return false;
} else if (o instanceof Boolean) {
return (Boolean)o;
} else if (o instanceof String) {
return !((String)o).isEmpty();
} else if (o instanceof Collection) {
return !((Collection)o).isEmpty();
} else if (o instanceof Map) {
return !((Map)o).isEmpty();
} else if (o instanceof Iterator) {
return ((Iterator)o).hasNext();
} else if (o instanceof Enumeration) {
return ((Enumeration)o).hasMoreElements();
} else {
return true;
}
}
public static Predicate groovyTruthPredicate() {
return new Predicate() {
@Override public boolean apply(T val) {
return groovyTruth(val);
}
};
}
public static Function