from_multidimensional_java_arrays
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| from_multidimensional_java_arrays [2025/04/18 18:10] – colinr | from_multidimensional_java_arrays [2025/04/18 18:31] (current) – colinr | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== sf00FromNested(Object data) ===== | + | ====== Programmatically Creating Complex SecsFormat00 Objects from Java Arrays ====== |
| + | |||
| + | |||
| + | ===== 🧩 Convert Mixed-Type Nested Arrays: | ||
| Recursively converts a nested '' | Recursively converts a nested '' | ||
| You may use this method when your structure may contain integers, floats, doubles, longs, or strings. Each array level becomes a '' | You may use this method when your structure may contain integers, floats, doubles, longs, or strings. Each array level becomes a '' | ||
| - | ==== Example usage ==== | ||
| - | <code java> | + | ==== Example method ==== |
| - | Object[][] example | + | |
| - | {" | + | |
| - | {" | + | |
| - | }; | + | |
| - | + | ||
| - | SecsFormat00 result | + | |
| - | </ | + | |
| - | + | ||
| - | **Expected output:** | + | |
| - | + | ||
| - | < | + | |
| - | [[<A ' | + | |
| - | </ | + | |
| <code java> | <code java> | ||
| Line 57: | Line 46: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | |||
| - | ===== sf00FromNestedStrings(Object data) ===== | ||
| - | |||
| - | Recursively converts a nested '' | ||
| ==== Example usage ==== | ==== Example usage ==== | ||
| <code java> | <code java> | ||
| - | String[][][] nestedStrings | + | Object[][] example |
| - | | + | {"abc", |
| - | | + | {"xyz", |
| - | {" | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| }; | }; | ||
| - | SecsFormat00 result = sf00FromNestedStrings(nestedStrings); | + | SecsFormat00 result = sf00FromNested(example); |
| </ | </ | ||
| Line 83: | Line 61: | ||
| < | < | ||
| - | [[[<A 'A'>, <A ' | + | [[<A 'abc'>, <I4 123>, <F8 1.23000>], [<A 'xyz'>, [<I8 456>, <F4 7.89000>]]] |
| </ | </ | ||
| + | |||
| + | |||
| + | ===== 🔤 Convert All-String Nested Arrays: sf00FromNestedStrings(Object data) ===== | ||
| + | |||
| + | Recursively converts a nested '' | ||
| + | |||
| + | ==== Example method ==== | ||
| <code java> | <code java> | ||
| Line 113: | Line 98: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | ===== sf00FromNested(Object data) ===== | ||
| - | |||
| - | Recursively converts a nested '' | ||
| - | |||
| - | Use this method when your structure may contain integers, floats, doubles, longs, or strings. Each array level becomes a '' | ||
| - | |||
| - | ==== Example usage ==== | ||
| - | |||
| - | <code java> | ||
| - | Object[][] example = { | ||
| - | {" | ||
| - | {" | ||
| - | }; | ||
| - | |||
| - | SecsFormat00 result = sf00FromNested(example); | ||
| - | </ | ||
| - | |||
| - | **Expected output:** | ||
| - | |||
| - | < | ||
| - | [[<A ' | ||
| - | </ | ||
| - | |||
| - | <code java> | ||
| - | /** | ||
| - | * Recursively converts a nested {@code Object} (array or leaf) into a hierarchical SECS structure. | ||
| - | | ||
| - | * Arrays (including multidimensional ones) become nested {@link SecsFormat00}, | ||
| - | * to SECS types based on their Java type. | ||
| - | * | ||
| - | * @param data the nested Object to convert (can be an Object[], Object[][], etc.) | ||
| - | * @return a {@link SecsFormat00} representation of the structure | ||
| - | */ | ||
| - | public static SecsFormat00 sf00FromNested(Object data) { | ||
| - | SecsFormat00 list = new SecsFormat00(); | ||
| - | |||
| - | if (data instanceof Object[]) { | ||
| - | for (Object element : (Object[]) data) { | ||
| - | if (element instanceof Object[]) { | ||
| - | list.add(sf00FromNested(element)); | ||
| - | } else if (element instanceof Integer) { | ||
| - | list.add(new SecsFormat34((Integer) element)); | ||
| - | } else if (element instanceof Long) { | ||
| - | list.add(new SecsFormat30((Long) element)); | ||
| - | } else if (element instanceof Float) { | ||
| - | list.add(new SecsFormat44((Float) element)); | ||
| - | } else if (element instanceof Double) { | ||
| - | list.add(new SecsFormat40((Double) element)); | ||
| - | } else { | ||
| - | list.add(new SecsFormat20(String.valueOf(element))); | ||
| - | } | ||
| - | } | ||
| - | } else { | ||
| - | throw new IllegalArgumentException(" | ||
| - | } | ||
| - | |||
| - | return list; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||
| - | ===== sf00FromNestedStrings(Object data) ===== | ||
| - | |||
| - | Recursively converts a nested '' | ||
| - | |||
| - | This method is more efficient than '' | ||
| ==== Example usage ==== | ==== Example usage ==== | ||
| Line 203: | Line 121: | ||
| [[[<A ' | [[[<A ' | ||
| </ | </ | ||
| + | |||
| + | |||
| + | ===== 📦 Convert Structured Triple Arrays: sf00FromNestedStructuredTriple(Object data) ===== | ||
| + | |||
| + | Recursively converts a nested '' | ||
| + | Non-array elements are assumed to be leaf-level triples: **[String, Float, Integer]**. | ||
| + | Arrays are recursively processed into nested '' | ||
| + | |||
| + | ==== Example method ==== | ||
| <code java> | <code java> | ||
| /** | /** | ||
| - | * Recursively converts a nested {@code Object} | + | * Recursively converts a nested {@code Object} array into a hierarchical |
| - | * | + | |
| - | | + | |
| + | * Arrays are recursively processed into nested {@link SecsFormat00} structures. | ||
| * | * | ||
| - | * @param data a nested Object | + | * @param data the nested Object |
| - | * @return a {@link SecsFormat00} structure | + | * @return a {@link SecsFormat00} |
| + | * @throws IllegalArgumentException if the leaf triple is invalid or structure | ||
| */ | */ | ||
| - | public static SecsFormat00 | + | public static SecsFormat00 |
| + | if (!(data instanceof Object[])) { | ||
| + | throw new IllegalArgumentException(" | ||
| + | } | ||
| + | |||
| + | Object[] array = (Object[]) data; | ||
| + | |||
| + | // Check if this is a valid leaf triple | ||
| + | if (array.length == 3 && | ||
| + | array[0] instanceof String && | ||
| + | array[1] instanceof Float && | ||
| + | array[2] instanceof Integer) { | ||
| + | |||
| + | SecsFormat00 triple = new SecsFormat00(); | ||
| + | triple.add(new SecsFormat20((String) array[0])); | ||
| + | triple.add(new SecsFormat44((Float) array[1])); | ||
| + | triple.add(new SecsFormat34((Integer) array[2])); | ||
| + | return triple; | ||
| + | } | ||
| + | |||
| + | // Otherwise, treat as nested structure | ||
| SecsFormat00 list = new SecsFormat00(); | SecsFormat00 list = new SecsFormat00(); | ||
| - | + | | |
| - | if (data instanceof Object[]) { | + | if (!(element instanceof Object[])) { |
| - | | + | |
| - | if (element instanceof Object[]) { | + | |
| - | list.add(sf00FromNestedStrings(element)); | + | |
| - | | + | |
| - | list.add(new SecsFormat20(String.valueOf(element))); | + | |
| - | } | + | |
| } | } | ||
| - | } else { | + | list.add(sf00FromNestedStructuredTriple(element)); |
| - | throw new IllegalArgumentException("Input must be an Object array at the top level"); | + | |
| } | } | ||
| Line 231: | Line 174: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | |||
| - | ===== sf00FromStructuredTriple(Object data) ===== | ||
| - | |||
| - | Converts a nested '' | ||
| - | |||
| - | Each row becomes a '' | ||
| - | * '' | ||
| - | * '' | ||
| - | * '' | ||
| ==== Example usage ==== | ==== Example usage ==== | ||
| <code java> | <code java> | ||
| - | Object[][] triples | + | Object[] |
| - | {" | + | |
| - | {" | + | new Object[] |
| + | new Object[] | ||
| + | }, | ||
| + | new Object[] { | ||
| + | new Object[] { " | ||
| + | | ||
| }; | }; | ||
| - | SecsFormat00 result = sf00FromStructuredTriple(triples); | + | SecsFormat00 result = sf00FromNestedStructuredTriple(nested); |
| </ | </ | ||
| Line 256: | Line 194: | ||
| < | < | ||
| - | [[<A ' | + | [[[<A ' |
| - | </code> | + | |
| - | + | ||
| - | <code java> | + | |
| - | /** | + | |
| - | * Converts a nested {@code Object} array into a hierarchical SECS structure assuming each inner array | + | |
| - | * has exactly | + | |
| - | * <p> | + | |
| - | * Each row becomes a {@link SecsFormat00} containing: | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | * </ | + | |
| - | * | + | |
| - | * @param data the nested Object array (e.g. Object[][]) | + | |
| - | * @return a {@link SecsFormat00} representing the structured SECS data | + | |
| - | * @throws IllegalArgumentException if the structure or types are incorrect | + | |
| - | */ | + | |
| - | public static SecsFormat00 sf00FromStructuredTriple(Object data) { | + | |
| - | SecsFormat00 outerList = new SecsFormat00(); | + | |
| - | + | ||
| - | if (!(data instanceof Object[])) { | + | |
| - | throw new IllegalArgumentException(" | + | |
| - | } | + | |
| - | + | ||
| - | for (Object rowObj : (Object[]) data) { | + | |
| - | if (!(rowObj instanceof Object[])) { | + | |
| - | throw new IllegalArgumentException(" | + | |
| - | } | + | |
| - | + | ||
| - | Object[] row = (Object[]) rowObj; | + | |
| - | if (row.length != 3) { | + | |
| - | throw new IllegalArgumentException(" | + | |
| - | } | + | |
| - | + | ||
| - | Object strObj = row[0]; | + | |
| - | Object floatObj = row[1]; | + | |
| - | Object intObj = row[2]; | + | |
| - | + | ||
| - | if (!(strObj instanceof String)) { | + | |
| - | throw new IllegalArgumentException(" | + | |
| - | } | + | |
| - | if (!(floatObj instanceof Float)) { | + | |
| - | throw new IllegalArgumentException(" | + | |
| - | } | + | |
| - | if (!(intObj instanceof Integer)) { | + | |
| - | throw new IllegalArgumentException(" | + | |
| - | } | + | |
| - | + | ||
| - | SecsFormat00 rowList = new SecsFormat00(); | + | |
| - | rowList.add(new SecsFormat20((String) strObj)); | + | |
| - | rowList.add(new SecsFormat44((Float) floatObj)); | + | |
| - | rowList.add(new SecsFormat34((Integer) intObj)); | + | |
| - | + | ||
| - | outerList.add(rowList); | + | |
| - | } | + | |
| - | + | ||
| - | return outerList; | + | |
| - | } | + | |
| </ | </ | ||
from_multidimensional_java_arrays.1745014219.txt.gz · Last modified: by colinr
