Brooklyn

brooklyn.util.internal
[Groovy] Class LanguageUtils

java.lang.Object
  brooklyn.util.internal.LanguageUtils

class LanguageUtils

Useful Groovy utility methods.


Nested Class Summary
interface LanguageUtils.FieldVisitor

 
Field Summary
static groovy.lang.Closure DEFAULT_FIELD_GETTER

Default field getter.

static java.lang.Object NO_SUCH_FIELD

return value used to indicate that there is no such field

 
Method Summary
static java.util.List addToMapOfLists(java.util.Map map, java.lang.Object key, java.lang.Object valueInCollection)

as addToMapOfSets(Map, Object, Object) but for java.util.ArrayList

static java.util.Set addToMapOfSets(java.util.Map map, java.lang.Object key, java.lang.Object valueInCollection)

Adds the given value to a collection in the map under the key.

static java.lang.Object clone(java.lang.Object src)

static java.lang.Object deserialize(byte[] bytes, java.lang.ClassLoader classLoader)

static boolean equals(java.lang.Object o1, java.lang.Object o2, java.lang.Class optionalCommonSuperClass = null, groovy.lang.Closure optionalGetter = null, java.lang.Iterable fieldNames)

Checks equality of o1 and o2 with respect to the named fields, optionally enforcing a common superclass and using a custom field-getter.

static boolean equals(java.lang.Object o1, java.lang.Object o2, java.lang.Class optionalCommonSuperClass = null, groovy.lang.Closure optionalGetter = null, java.lang.Object[] fieldNames)

static java.util.Collection forBoth(java.util.Collection l1, java.util.Collection l2, groovy.lang.Closure code)

Iterates through two collections simultaneously, passing both args to code.

static java.util.Collection forBoth(java.lang.Object[] l1, java.lang.Object[] l2, groovy.lang.Closure code)

static java.util.Collection forBothWithIndex(java.util.Collection l1, java.util.Collection l2, groovy.lang.Closure code)

static java.util.Collection forBothWithIndex(java.lang.Object[] l1, java.lang.Object[] l2, groovy.lang.Closure code)

static java.lang.Object getOptionalField(java.lang.String name, java.util.Map m, java.lang.Object defaultValue = null)

static java.lang.Object getPropertySafe(java.lang.Object target, java.lang.String name, java.lang.Object defaultValue = null)

static java.lang.Object getRequiredField(java.lang.String name, java.util.Map m)

static int hashCode(java.lang.Object o, groovy.lang.Closure optionalGetter = null, java.util.Collection fieldNames)

Generates a hashcode for an object.

static int hashCode(java.lang.Object o, groovy.lang.Closure optionalGetter = null, java.lang.Object[] fieldNames)

static java.lang.String newUid()

@deprecated use Identifiers.makeRandomId(8)

static boolean removeFromMapOfCollections(java.util.Map map, java.lang.Object key, java.lang.Object valueInCollection)

Removes the given value from a collection in the map under the key.

static boolean repeatUntilSuccess(java.lang.String description, java.util.concurrent.Callable action)

static boolean repeatUntilSuccess(java.util.Map flags = [:], java.lang.String description = null, java.util.concurrent.Callable action)

static byte[] serialize(java.lang.Object orig)

static java.util.Map setFieldsFromMap(java.lang.Object target, java.util.Map fieldValues)

static void visitFields(java.lang.Object o, LanguageUtils.FieldVisitor fv, java.util.Collection objectsToSkip = ([] as Set)

Visits all fields of a given object, recursively.

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

DEFAULT_FIELD_GETTER

public static final groovy.lang.Closure DEFAULT_FIELD_GETTER
Default field getter. Delegates to object[field] (which will invoke a getter if one exists, in groovy), unless field starts with {
literal:
@} in which case it looks up the actual java field (bypassing getter).

Can be extended as needed when passed to equals(Object, Object, Class, String[])


NO_SUCH_FIELD

public static final java.lang.Object NO_SUCH_FIELD
return value used to indicate that there is no such field


 
Method Detail

addToMapOfLists

static java.util.List addToMapOfLists(java.util.Map map, java.lang.Object key, java.lang.Object valueInCollection)
as addToMapOfSets(Map, Object, Object) but for java.util.ArrayList


addToMapOfSets

static java.util.Set addToMapOfSets(java.util.Map map, java.lang.Object key, java.lang.Object valueInCollection)
Adds the given value to a collection in the map under the key. A collection (as java.util.LinkedHashMap) will be created if necessary, synchronized on map for map access/change and set for addition there
Returns:
the updated set (instance, not copy)


clone

static java.lang.Object clone(java.lang.Object src)


deserialize

static java.lang.Object deserialize(byte[] bytes, java.lang.ClassLoader classLoader)


equals

static boolean equals(java.lang.Object o1, java.lang.Object o2, java.lang.Class optionalCommonSuperClass = null, groovy.lang.Closure optionalGetter = null, java.lang.Iterable fieldNames)
Checks equality of o1 and o2 with respect to the named fields, optionally enforcing a common superclass and using a custom field-getter. Other types can be supplied if they are supported by object[field] (what the DEFAULT_FIELD_GETTER does) or if the {
literal:
optionalGetter} handles it. Note that object[field] causes invocation of object.getAt(field) (which can be provided on the object for non-strings - this is preferred to an optionalGetter, generally) looking for object.getXxx(), where field is a string {@literal xxx}, then object.xxx.

One exception is that field names which start with {@literal

@} get the field directly according to DEFAULT_FIELD_GETTER, but use with care on private fields, as they must be on the object and not a superclass, and with groovy properties (formerly known as package-private, i.e. with no access modifiers) because they become private fields.

For example

 public class Foo {
   Object bar;
   public boolean equals(Object other) { LangaugeUtils.equals(this, other, Foo.class, ["bar"]); }
   public int hashCode() { LangaugeUtils.hashCode(this, ["bar"]); }
 }
 
Parameters:
o1 - one object to compare
o2 - other object to compare
optionalCommonSuperClass - if supplied, returns false unless both objects are instances of the given type; (if not supplied it effectively does duck typing, returning false if any field is not present)
optionalGetter - if supplied, a closure which takes (object, field) and returns the value of field on object; should return static NO_SUCH_FIELD if none found; recommended to delegate to DEFAULT_FIELD_GETTER at least for strings (or for anything)
fields - typically a list of strings being names of fields on the class to compare
Returns:
true if the two objects are equal in all indicated fields, and conform to the optionalCommonSuperClass if supplied


equals

static boolean equals(java.lang.Object o1, java.lang.Object o2, java.lang.Class optionalCommonSuperClass = null, groovy.lang.Closure optionalGetter = null, java.lang.Object[] fieldNames)


forBoth

static java.util.Collection forBoth(java.util.Collection l1, java.util.Collection l2, groovy.lang.Closure code)
Iterates through two collections simultaneously, passing both args to code.
 a = ['a','b']; b=[1,2];
 assert ['a1','b2'] == forboth(a,b) { x,y -> x+y }
 


forBoth

static java.util.Collection forBoth(java.lang.Object[] l1, java.lang.Object[] l2, groovy.lang.Closure code)


forBothWithIndex

static java.util.Collection forBothWithIndex(java.util.Collection l1, java.util.Collection l2, groovy.lang.Closure code)


forBothWithIndex

static java.util.Collection forBothWithIndex(java.lang.Object[] l1, java.lang.Object[] l2, groovy.lang.Closure code)


getOptionalField

static java.lang.Object getOptionalField(java.lang.String name, java.util.Map m, java.lang.Object defaultValue = null)


getPropertySafe

static java.lang.Object getPropertySafe(java.lang.Object target, java.lang.String name, java.lang.Object defaultValue = null)


getRequiredField

static java.lang.Object getRequiredField(java.lang.String name, java.util.Map m)


hashCode

static int hashCode(java.lang.Object o, groovy.lang.Closure optionalGetter = null, java.util.Collection fieldNames)
Generates a hashcode for an object. Similar to com.google.common.base.Objects#hashCode() but taking field names and an optional getter, with the same rich groovy semantics as described in equals(Object, Object, Class).


hashCode

static int hashCode(java.lang.Object o, groovy.lang.Closure optionalGetter = null, java.lang.Object[] fieldNames)


newUid

static java.lang.String newUid()
deprecated:
use Identifiers.makeRandomId(8)


removeFromMapOfCollections

static boolean removeFromMapOfCollections(java.util.Map map, java.lang.Object key, java.lang.Object valueInCollection)
Removes the given value from a collection in the map under the key.
Returns:
the updated set (instance, not copy)


repeatUntilSuccess

static boolean repeatUntilSuccess(java.lang.String description, java.util.concurrent.Callable action)


repeatUntilSuccess

static boolean repeatUntilSuccess(java.util.Map flags = [:], java.lang.String description = null, java.util.concurrent.Callable action)


serialize

static byte[] serialize(java.lang.Object orig)


setFieldsFromMap

static java.util.Map setFieldsFromMap(java.lang.Object target, java.util.Map fieldValues)


visitFields

static void visitFields(java.lang.Object o, LanguageUtils.FieldVisitor fv, java.util.Collection objectsToSkip = ([] as Set)
Visits all fields of a given object, recursively. For collections, arrays, and maps it visits the items within, passing null for keys where it isn't a map.


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.