from_multidimensional_java_arrays

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
from_multidimensional_java_arrays [2025/04/18 17:10]
colinr
from_multidimensional_java_arrays [2025/04/18 17:31] (current)
colinr
Line 1: Line 1:
-===== sf00FromNested(Object data) =====+====== Programmatically Creating Complex SecsFormat00 Objects from Java Arrays ====== 
 + 
 + 
 +===== 🧩 Convert Mixed-Type Nested Arrays: sf00FromNested(Object data) =====
  
 Recursively converts a nested ''Object'' array into a hierarchical SECS structure, inferring SECS types based on the Java type of each leaf element. Recursively converts a nested ''Object'' array into a hierarchical SECS structure, inferring SECS types based on the Java type of each leaf element.
  
 You may use this method when your structure may contain integers, floats, doubles, longs, or strings. Each array level becomes a ''SecsFormat00''. You may use this method when your structure may contain integers, floats, doubles, longs, or strings. Each array level becomes a ''SecsFormat00''.
-==== Example usage ==== 
  
-<code java> +==== Example method ====
-Object[][] example +
-    {"abc", 123, 1.23}, +
-    {"xyz", new Object[] { 456L, 7.89f }}, +
-}; +
- +
-SecsFormat00 result sf00FromNested(example); +
-</code> +
- +
-**Expected output:** +
- +
-<code> +
-[[<A 'abc'>, <I4 123>, <F8 1.23000>], [<A 'xyz'>, [<I8 456>, <F4 7.89000>]]] +
-</code>+
  
 <code java> <code java>
Line 57: Line 46:
 } }
 </code> </code>
- 
- 
-===== sf00FromNestedStrings(Object data) ===== 
- 
-Recursively converts a nested ''Object'' array into a SECS structure assuming all leaf elements are strings or nulls. This is often better when you know the expected SecsFormat of your values because the type can be defined explicitly. 
  
 ==== Example usage ==== ==== Example usage ====
  
 <code java> <code java>
-String[][][] nestedStrings = { +Object[][] example = { 
-    +    {"abc", 1231.23}, 
-        {"A", "B"}, +    {"xyz", new Object[] 456L7.89f }},
-        {"C", "D"+
-    }, +
-    +
-        {"E", "F"}, +
-        {"G""H"} +
-    }+
 }; };
  
-SecsFormat00 result = sf00FromNestedStrings(nestedStrings);+SecsFormat00 result = sf00FromNested(example);
 </code> </code>
  
Line 83: Line 61:
  
 <code> <code>
-[[[<A 'A'>, <A 'B'>][<A 'C'>, <A 'D'>]], [[<A 'E'>, <A 'F'>], [<A 'G'>, <A 'H'>]]]+[[<A 'abc'>, <I4 123>, <F8 1.23000>], [<A 'xyz'>, [<I8 456>, <F4 7.89000>]]]
 </code> </code>
 +
 +
 +===== 🔤 Convert All-String Nested Arrays: sf00FromNestedStrings(Object data) =====
 +
 +Recursively converts a nested ''Object'' array into a SECS structure assuming all leaf elements are strings or nulls. This is often better when you know the expected SecsFormat of your values because the type can be defined explicitly.
 +
 +==== Example method ====
  
 <code java> <code java>
Line 113: Line 98:
 } }
 </code> </code>
- 
-===== sf00FromStructuredTriple(Object data) ===== 
- 
-Converts a nested ''Object'' array into a hierarchical SECS structure assuming each inner array has exactly 3 elements in the order: String, Float, Integer. 
- 
-Each row becomes a ''SecsFormat00'' containing: 
-  * ''SecsFormat20'' for the String 
-  * ''SecsFormat44'' for the Float 
-  * ''SecsFormat34'' for the Integer 
  
 ==== Example usage ==== ==== Example usage ====
  
 <code java> <code java>
-Object[][] triples = { +String[][][] nestedStrings = { 
-    {"abc", 1.5f42}, +    
-    {"def", 2.5f99}+        {"A", "B"}, 
 +        {"C", "D"
 +    }, 
 +    
 +        {"E", "F"}, 
 +        {"G", "H"
 +    }
 }; };
  
-SecsFormat00 result = sf00FromStructuredTriple(triples);+SecsFormat00 result = sf00FromNestedStrings(nestedStrings);
 </code> </code>
  
Line 137: Line 119:
  
 <code> <code>
-[[<A 'abc'>, <F4 1.50000>, <I4 42>], [<A 'def'>, <F4 2.50000>, <I4 99>]]+[[[<A 'A'>, <A 'B'>][<A 'C'>, <A 'D'>]], [[<A 'E'>, <A 'F'>][<A 'G'>, <A 'H'>]]]
 </code> </code>
 +
 +
 +===== 📦 Convert Structured Triple Arrays: sf00FromNestedStructuredTriple(Object data) =====
 +
 +Recursively converts a nested ''Object'' array into a hierarchical SECS structure, using explicit types at the leaf. In this example,  
 +Non-array elements are assumed to be leaf-level triples: **[String, Float, Integer]**.  
 +Arrays are recursively processed into nested ''SecsFormat00'' structures.
 +
 +==== Example method ====
  
 <code java> <code java>
 /** /**
- Converts a nested {@code Object} array into a hierarchical SECS structure assuming each inner array + Recursively converts a nested {@code Object} array into a hierarchical SECS structure.
- * has exactly 3 elements in the order: String, Float, Integer.+
  * <p>  * <p>
- Each row becomes a {@link SecsFormat00} containing: + Non-array elements are assumed to be leaf-level triples[StringFloat, Integer]. 
- * <ul> + Arrays are recursively processed into nested {@link SecsFormat00structures.
-   <li>{@link SecsFormat20} for the String</li> +
-   <li>{@link SecsFormat44} for the Float</li> +
-   <li>{@link SecsFormat34for the Integer</li> +
- * </ul>+
  *  *
- * @param data the nested Object array (e.g. Object[][])+ * @param data the nested Object array
  * @return a {@link SecsFormat00} representing the structured SECS data  * @return a {@link SecsFormat00} representing the structured SECS data
- * @throws IllegalArgumentException if the structure or types are incorrect+ * @throws IllegalArgumentException if the leaf triple is invalid or structure is malformed
  */  */
-public static SecsFormat00 sf00FromStructuredTriple(Object data) { +public static SecsFormat00 sf00FromNestedStructuredTriple(Object data) {
-    SecsFormat00 outerList = new SecsFormat00(); +
     if (!(data instanceof Object[])) {     if (!(data instanceof Object[])) {
-        throw new IllegalArgumentException("Input must be a 2D Object array (Object[][])");+        throw new IllegalArgumentException("Input must be an Object[]");
     }     }
  
-    for (Object rowObj : (Object[]) data) { +    Object[] array = (Object[]) data;
-        if (!(rowObj instanceof Object[])) { +
-            throw new IllegalArgumentException("Each row must be an Object array"); +
-        }+
  
-        Object[] row = (Object[]) rowObj; +    // Check if this is a valid leaf triple 
-        if (row.length != 3) { +    if (array.length == 3 && 
-            throw new IllegalArgumentException("Each row must have exactly 3 elements"); +            array[0] instanceof String && 
-        }+            array[1] instanceof Float && 
 +            array[2] instanceof Integer) {
  
-        Object strObj row[0]; +        SecsFormat00 triple new SecsFormat00(); 
-        Object floatObj = row[1]; +        triple.add(new SecsFormat20((String) array[0]))
-        Object intObj = row[2];+        triple.add(new SecsFormat44((Float) array[1]))
 +        triple.add(new SecsFormat34((Integer) array[2])); 
 +        return triple; 
 +    }
  
-        if (!(strObj instanceof String)) { +    // Otherwise, treat as nested structure 
-            throw new IllegalArgumentException("First element must be a String"); +    SecsFormat00 list = new SecsFormat00(); 
-        } +    for (Object element : array) { 
-        if (!(floatObj instanceof Float)) { +        if (!(element instanceof Object[])) { 
-            throw new IllegalArgumentException("Second element must be a Float"); +            throw new IllegalArgumentException("Encountered non-array element where nested structure was expected");
-        } +
-        if (!(intObj instanceof Integer)) { +
-            throw new IllegalArgumentException("Third element must be an Integer");+
         }         }
 +        list.add(sf00FromNestedStructuredTriple(element));
 +    }
  
-        SecsFormat00 rowList = new SecsFormat00()+    return list
-        rowList.add(new SecsFormat20((String) strObj)); +} 
-        rowList.add(new SecsFormat44((Float) floatObj)); +</code>
-        rowList.add(new SecsFormat34((Integer) intObj));+
  
-        outerList.add(rowList);+==== Example usage ==== 
 + 
 +<code java> 
 +Object[] nested = { 
 +    new Object[] { 
 +        new Object[] { "abc", 1.5f, 42 }, 
 +        new Object[] { "def", 2.5f, 99 } 
 +    }, 
 +    new Object[] { 
 +        new Object[] { "ghi", 3.0f, 7 }
     }     }
 +};
  
-    return outerList+SecsFormat00 result = sf00FromNestedStructuredTriple(nested)
-}+</code> 
 + 
 +**Expected output:** 
 + 
 +<code> 
 +[[[<A 'abc'>, <F4 1.50000>, <I4 42>], [<A 'def'>, <F4 2.50000>, <I4 99>]], [[<A 'ghi'>, <F4 3.00000>, <I4 7>]]]
 </code> </code>
  
  • from_multidimensional_java_arrays.1745014255.txt.gz
  • Last modified: 2025/04/18 17:10
  • by colinr