Package org.apache.sling.scripting.sightly.render

## Class ObjectModel

- java.lang.Object
- - org.apache.sling.scripting.sightly.render.ObjectModel

    public final class ObjectModel
    extends Object

The `ObjectModel` class provides various static models for object conversion and object property resolution.

### Method Summary

All Methods Static Methods Concrete Methods
`static String` `collectionToString​(Collection<?> collection)` Converts the passed `collection` to a comma separated values `String` representation.
`static Method` `findBeanMethod​(Class<?> cls, String baseName)` Given a bean class and a base method name, this method will try to find a public method without parameters that is named: `baseName` get + `BaseName` is + `BaseName`
`static Collection<Object>` `fromIterator​(Iterator<Object> iterator)` Given an `iterator`, this method will return a `Collection`.
`static Object` `getEnumValue​(Object object, String valueName)` Given an `object`, this method will return the value of the enum value identified by `valueName`.
`static Object` `getField​(Object object, String fieldName)` Given an `object`, this method will return the value of the public field identified by `fieldName`.
`static Object` `getIndex​(Object object, int index)` Given an indexable `object` (i.e.
`static Object` `invokeBeanMethod​(Object object, String methodName)` Given a bean `object`, this method will invoke the public method without parameters identified by `methodName` and return the invocation's result.
`static boolean` `isMethodAllowed​(Method method)` Returns `true` if the method is not one of the `Object`'s class declared methods, with the exception of `Object.toString()`.
`static boolean` `isPrimitive​(Object object)` Checks if the provided `object` is an instance of a primitive class.
`static Object` `resolveProperty​(Object target, Object property)` Given the `target` object, this method attempts to resolve and return the value of the passed `property`.

`static boolean`

`toBoolean​(Object object)`

Converts the given `object` to a boolean value, applying the following rules: if the `object` is `null` the returned value is `false` if the `object` is a `Number` the method will return `false` only if the number's value is 0 if the `String` representation of the `object` is equal irrespective of its casing to "true", the method will return `true` if the `object` is a `Collection` or a `Map`, the method will return `true` only if the collection / map is not empty if the object is an array, the method will return `true` only if the array is not empty

`static Collection<Object>`

`toCollection​(Object object)`

Forces the conversion of the passed `object` to an immutable collection, according to the following rules: if the `object` is `null` an empty collection will be returned if the `object` is an array a list transformation of the array will be returned if the `object` is a `Collection` the object itself will be returned if the `object` is an instance of a `Map` the map's key set will be returned (see `Map.keySet()`) if the `object` is an instance of an `Enumeration` a list transformation will be returned if the `object` is an instance of an `Iterator` or `Iterable` the result of `fromIterator(Iterator)` will be returned otherwise the `object` is wrapped in a single item list

`static Number` `toNumber​(Object object)` Coerces the passed `object` to a numeric value.
`static String` `toString​(Object object)` Converts the passed `object` to a `String`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### isPrimitive

    public static boolean isPrimitive​(Object object)

Checks if the provided `object` is an instance of a primitive class.

Parameters:
  `object` - the `Object` to check
Returns:
  `true` if the `object` is a primitive, `false` otherwise

#### resolveProperty

    public static Object resolveProperty​(Object target,
                                         Object property)

Given the `target` object, this method attempts to resolve and return the value of the passed `property`.

The property can be either an index or a name:

- index: the property is considered an index if its value is an integer number and in this case the `target` will be assumed to be either an array or it will be converted to a `Collection`; a fallback to `Map` will be made in case the previous two attempts failed
- name: the `property` will be converted to a `String` (see `toString(Object)`); the `target` will be assumed to be either a `Map` or an object; if the `Map` attempt fails, the `property` will be used to check if the `target` has a publicly accessible field with this name or a publicly accessible method with no parameters with this name or a combination of the "get" or "is" prefixes plus the capitalised name (see `invokeBeanMethod(Object, String)`)

Parameters:
  `target` - the target object
  `property` - the property to be resolved
Returns:
  the value of the property or `null`

#### toBoolean

    public static boolean toBoolean​(Object object)

Converts the given `object` to a boolean value, applying the following rules:

- if the `object` is `null` the returned value is `false`
- if the `object` is a `Number` the method will return `false` only if the number's value is 0
- if the `String` representation of the `object` is equal irrespective of its casing to "true", the method will return `true`
- if the `object` is a `Collection` or a `Map`, the method will return `true` only if the collection / map is not empty
- if the object is an array, the method will return `true` only if the array is not empty

Parameters:
  `object` - the target object
Returns:
  the boolean representation of the `object` according to the conversion rules

#### toNumber

    public static Number toNumber​(Object object)

Coerces the passed `object` to a numeric value. If the passed value is a `String` the conversion rules are those of `NumberUtils.createNumber(String)`.

Parameters:
  `object` - the target object
Returns:
  the numeric representation if one can be determined or `null`
See Also:
  `NumberUtils.createNumber(String)`

#### toString

    public static String toString​(Object object)

Converts the passed `object` to a `String`. The following rules apply:

- if the `object` is `null` an empty string will be returned
- if the `object` is an instance of a `String` the object itself will be returned
- if the object is a primitive (see `isPrimitive(Object)`), its `String` representation will be returned
- if the object is an `Enum` its name will be returned (see `Enum.name()`)
- if the object is a collection an attempt to convert the object to a `Collection` will be made and then the output of `collectionToString(Collection)` will be returned
- otherwise `object.toString()` is returned

Parameters:
  `object` - the target object
Returns:
  the string representation of the object or an empty string

#### toCollection

    public static Collection<Object> toCollection​(Object object)

Forces the conversion of the passed `object` to an immutable collection, according to the following rules:

- if the `object` is `null` an empty collection will be returned
- if the `object` is an array a list transformation of the array will be returned
- if the `object` is a `Collection` the object itself will be returned
- if the `object` is an instance of a `Map` the map's key set will be returned (see `Map.keySet()`)
- if the `object` is an instance of an `Enumeration` a list transformation will be returned
- if the `object` is an instance of an `Iterator` or `Iterable` the result of `fromIterator(Iterator)` will be returned
- otherwise the `object` is wrapped in a single item list

Parameters:
  `object` - the target object
Returns:
  the immutable collection representation of the object

#### collectionToString

    public static String collectionToString​(Collection<?> collection)

Converts the passed `collection` to a comma separated values `String` representation.

Parameters:
  `collection` - the collection to be converted to CSV
Returns:
  the CSV; if the `collection` is empty then an empty string will be returned

#### fromIterator

    public static Collection<Object> fromIterator​(Iterator<Object> iterator)

Given an `iterator`, this method will return a `Collection`.

Parameters:
  `iterator` - the iterator to be transformed into a `collection`
Returns:
  an immutable collection with the iterator's elements

#### getIndex

    public static Object getIndex​(Object object,
                                  int index)

Given an indexable `object` (i.e. an array or a collection), this method will return the value available at the `index` position.

Parameters:
  `object` - the indexable object
  `index` - the index
Returns:
  the value stored at the `index` or `null`

#### getField

    public static Object getField​(Object object,
                                  String fieldName)

Given an `object`, this method will return the value of the public field identified by `fieldName`.

Parameters:
  `object` - the target object
  `fieldName` - the name of the field
Returns:
  the value of the field or `null` if the field was not found

#### invokeBeanMethod

    public static Object invokeBeanMethod​(Object object,
                                          String methodName)

Given a bean `object`, this method will invoke the public method without parameters identified by `methodName` and return the invocation's result.

Parameters:
  `object` - the target object
  `methodName` - the name of the public method without parameters to invoke
Returns:
  the invocation's result or `null` if such a method cannot be found

#### getEnumValue

    public static Object getEnumValue​(Object object,
                                      String valueName)

Given an `object`, this method will return the value of the enum value identified by `valueName`.

Parameters:
  `object` - the target object
  `valueName` - the name of the enum value
Returns:
  the value of the enum or `null` if the enum was not found

#### findBeanMethod

    public static Method findBeanMethod​(Class<?> cls,
                                        String baseName)

Given a bean class and a base method name, this method will try to find a public method without parameters that is named:

1. `baseName`
2. get + `BaseName`
3. is + `BaseName`

Parameters:
  `cls` - the class into which to search for the method
  `baseName` - the base method name
Returns:
  a method that matches the criteria or `null`

#### isMethodAllowed

    public static boolean isMethodAllowed​(Method method)

Returns `true` if the method is not one of the `Object`'s class declared methods, with the exception of `Object.toString()`.

Parameters:
  `method` - the method to check
Returns:
  `true` if the method is not one of the `Object`'s class declared methods, with the exception of `Object.toString()`, `false` otherwise