Record Class NaftahObject
- Record Components:
fromJava- whether this object originates from a Java instancejavaObject- the wrapped Java object iffromJavaistrue; otherwisenulltype- the Java class type of the wrapped object or declared structureobjectFields- the field definitions if this object is a declarative Naftah object; otherwisenull
This record provides utilities to:
- Wrap Java objects and expose their structure as nested maps
- Recursively serialize Java objects, records, arrays, and collections into a
Map - Transliterate field or key names into Arabic script for user-facing representation
- Distinguish between Java-backed and declaratively defined Naftah objects
When fromJava is true, the instance wraps a native Java object.
Otherwise, it wraps a map of DeclaredVariables describing a Naftah object structure.
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringFormat pattern for field processing errors in Arabic: "[خطأ أثناء معالجة الحقل: %s]".private final booleanThe field for thefromJavarecord component.private final ObjectThe field for thejavaObjectrecord component.static final StringFormat pattern used for transliterating field or key names into Arabic script.static final intMaximum depth allowed for Java object reflection mapping.private final Map<String,DeclaredVariable> The field for theobjectFieldsrecord component.static final booleanWhether null fields should be skipped during reflection mapping.private final Class<?>The field for thetyperecord component. -
Constructor Summary
ConstructorsConstructorDescriptionNaftahObject(boolean fromJava, Object javaObject, Class<?> type, Map<String, DeclaredVariable> objectFields) Canonical constructor with validation logic. -
Method Summary
Modifier and TypeMethodDescriptionprivate static ObjectconvertValue(Object value, IdentityHashMap<Object, Boolean> visited, boolean skipNulls, int depth) Converts an arbitrary value (including nested collections, maps, or arrays) into a serializable representation suitable for mapping.booleanCompares thisNaftahObjectto another object for equality.static StringformatKeyOrFieldName(String keyOrFieldName) Formats a given key or field name into a bilingual representation using Arabic transliteration.booleanfromJava()Returns the value of thefromJavarecord component.get()Returns the evaluated representation of this object.get(boolean original) Returns the evaluated value represented by thisNaftahObject.static ObjectReturns a converted form of the given object suitable for Naftah representation.inthashCode()Computes the hash code for thisNaftahObject.Returns the value of thejavaObjectrecord component.Returns the value of theobjectFieldsrecord component.static NaftahObjectCreates aNaftahObjectinstance backed by a Java object.static NaftahObjectof(Map<String, DeclaredVariable> objectFields) Creates aNaftahObjectinstance backed by declarative field definitions.Converts an arbitrary Java object into a structuredMaprepresentation.Converts an arbitrary Java object into a structuredMaprepresentation, optionally skippingnullvalues.toMap(Object obj, IdentityHashMap<Object, Boolean> visited, boolean skipNulls, int depth) Recursively resolves a Java object into a nested map structure, tracking visited instances to prevent infinite recursion from circular references.toString()Returns a string representation of this Naftah object.Class<?>type()Returns the value of thetyperecord component.
-
Field Details
-
fromJava
private final boolean fromJavaThe field for thefromJavarecord component. -
javaObject
The field for thejavaObjectrecord component. -
type
The field for thetyperecord component. -
objectFields
The field for theobjectFieldsrecord component. -
MAX_DEPTH
public static final int MAX_DEPTHMaximum depth allowed for Java object reflection mapping. -
SKIP_NULLS
public static final boolean SKIP_NULLSWhether null fields should be skipped during reflection mapping. -
KEY_OR_FIELD_TRANSLITERATION_FORMAT
Format pattern used for transliterating field or key names into Arabic script. The resulting format will be"<arabic> (<latin>)".- See Also:
-
FIELD_ERROR_FORMAT
Format pattern for field processing errors in Arabic: "[خطأ أثناء معالجة الحقل: %s]".- See Also:
-
-
Constructor Details
-
NaftahObject
public NaftahObject(boolean fromJava, Object javaObject, Class<?> type, Map<String, DeclaredVariable> objectFields) Canonical constructor with validation logic.Ensures that required fields are non-null based on
fromJava:- If
fromJavaistrue,javaObjectmust not benull. - If
fromJavaisfalse,objectFieldsmust not benull.
- Throws:
NullPointerException- iftypeisnull, or if required fields are missing
- If
-
-
Method Details
-
formatKeyOrFieldName
Formats a given key or field name into a bilingual representation using Arabic transliteration.- Parameters:
keyOrFieldName- the Latin field or key name- Returns:
- a formatted string combining Arabic transliteration and Latin script
-
of
Creates aNaftahObjectinstance backed by declarative field definitions.- Parameters:
objectFields- a map of field names to declared variables- Returns:
- a new
NaftahObjectrepresenting a declarative object - Throws:
NullPointerException- ifobjectFieldsisnull
-
of
Creates aNaftahObjectinstance backed by a Java object.- Parameters:
javaObject- the Java object to wrap- Returns:
- a new
NaftahObjectrepresenting a Java object - Throws:
NullPointerException- ifjavaObjectisnull
-
toMap
Converts an arbitrary Java object into a structuredMaprepresentation. Equivalent to callingtoMap(Object, boolean)withskipNulls = false.- Parameters:
obj- the object to convert- Returns:
- a map representation of the object, or
nullif the input isnull
-
toMap
Converts an arbitrary Java object into a structuredMaprepresentation, optionally skippingnullvalues.- Parameters:
obj- the object to convertskipNulls- whether to excludenullvalues from the resulting map- Returns:
- a map representation of the object, or
nullif the input isnull
-
toMap
private static Map<String,Object> toMap(Object obj, IdentityHashMap<Object, Boolean> visited, boolean skipNulls, int depth) Recursively resolves a Java object into a nested map structure, tracking visited instances to prevent infinite recursion from circular references.This method supports:
- Records (via
RecordComponent) - Plain old Java objects (POJOs)
- Collections, maps, and arrays
If a circular reference is detected, a special placeholder entry is added:
"[circular-reference]" → <simple-class-name>. - Records (via
-
convertValue
private static Object convertValue(Object value, IdentityHashMap<Object, Boolean> visited, boolean skipNulls, int depth) Converts an arbitrary value (including nested collections, maps, or arrays) into a serializable representation suitable for mapping.- Parameters:
value- the value to convertvisited- objects already visited during recursion to avoid cyclesskipNulls- whether to skipnullentries- Returns:
- a converted value or map/list structure
-
get
Returns a converted form of the given object suitable for Naftah representation.Performs recursive conversion for collections, maps, arrays, and custom objects. Leaves simple or built-in types unchanged.
- Parameters:
javaObject- the object to process- Returns:
- a converted object representation
-
get
Returns the evaluated value represented by thisNaftahObject.- If wrapping a Java object, recursively resolves it into a map, collection, or scalar value.
- If wrapping declared fields, returns the underlying field map directly.
- Returns:
- the evaluated representation of this object
-
get
Returns the evaluated representation of this object.This is equivalent to calling
get(boolean)withoriginal = false.- Returns:
- the evaluated representation of this object
-
equals
Compares thisNaftahObjectto another object for equality.Two
NaftahObjectinstances are considered equal if and only if:- They are of the same runtime class, and
- Their
fromJavaflags are identical, and - Their
javaObject,type, andobjectFieldsproperties are equal according toObjects.equals(Object, Object).
This ensures that equality reflects both the source (Java vs. declarative) and the internal data or type structure of the wrapped object.
-
hashCode
public int hashCode()Computes the hash code for thisNaftahObject.The hash code is derived from the
fromJavaflag, thejavaObjectreference, thetypeof the object, and theobjectFieldsmap. This ensures that twoNaftahObjectinstances considered equal will always produce the same hash code, satisfying the general contract ofObject.hashCode(). -
toString
Returns a string representation of this Naftah object.The format depends on whether it wraps a Java object or declared fields:
- For collections, maps, and arrays → formatted collection output
- For simple or built-in values → localized string representation
- For complex Java objects → Arabic-labeled key-value pairs
- For declarative Naftah objects → field map string representation
-
fromJava
public boolean fromJava()Returns the value of thefromJavarecord component.- Returns:
- the value of the
fromJavarecord component
-
javaObject
Returns the value of thejavaObjectrecord component.- Returns:
- the value of the
javaObjectrecord component
-
type
Returns the value of thetyperecord component.- Returns:
- the value of the
typerecord component
-
objectFields
Returns the value of theobjectFieldsrecord component.- Returns:
- the value of the
objectFieldsrecord component
-