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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
From: Ole Streicher <ole@aip.de>
Date: Fri, 30 Jul 2021 12:24:54 +0200
Subject: Temporarily add TestTableScheme
until we use a newer version of starlink-table-java
src/main/uk/ac/starlink/table/TestTableScheme.java | 456 +++++++++++++++++++++
1 file changed, 456 insertions(+)
create mode 100644 src/main/uk/ac/starlink/table/TestTableScheme.java
diff --git a/src/main/uk/ac/starlink/table/TestTableScheme.java b/src/main/uk/ac/starlink/table/TestTableScheme.java
new file mode 100644
index 0000000..6b4b119
@@ -0,0 +1,456 @@
+package uk.ac.starlink.table;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.IntFunction;
+import java.util.stream.Collectors;
+
+/**
+ * TableScheme implementation for test data.
+ *
+ * @author Mark Taylor
+ * @since 26 Apr 2021
+ */
+public class TestTableScheme implements TableScheme, Documented {
+
+ private static final int DFLT_NROW = 10;
+ private static final Map<Character,ContentOpt> OPTS = createOpts();
+ private static final String OPT_CHRS =
+ OPTS.keySet().stream().map( Object::toString )
+ .collect( Collectors.joining() );
+ private static final int ZP = 10;
+
+ public TestTableScheme() {
+ }
+
+ public String getSchemeName() {
+ return "test";
+ }
+
+ public String getSchemeUsage() {
+ return "[<nrow>[,<opts-" + OPT_CHRS + "*>]]";
+ }
+
+ public String getExampleSpecification() {
+ return "10,is";
+ }
+
+ public String getXmlDescription() {
+ return String.join( "\n",
+ "<p>Generates a table containing test data.",
+ "The idea is to include columns of different data types,",
+ "for instance to provide an example for testing",
+ "I/O handler implementations.",
+ "The columns will contain some variety of more or less meaningless",
+ "values, but the content is reproducible between runs,",
+ "so the same specification will produce the same output each time.",
+ "Updates of the implementation might change the output however,",
+ "so the output is not guaranteed to be the same for all time.",
+ "</p>",
+ "<p>The table specification has two comma-separated parameters:",
+ "<ul>",
+ "<li><code><nrow></code>: row count</li>",
+ "<li><code><opts></code>:",
+ "a string of letter options specifying what types of data",
+ "will be included; options are:",
+ "<ul>",
+ OPTS.values().stream().map( opt -> new StringBuffer()
+ .append( "<li><strong>" )
+ .append( opt.idChr_ )
+ .append( "</strong>: " )
+ .append( opt.description_ )
+ .append( "</li>\n" )
+ .toString()
+ ).collect( Collectors.joining( "\n" ) ),
+ "<li><strong>*</strong>: equivalent to all of the above</li>",
+ "</ul>",
+ "</li>",
+ "</ul>",
+ "If <code><opts></code> and/or <code><nrow></code>",
+ "are omitted, some default values are used.",
+ "</p>",
+ "" );
+ }
+
+ public StarTable createTable( String spec ) throws TableFormatException {
+ String[] args = spec.split( ",", -1 );
+ if ( args.length > 2 ) {
+ throw new TableFormatException( "Too many args" );
+ }
+ final int nrow;
+ try {
+ nrow = args.length > 0 && args[ 0 ].trim().length() > 0
+ ? Integer.parseInt( args[ 0 ].trim() )
+ : DFLT_NROW;
+ }
+ catch ( NumberFormatException e ) {
+ throw new TableFormatException( "Bad row count \""
+ + args[ 0 ] + "\"" );
+ }
+ String optsTxt = args.length > 1
+ ? args[ 1 ]
+ : "b";
+ List<ContentOpt> optList = new ArrayList<>();
+ for ( int ic = 0; ic < optsTxt.length(); ic++ ) {
+ Character chr = Character.valueOf( optsTxt.charAt( ic ) );
+ if ( chr == '*' ) {
+ optList.addAll( OPTS.values() );
+ }
+ else {
+ ContentOpt opt = OPTS.get( chr );
+ if ( opt == null ) {
+ throw new TableFormatException( "Unknown content option '"
+ + chr + "' - must be one of ["
+ + OPT_CHRS + "*]" );
+ }
+ optList.add( opt );
+ }
+ }
+ ColumnStarTable table = new ColumnStarTable() {
+ public long getRowCount() {
+ return nrow;
+ }
+ };
+ for ( ContentOpt opt : optList ) {
+ opt.addContent_.accept( table );
+ }
+ return table;
+ }
+
+ /**
+ * Creates the list of run-time options for table content.
+ *
+ * @param map from identifier character to option
+ */
+ private static Map<Character,ContentOpt> createOpts() {
+ ContentOpt[] opts = new ContentOpt[] {
+ new ContentOpt( 'i', "index", "an integer index column", t -> {
+ t.addColumn( new ColumnData( new ColumnInfo( "i_index",
+ Integer.class,
+ "Row index" ) ) {
+ public Object readValue( long irow ) {
+ return Integer.valueOf( (int) irow );
+ }
+ } );
+ } ),
+ new ContentOpt( 'b', "basic", "a few basic columns", t -> {
+ int nPhase = 1;
+ addColumn( t, "b_int", Integer.class, nPhase++,
+ i -> Integer.valueOf( i ) );
+ addColumn( t, "b_double", Double.class, nPhase++,
+ i -> Double.valueOf( i ) );
+ addColumn( t, "b_string", String.class, nPhase++,
+ i -> valString( i, false ) );
+ } ),
+ new ContentOpt( 's', "scalars",
+ "a selection of typed scalar columns", t -> {
+ int nPhase = 1;
+ addColumn( t, "s_byte", Byte.class, nPhase++,
+ i -> Byte.valueOf( (byte) valInt( i ) ) );
+ addColumn( t, "s_short", Short.class, nPhase++,
+ i -> Short.valueOf( (short) valInt( i ) ) );
+ addColumn( t, "s_int", Integer.class, nPhase++,
+ i -> Integer.valueOf( valInt( i ) ) );
+ addColumn( t, "s_long", Long.class, nPhase++,
+ i -> Long.valueOf( valInt( i ) ) );
+ addColumn( t, "s_float", Float.class, nPhase++,
+ i -> Float.valueOf( (float) valDouble( i ) ) );
+ addColumn( t, "s_double", Double.class, nPhase++,
+ i -> Double.valueOf( valDouble( i ) ) );
+ addColumn( t, "s_string", String.class, nPhase++,
+ i -> valString( i, true ) );
+ addColumn( t, "s_boolean", Boolean.class, nPhase++,
+ i -> Boolean.valueOf( i % 2 == 1 ) );
+ } ),
+ new ContentOpt( 'f', "fixed-vectors",
+ "a selection of fixed-length 1-d array columns",
+ t -> {
+ int nPhase = 1;
+ int[] s3 = new int[] { 3 };
+ addShapeColumn( t, "f_byte", byte[].class, s3, nPhase++,
+ i -> new byte[] { (byte) ( i + 0 ),
+ (byte) ( i + 1 ),
+ (byte) ( i + 2 ) } );
+ addShapeColumn( t, "f_short", short[].class, s3, nPhase++,
+ i -> new short[] { (short) ( i + 0 ),
+ (short) ( i + 1 ),
+ (short) ( i + 2 ) } );
+ addShapeColumn( t, "f_int", int[].class, s3, nPhase++,
+ i -> new int[] { i + 0, i + 1, i + 2 } );
+ addShapeColumn( t, "f_long", long[].class, s3, nPhase++,
+ i -> new long[] { i + 0, i + 1, i + 2 } );
+ addShapeColumn( t, "f_float", float[].class, s3, nPhase++,
+ i -> new float[] { i, Float.NaN, i + 2.5f } );
+ addShapeColumn( t, "f_double", double[].class, s3, nPhase++,
+ i -> new double[] { i, Double.NaN, i + 2.5 } );
+ addShapeColumn( t, "f_string", String[].class, s3, nPhase++,
+ i -> new String[] { "foo", null,
+ valString( i, true ) } );
+ addShapeColumn( t, "f_boolean", boolean[].class, s3, nPhase++,
+ i -> new boolean[] { (i/1)%2==1,
+ (i/2)%2==1,
+ (i/4)%2==1 } );
+ } ),
+ new ContentOpt( 'v', "var-vectors",
+ "a selection of variable-length 1-d array columns",
+ t -> {
+ int nPhase = 1;
+ addVarColumn( t, "v_byte", byte[].class, nPhase++,
+ i -> new byte[] { (byte) ( i + 0 ),
+ (byte) ( i + 1 ),
+ (byte) ( i + 2 ) } );
+ addVarColumn( t, "v_short", short[].class, nPhase++,
+ i -> new short[] { (short) ( i + 0 ),
+ (short) ( i + 1 ),
+ (short) ( i + 2 ) } );
+ addVarColumn( t, "v_int", int[].class, nPhase++,
+ i -> new int[] { i + 0, i + 1, i + 2 } );
+ addVarColumn( t, "v_long", long[].class, nPhase++,
+ i -> new long[] { i + 0, i + 1, i + 2 } );
+ addVarColumn( t, "v_float", float[].class, nPhase++,
+ i -> new float[] { i + 0, Float.NaN, i + 2.5f } );
+ addVarColumn( t, "v_double", double[].class, nPhase++,
+ i -> new double[] { i + 0, Double.NaN, i + 2.5 });
+ addVarColumn( t, "v_string", String[].class, nPhase++,
+ i -> new String[] { "foo", null,
+ valString( i, true ) } );
+ addVarColumn( t, "v_boolean", boolean[].class, nPhase++,
+ i -> new boolean[] { (i/1)%2==1,
+ (i/2)%2==1,
+ (i/4)%2==1 } );
+ } ),
+ new ContentOpt( 'm', "multi-d arrays",
+ "a selection of multi-dimensional array columns",
+ t -> {
+ int nPhase = 1;
+ addShapeColumn( t, "m_int", int[].class, new int[] { 2, 4 },
+ nPhase++,
+ i -> new int[] { 1000 + i, 1001 + i,
+ 2000 + i, 2001 + i,
+ 3000 + i, 3001 + i,
+ 4000 + i, 4001 + i, } );
+ addShapeColumn( t, "m_double", double[].class,
+ new int[] { 3, 2 }, nPhase++,
+ i -> new double[] {
+ i + .25, i + .50, i + .75,
+ - i - .25, - i - .50, - i - .75,
+ } );
+ } ),
+ new ContentOpt( 'k', "kilo-column", "almost a thousand columns",
+ t -> {
+ for ( int j = 0; j < 995; j++ ) {
+ addColumn( t, "k_" + (j + 1), Integer.class, 0,
+ i -> Integer.valueOf( valInt( i ) ) );
+ }
+ } ),
+ };
+ Map<Character,ContentOpt> map = new LinkedHashMap<>();
+ for ( ContentOpt opt : opts ) {
+ char chr = Character.valueOf( opt.idChr_ );
+ assert ! map.containsKey( chr );
+ map.put( chr, opt );
+ }
+ return Collections.unmodifiableMap( map );
+ }
+
+ /**
+ * Gives a nominal numeric integer value for a given row index.
+ *
+ * @param ix row index
+ * @return integer numeric value
+ */
+ private static int valInt( int ix ) {
+ return ix * ( ( ( ix / 10 ) % 2 == 0 ) ? +1 : -1 );
+ }
+
+ /**
+ * Gives a nominal floating point value for a given row index.
+ *
+ * @param ix row index
+ * @return floating point numeric value
+ */
+ private static double valDouble( int ix ) {
+ return isBlank( ix ) ? Double.NaN : valInt( ix );
+ }
+
+ /**
+ * Gives a nominal string value for a given row index.
+ *
+ * @param ix row index
+ * @param isTricky true to include some specially weird strings
+ * @return string value
+ */
+ private static String valString( int ix, boolean isTricky ) {
+ if ( isBlank( ix ) ) {
+ return "";
+ }
+ else if ( isTricky && isFunny( ix ) ) {
+ return "' \"\\\"\"' ; '&<>";
+ }
+ else if ( ix == 0 ) {
+ return "zero";
+ }
+ int u = ix % 10;
+ int t = ( ix / 10 ) * 10;
+ int v = ix / 10;
+ StringBuffer sbuf = new StringBuffer();
+ String[] qpairs = { null, "\"\"", "''", "<>", "()", "{}", "[]", ",;" };
+ String qpair = isTricky ? qpairs[ v % qpairs.length ] : null;
+ final String preQuote = qpair == null ? "" : qpair.substring( 0, 1 );
+ final String postQuote = qpair == null ? "" : qpair.substring( 1, 2 );
+ if ( t != 0 ) {
+ sbuf.append( Integer.toString( t ) );
+ if ( u != 0 ) {
+ sbuf.append( isTricky ? " + " : "+" );
+ }
+ }
+ if ( u != 0 ) {
+ sbuf.append( preQuote )
+ .append( (new String[] {
+ "zero", "one", "two", "three", "four",
+ "five", "six", "seven", "eight", "nine",
+ })[ u ] )
+ .append( postQuote );
+ }
+ return sbuf.toString();
+ }
+
+ /**
+ * Utility function that indicates whether a value at a given index
+ * should be set to a non-null blank value.
+ *
+ * @param ix row index
+ * @return true for blank
+ */
+ private static boolean isBlank( int ix ) {
+ return ix % 10 == 9;
+ }
+
+ /**
+ * Utility function that indicates whether a value at a given index
+ * should be set to a test value out of the normal sequence.
+ *
+ * @param ix row index
+ * @return true for funny value
+ */
+ private static boolean isFunny( int ix ) {
+ return ix % 10 == 8;
+ }
+
+ /**
+ * Adds a column to a table given a value mapping from row index.
+ *
+ * @param table table to add column to
+ * @param name column name
+ * @param clazz cell content class
+ * @param nullPhase row phase at which nulls should be substituted
+ * @param cellData produces basic value for given row index
+ */
+ private static <T> void addColumn( ColumnStarTable table,
+ String name, Class<T> clazz,
+ int nullPhase,
+ IntFunction<T> cellData ) {
+ table.addColumn( new ColumnData( new ColumnInfo( name, clazz, null ) ) {
+ public T readValue( long irow ) {
+ return irow % ZP == nullPhase ? null
+ : cellData.apply( (int) irow );
+ }
+ } );
+ }
+
+ /**
+ * Adds a column to a table with a supplied array shape.
+ * No checking is done to ensure that the shape makes sense.
+ *
+ * @param table table to add column to
+ * @param name column name
+ * @param clazz cell content class
+ * @param shape array shape
+ * @param nullPhase row phase at which nulls should be substituted
+ * @param cellData produces basic value for given row index
+ */
+ private static <T> void addShapeColumn( ColumnStarTable table,
+ String name, Class<T> clazz,
+ int[] shape, int nullPhase,
+ IntFunction<T> cellData ) {
+ addColumn( table, name, clazz, nullPhase, cellData );
+ table.getColumnInfo( table.getColumnCount() - 1 )
+ .setShape( shape );
+ }
+
+ /**
+ * Adds a variable-length column to a table given a value mapping
+ * from row index to a fixed-length array value.
+ *
+ * @param table table to add column to
+ * @param name column name
+ * @param clazz cell content class, an array type
+ * @param nullPhase row phase at which nulls should be substituted
+ * @param cellData produces basic array value for given row index
+ */
+ private static <T> void addVarColumn( ColumnStarTable table,
+ String name, Class<T> clazz,
+ int nullPhase,
+ IntFunction<T> cellData ) {
+ final Class<?> elClazz = clazz.getComponentType();
+ table.addColumn( new ColumnData( new ColumnInfo( name, clazz, null ) ) {
+ public T readValue( long irow ) {
+ if ( irow % ZP == nullPhase ) {
+ return null;
+ }
+ else {
+ T value0 = cellData.apply( (int) irow );
+ if ( value0 != null ) {
+ int leng0 = Array.getLength( value0 );
+ int leng1 = leng0 - ( ((int) irow) % (leng0 + 1) );
+ if ( leng1 == leng0 ) {
+ return value0;
+ }
+ else {
+ T value1 =
+ clazz.cast( Array.newInstance( elClazz,
+ leng1 ) );
+ for ( int i = 0; i < leng1; i++ ) {
+ Array.set( value1, i, Array.get( value0, i ) );
+ }
+ return value1;
+ }
+ }
+ else {
+ return value0;
+ }
+ }
+ }
+ } );
+ }
+
+ /**
+ * Defines an output table content option.
+ */
+ private static class ContentOpt {
+ final char idChr_;
+ final String name_;
+ final String description_;
+ final Consumer<ColumnStarTable> addContent_;
+
+ /**
+ * @param idChr identifier character
+ * @param name option name
+ * @param description user-oriented short description
+ * @param addContent adds content to table
+ */
+ ContentOpt( char idChr, String name, String description,
+ Consumer<ColumnStarTable> addContent ) {
+ idChr_ = idChr;
+ name_ = name;
+ description_ = description;
+ addContent_ = addContent;
+ }
+ }
+}
|