1 package nom.tam.util.array;
2
3 /*
4 * #%L
5 * nom.tam FITS library
6 * %%
7 * Copyright (C) 1996 - 2024 nom-tam-fits
8 * %%
9 * This is free and unencumbered software released into the public domain.
10 *
11 * Anyone is free to copy, modify, publish, use, compile, sell, or
12 * distribute this software, either in source code form or as a compiled
13 * binary, for any purpose, commercial or non-commercial, and by any
14 * means.
15 *
16 * In jurisdictions that recognize copyright laws, the author or authors
17 * of this software dedicate any and all copyright interest in the
18 * software to the public domain. We make this dedication for the benefit
19 * of the public at large and to the detriment of our heirs and
20 * successors. We intend this dedication to be an overt act of
21 * relinquishment in perpetuity of all present and future rights to this
22 * software under copyright law.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 * #L%
32 */
33
34 import java.lang.reflect.Array;
35
36 /**
37 * @deprecated (<i>)for internal use</i>) Visibility may be reduced to the package level in the future. A
38 * multi-dimensional array index. Used only by {@link MultiArrayCopier} and {@link MultiArrayIterator}.
39 */
40 @SuppressWarnings("javadoc")
41 public class MultiArrayPointer {
42
43 public static final Object END = new Object();
44
45 public static boolean isSubArray(Object element) {
46 if (!(element instanceof Object[])) {
47 return false;
48 }
49
50 for (Object o : (Object[]) element) {
51 if (o != null) {
52 if (o.getClass().isArray()) {
53 return true;
54 }
55 }
56 }
57
58 return false;
59 }
60
61 private Object array;
62
63 private int index;
64
65 private int length;
66
67 private MultiArrayPointer sub;
68
69 private MultiArrayPointer backup;
70
71 public MultiArrayPointer() {
72 }
73
74 public MultiArrayPointer(Object baseArray) {
75 set(baseArray);
76 }
77
78 private void activateSub(Object element) {
79 if (backup == null) {
80 backup = new MultiArrayPointer();
81 }
82 sub = backup;
83 sub.set(element);
84 }
85
86 private void deactivateSub() {
87 sub = null;
88 }
89
90 public Object next() {
91 while (true) {
92 if (sub != null) {
93 Object subNext = sub.next();
94 if (subNext != MultiArrayPointer.END) {
95 return subNext;
96 }
97 deactivateSub();
98 }
99 if (index >= length) {
100 return MultiArrayPointer.END;
101 }
102 Object element = Array.get(array, index++);
103 if ((element == null) || !isSubArray(element)) {
104 return element;
105 }
106 activateSub(element);
107 }
108 }
109
110 public void reset() {
111 index = 0;
112 deactivateSub();
113 }
114
115 private void set(Object newArray) {
116 array = newArray;
117 length = Array.getLength(array);
118 sub = null;
119 index = 0;
120 }
121
122 }