File: RectangularArraySummary.java

package info (click to toggle)
rjava 1.0-11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,184 kB
  • sloc: java: 13,223; ansic: 5,479; sh: 3,776; xml: 325; makefile: 250; perl: 33
file content (269 lines) | stat: -rw-r--r-- 6,064 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// :tabSize=2:indentSize=2:noTabs=false:folding=explicit:collapseFolds=1:

import java.lang.reflect.Array ; 

/** 
 * Utility class to extract something from a rectangular array
 */
public class RectangularArraySummary extends RJavaArrayIterator {
	
	private int length ; 
	
	private String typeName ;
	
	private boolean isprimitive ;
	
	private Class componentType ;
	
	/**
	 * Constructor
	 *
	 * @param array the array to check
	 * @throws NotAnArrayException if array is not an array
	 */
	public RectangularArraySummary(Object array, int[] dimensions) throws NotAnArrayException, NotComparableException {
		super( dimensions );
		this.array = array ;
		typeName = RJavaArrayTools.getObjectTypeName(array );
		isprimitive = RJavaArrayTools.isPrimitiveTypeName( typeName ) ;
		try{
			componentType = RJavaArrayTools.getClassForSignature( typeName , array.getClass().getClassLoader() ) ;
		} catch( ClassNotFoundException e){}
		checkComparableObjects() ;
	}
	
	public RectangularArraySummary(Object array, int length ) throws NotAnArrayException, NotComparableException{
		this( array, new int[]{ length } ); 
	}
	
	/** 
	 * Iterates over the array to find the minimum value
	 * (in the sense of the Comparable interface)
	 */
	public Object min( boolean narm ) {
		if( isprimitive ){
			return null ; // TODO :implement
		}
		Object smallest = null ;
		Object current ;
		boolean found = false ;
		
		if( dimensions.length == 1 ){
			return( min( (Object[])array, narm ) ) ;
		} else{
			
			/* need to iterate */
			while( hasNext() ){
				current = min( (Object[])next(), narm ) ;
				if( current == null ){
					if( !narm ) return null ;
				} else{
					if( !found ){
						smallest = current ;
						found = true ;
					} else if( smaller( current, smallest ) ) {
						smallest = current ;
					}					
				}
			}
			return smallest ;
		}
		
	}
	
	/** 
	 * Iterates over the array to find the maximum value
	 * (in the sense of the Comparable interface)
	 */
	public Object max( boolean narm )  {
		if( isprimitive ){
			return null ; // TODO :implement
		}
		
		Object biggest = null ;
		Object current ;
		boolean found = false ;
		
		if( dimensions.length == 1 ){
			return( max( (Object[])array, narm ) ) ;
		} else{
			
			/* need to iterate */
			while( hasNext() ){
				current = max( (Object[])next(), narm ) ;
				if( current == null ){
					if( !narm ) return null ;
				} else{
					if( !found ){
						biggest = current ;
						found = true ;
					} else if( bigger( current, biggest) ){ 
						biggest = current ;
					}					
				}
			}
			return biggest ;
		}
		
	}

	/** 
	 * Iterates over the array to find the range of the java array
	 * (in the sense of the Comparable interface)
	 */
	public Object[] range( boolean narm )  {
		if( isprimitive ){
			return null ; // TODO :implement
		}
		
		if( dimensions.length == 1 ){
			return( range( (Object[])array, narm ) ) ;
		} else{
			
			Object[] range = null ;
			Object[] current ;
			boolean found = false ;
		
			/* need to iterate */
			while( hasNext() ){
				current = range( (Object[])next(), narm ) ;
				if( current == null ){
					if( !narm ) return null ;
				} else{
					if( !found ){
						range = current ;
						found = true ;
					} else {
						if( bigger( current[1], range[1] ) ){
							range[1] = current[1] ;
						}
						if( smaller( current[0], range[0] ) ){
							range[0] = current[0] ;
						}	
						
					}
				}
			}
			return range ;
		}
		
	}

	
	
	
	/**
	 * returns the minimum (in the sense of Comparable) of the 
	 * objects in the one dimensioned array
	 */ 
	private static Object min( Object[] x, boolean narm ){
		
		int n = x.length ;
		Object smallest = null ; 
		Object current ;
		boolean found_min = false; 
		
		// find somewhere to start from ()
		for( int i=0; i<n; i++){
			current = x[i] ;
			if( current == null ){
				if( !narm ) return null ;
			} else{
				if( !found_min ){
					smallest = current ;
					found_min = true ;
				} else if( ((Comparable)smallest).compareTo(current) > 0 ) {
					smallest = current ;
				}
			}
		}
		return smallest ; 
		
	}
	
	/**
	 * returns the minimum (in the sense of Comparable) of the 
	 * objects in the one dimensioned array
	 */ 
	private static Object max( Object[] x, boolean narm ){
		
		int n = x.length ;
		Object biggest = null ; 
		Object current ;
		boolean found_min = false; 
		
		// find somewhere to start from ()
		for( int i=0; i<n; i++){
			current = x[i] ;
			if( current == null ){
				if( !narm ) return null ;
			} else{
				if( !found_min ){
					biggest = current ;
					found_min = true ;
				} else if( ((Comparable)biggest).compareTo(current) < 0 ) {
					biggest = current ;
				}
			}
		}
		return biggest ; 
		
	}

	/**
	 * returns the range (in the sense of Comparable) of the 
	 * objects in the one dimensioned array
	 */ 
	private static Object[] range( Object[] x, boolean narm ){
		
		int n = x.length ;
		Object[] range = (Object[])Array.newInstance( x.getClass().getComponentType(), 2) ; 
		Object current ;
		boolean found = false; 
		
		// find somewhere to start from ()
		for( int i=0; i<n; i++){
			current = x[i] ;
			if( current == null ){
				if( !narm ) return null ;
			} else{
				if( !found ){
					range[0] = current ;
					range[1] = current ;
					found = true ;
				} else {
					// max
					if( ((Comparable)range[1]).compareTo(current) < 0 ) {
						range[1] = current ;
					}
					// min
					if( ((Comparable)range[0]).compareTo(current) > 0 ) {
						range[0] = current ;
					}
					
				}
			}
		}
		return range ; 
		
	}
	
	public void checkComparableObjects() throws NotComparableException {
		if( ! containsComparableObjects() ) throw new NotComparableException( typeName ) ;
	}
	
	public boolean containsComparableObjects(){
		return Comparable.class.isAssignableFrom( componentType ) ;
	}
	
	// TODO : use these
	private static boolean smaller( Object x, Object y){
		return ( (Comparable)x ).compareTo(y) < 0 ;
	}
	private static boolean bigger( Object x, Object y){
		return ( (Comparable)x ).compareTo(y) > 0 ;
	}
	
	
}