diff --git a/docs/default/feature/types.md b/docs/default/feature/types.md index 54f4f20..3a8709b 100644 --- a/docs/default/feature/types.md +++ b/docs/default/feature/types.md @@ -111,9 +111,11 @@ Take in count the multiple-type conversion doesn't do "magic" to convert types, ::: -:::caution Current limitations +:::tip Single type conversion -The `OptionalType` instance provide a single object to collection or array conversion, but it doesn't convert collections or arrays into single objects. +If you want the `OptionalType` instance as single object just wrap it with `OptionalType#single()` method, if the current type is a collection or array the first value will be used **recursively**. + +For only "the first value" use the method `OptionalType#first()` instead. ::: diff --git a/docs/es/feature/types.md b/docs/es/feature/types.md index c329add..016f424 100644 --- a/docs/es/feature/types.md +++ b/docs/es/feature/types.md @@ -111,9 +111,11 @@ Tomar en cuenta que la conversión de múltiples objetos no hace "magia" para co ::: -:::caution Limitaciones actuales +:::tip Conversión de objeto único -La instancia de `OptionalType` tiene conversión de objetos únicos a objetos múltiples como colecciones o array, pero no puede convertir colecciones y arrays en objetos únicos. +Si quieres que la instancia de `OptionalType` sea un objeto único solamente utiliza el método `OptionalType#single()`, si el tipo de objeto actual es una colección o array el primer valor será utilizado **de manera recursiva**. + +Para tener solo "el primer valor" utiliza en cambio el método `OptionalType#first()`. ::: diff --git a/src/main/java/com/saicone/rtag/util/OptionalType.java b/src/main/java/com/saicone/rtag/util/OptionalType.java index 4cddb68..f1b877b 100644 --- a/src/main/java/com/saicone/rtag/util/OptionalType.java +++ b/src/main/java/com/saicone/rtag/util/OptionalType.java @@ -110,6 +110,38 @@ public boolean isNotInstance(Class clazz) { return !isInstance(clazz); } + /** + * Get the first value.
+ * This method extract the first value if the current object is a collection or array. + * + * @return An OptionalType with object value. + */ + public OptionalType first() { + if (isIterable()) { + for (Object o : this) { + return OptionalType.of(o); + } + return OptionalType.EMPTY; + } + return this; + } + + /** + * Get actual value as single object
+ * This method make a recursively extract from the first value if the current object is a collection or array. + * + * @return An OptionalType with object value. + */ + public OptionalType single() { + if (isIterable()) { + for (Object o : this) { + return OptionalType.of(o).single(); + } + return OptionalType.EMPTY; + } + return this; + } + /** * Get actual value converted to required type. *