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 @Deprecated
41 public class MultiArrayPointer {
42
43 /** A dummy object to mark the end. */
44 @Deprecated
45 public static final Object END = new Object();
46
47 private Object array;
48
49 private int index;
50
51 private int length;
52
53 private MultiArrayPointer sub;
54
55 private MultiArrayPointer backup;
56
57 /**
58 * Constructs a new pointer for an unspecified multi-dimensional array object.
59 *
60 * @deprecated (<i>)for internal use</i>) Visibility may be reduced to private.
61 */
62 @Deprecated
63 public MultiArrayPointer() {
64 }
65
66 /**
67 * Constructs a new pointer for the specified multidimensional array object.
68 *
69 * @param baseArray the multidimensional array to iterate over.
70 */
71 @Deprecated
72 public MultiArrayPointer(Object baseArray) {
73 this();
74 set(baseArray);
75 }
76
77 private void activateSub(Object element) {
78 if (backup == null) {
79 backup = new MultiArrayPointer();
80 }
81 sub = backup;
82 sub.set(element);
83 }
84
85 private void deactivateSub() {
86 sub = null;
87 }
88
89 /**
90 * Returns the next base element in the array
91 *
92 * @return the next base element in the array.
93 */
94 @Deprecated
95 public Object next() {
96 while (true) {
97 if (sub != null) {
98 Object subNext = sub.next();
99 if (subNext != MultiArrayPointer.END) {
100 return subNext;
101 }
102 deactivateSub();
103 }
104 if (index >= length) {
105 return MultiArrayPointer.END;
106 }
107 Object element = Array.get(array, index++);
108 if ((element == null) || !isSubArray(element)) {
109 return element;
110 }
111 activateSub(element);
112 }
113 }
114
115 /**
116 * Resets the pointer such that {@link #next()} will return the first element again.
117 */
118 @Deprecated
119 public void reset() {
120 index = 0;
121 deactivateSub();
122 }
123
124 private void set(Object newArray) {
125 array = newArray;
126 length = Array.getLength(array);
127 sub = null;
128 index = 0;
129 }
130
131 /**
132 * Checks whether entries in a given element are also arrays themselves.
133 *
134 * @param element the element
135 *
136 * @return <code>true</code> if the entries in the given element are also arrays, otherwise
137 * <code>false</code>.
138 *
139 * @deprecated (<i>)for internal use</i>) Visibility may be reduced to private.
140 */
141 @Deprecated
142 public static boolean isSubArray(Object element) {
143 if (!(element instanceof Object[])) {
144 return false;
145 }
146
147 for (Object o : (Object[]) element) {
148 if (o != null) {
149 if (o.getClass().isArray()) {
150 return true;
151 }
152 }
153 }
154
155 return false;
156 }
157 }