Tue, 25 May 2010 15:52:11 -0700
6943119: Rebrand source copyright notices
Reviewed-by: darcy
duke@1 | 1 | /* |
ohair@158 | 2 | * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@158 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@158 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@158 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@158 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@158 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | package com.sun.corba.se.spi.orb ; |
duke@1 | 26 | |
duke@1 | 27 | import java.util.StringTokenizer ; |
duke@1 | 28 | |
duke@1 | 29 | import java.lang.reflect.Array ; |
duke@1 | 30 | |
duke@1 | 31 | import java.net.URL ; |
duke@1 | 32 | import java.net.MalformedURLException ; |
duke@1 | 33 | |
duke@1 | 34 | import com.sun.corba.se.spi.logging.CORBALogDomains ; |
duke@1 | 35 | |
duke@1 | 36 | import com.sun.corba.se.impl.logging.ORBUtilSystemException ; |
duke@1 | 37 | import com.sun.corba.se.impl.orbutil.ORBClassLoader ; |
duke@1 | 38 | import com.sun.corba.se.impl.orbutil.ObjectUtility ; |
duke@1 | 39 | |
duke@1 | 40 | /** This is a static factory class for commonly used operations |
duke@1 | 41 | * for property parsing. The following operations are supported: |
duke@1 | 42 | * <ul> |
duke@1 | 43 | * <li>maskErrorAction( Operation op ) executes op and returns the result. If op throws an |
duke@1 | 44 | * exception, the result is null. |
duke@1 | 45 | * <li>indexAction( int arg ) returns the [arg] element of value, which must be an Object[]</li> |
duke@1 | 46 | * <li>identityAction() return the value</li> |
duke@1 | 47 | * <li>booleanAction() return a Boolean representing true or false values of the String value</li> |
duke@1 | 48 | * <li>integerAction() returns an Integer for the String value, which must be a decimal integer</li> |
duke@1 | 49 | * <li>stringAction() returns the String value</li> |
duke@1 | 50 | * <li>classAction() returns a class for the String value, as loaded by the ORB classloader</li> |
duke@1 | 51 | * <li>setFlagAction() always return Boolean.TRUE</li> |
duke@1 | 52 | * <li>URLAction() returns a java.net.URL for the String value, which must be a valid URL</li> |
duke@1 | 53 | * <li>integerRangeAction( int min, int max ) returns an Integer for the String value, which must be a |
duke@1 | 54 | * decimal integer in the range min to max inclusive</li> |
duke@1 | 55 | * <li>listAction( String sep, Operation ) tokenizes the String value with sep as separator, then |
duke@1 | 56 | * applies the Operation to each token, and returns an array of the result</li> |
duke@1 | 57 | * <li>sequenceAction( String, Operation[] ) tokenizes the String value with sep as separator, then |
duke@1 | 58 | * applies each Operation in the Operation array to successive tokens, and returns an array of the results</li> |
duke@1 | 59 | * <li>compose( Operation op1, Operation op2 ) is the operation that applies op2 to the result of applying |
duke@1 | 60 | * op1 to the value</li> |
duke@1 | 61 | * <li>mapAction( Operation ) applies the Operation to each element of an array of objects, and returns |
duke@1 | 62 | * an array of the results</li> |
duke@1 | 63 | * <li>mapSequenceAction( Operation[] ) applies the corresponding element of the Operation array to an |
duke@1 | 64 | * element of the Object[] value, and returns an array of the results</li> |
duke@1 | 65 | * <li>convertIntegerToShort coerces an Integer into a Short.</li> |
duke@1 | 66 | * </ul> |
duke@1 | 67 | * Other operations can be directly defined, and combined using these basic operations. |
duke@1 | 68 | */ |
duke@1 | 69 | public abstract class OperationFactory { |
duke@1 | 70 | private OperationFactory() {} |
duke@1 | 71 | |
duke@1 | 72 | private static String getString( Object obj ) |
duke@1 | 73 | { |
duke@1 | 74 | if (obj instanceof String) |
duke@1 | 75 | return (String)obj ; |
duke@1 | 76 | else |
duke@1 | 77 | throw new Error( "String expected" ) ; |
duke@1 | 78 | } |
duke@1 | 79 | |
duke@1 | 80 | private static Object[] getObjectArray( Object obj ) |
duke@1 | 81 | { |
duke@1 | 82 | if (obj instanceof Object[]) |
duke@1 | 83 | return (Object[])obj ; |
duke@1 | 84 | else |
duke@1 | 85 | throw new Error( "Object[] expected" ) ; |
duke@1 | 86 | } |
duke@1 | 87 | |
duke@1 | 88 | private static StringPair getStringPair( Object obj ) |
duke@1 | 89 | { |
duke@1 | 90 | if (obj instanceof StringPair) |
duke@1 | 91 | return (StringPair)obj ; |
duke@1 | 92 | else |
duke@1 | 93 | throw new Error( "StringPair expected" ) ; |
duke@1 | 94 | } |
duke@1 | 95 | |
duke@1 | 96 | private static abstract class OperationBase implements Operation{ |
duke@1 | 97 | public boolean equals( Object obj ) |
duke@1 | 98 | { |
duke@1 | 99 | if (this==obj) |
duke@1 | 100 | return true ; |
duke@1 | 101 | |
duke@1 | 102 | if (!(obj instanceof OperationBase)) |
duke@1 | 103 | return false ; |
duke@1 | 104 | |
duke@1 | 105 | OperationBase other = (OperationBase)obj ; |
duke@1 | 106 | |
duke@1 | 107 | return toString().equals( other.toString() ) ; |
duke@1 | 108 | } |
duke@1 | 109 | |
duke@1 | 110 | public int hashCode() |
duke@1 | 111 | { |
duke@1 | 112 | return toString().hashCode() ; |
duke@1 | 113 | } |
duke@1 | 114 | } |
duke@1 | 115 | |
duke@1 | 116 | private static class MaskErrorAction extends OperationBase |
duke@1 | 117 | { |
duke@1 | 118 | private Operation op ; |
duke@1 | 119 | |
duke@1 | 120 | public MaskErrorAction( Operation op ) |
duke@1 | 121 | { |
duke@1 | 122 | this.op = op ; |
duke@1 | 123 | } |
duke@1 | 124 | |
duke@1 | 125 | public Object operate( Object arg ) |
duke@1 | 126 | { |
duke@1 | 127 | try { |
duke@1 | 128 | return op.operate( arg ) ; |
duke@1 | 129 | } catch (java.lang.Exception exc) { |
duke@1 | 130 | return null ; |
duke@1 | 131 | } |
duke@1 | 132 | } |
duke@1 | 133 | |
duke@1 | 134 | public String toString() |
duke@1 | 135 | { |
duke@1 | 136 | return "maskErrorAction(" + op + ")" ; |
duke@1 | 137 | } |
duke@1 | 138 | } |
duke@1 | 139 | |
duke@1 | 140 | public static Operation maskErrorAction( Operation op ) |
duke@1 | 141 | { |
duke@1 | 142 | return new MaskErrorAction( op ) ; |
duke@1 | 143 | } |
duke@1 | 144 | |
duke@1 | 145 | private static class IndexAction extends OperationBase |
duke@1 | 146 | { |
duke@1 | 147 | private int index ; |
duke@1 | 148 | |
duke@1 | 149 | public IndexAction( int index ) |
duke@1 | 150 | { |
duke@1 | 151 | this.index = index ; |
duke@1 | 152 | } |
duke@1 | 153 | |
duke@1 | 154 | public Object operate( Object value ) |
duke@1 | 155 | { |
duke@1 | 156 | return getObjectArray( value )[ index ] ; |
duke@1 | 157 | } |
duke@1 | 158 | |
duke@1 | 159 | public String toString() |
duke@1 | 160 | { |
duke@1 | 161 | return "indexAction(" + index + ")" ; |
duke@1 | 162 | } |
duke@1 | 163 | } |
duke@1 | 164 | |
duke@1 | 165 | public static Operation indexAction( int index ) |
duke@1 | 166 | { |
duke@1 | 167 | return new IndexAction( index ) ; |
duke@1 | 168 | } |
duke@1 | 169 | |
duke@1 | 170 | private static class SuffixAction extends OperationBase |
duke@1 | 171 | { |
duke@1 | 172 | public Object operate( Object value ) |
duke@1 | 173 | { |
duke@1 | 174 | return getStringPair( value ).getFirst() ; |
duke@1 | 175 | } |
duke@1 | 176 | |
duke@1 | 177 | public String toString() { return "suffixAction" ; } |
duke@1 | 178 | } |
duke@1 | 179 | |
duke@1 | 180 | private static Operation suffixActionImpl = new SuffixAction() ; |
duke@1 | 181 | |
duke@1 | 182 | private static class ValueAction extends OperationBase |
duke@1 | 183 | { |
duke@1 | 184 | public Object operate( Object value ) |
duke@1 | 185 | { |
duke@1 | 186 | return getStringPair( value ).getSecond() ; |
duke@1 | 187 | } |
duke@1 | 188 | |
duke@1 | 189 | public String toString() { return "valueAction" ; } |
duke@1 | 190 | } |
duke@1 | 191 | |
duke@1 | 192 | private static Operation valueActionImpl = new ValueAction() ; |
duke@1 | 193 | |
duke@1 | 194 | private static class IdentityAction extends OperationBase |
duke@1 | 195 | { |
duke@1 | 196 | public Object operate( Object value ) |
duke@1 | 197 | { |
duke@1 | 198 | return value ; |
duke@1 | 199 | } |
duke@1 | 200 | |
duke@1 | 201 | public String toString() { return "identityAction" ; } |
duke@1 | 202 | } |
duke@1 | 203 | |
duke@1 | 204 | private static Operation identityActionImpl = new IdentityAction() ; |
duke@1 | 205 | |
duke@1 | 206 | private static class BooleanAction extends OperationBase |
duke@1 | 207 | { |
duke@1 | 208 | public Object operate( Object value ) |
duke@1 | 209 | { |
duke@1 | 210 | return new Boolean( getString( value ) ) ; |
duke@1 | 211 | } |
duke@1 | 212 | |
duke@1 | 213 | public String toString() { return "booleanAction" ; } |
duke@1 | 214 | } |
duke@1 | 215 | |
duke@1 | 216 | private static Operation booleanActionImpl = new BooleanAction() ; |
duke@1 | 217 | |
duke@1 | 218 | private static class IntegerAction extends OperationBase |
duke@1 | 219 | { |
duke@1 | 220 | public Object operate( Object value ) |
duke@1 | 221 | { |
duke@1 | 222 | return new Integer( getString( value ) ) ; |
duke@1 | 223 | } |
duke@1 | 224 | |
duke@1 | 225 | public String toString() { return "integerAction" ; } |
duke@1 | 226 | } |
duke@1 | 227 | |
duke@1 | 228 | private static Operation integerActionImpl = new IntegerAction() ; |
duke@1 | 229 | |
duke@1 | 230 | private static class StringAction extends OperationBase |
duke@1 | 231 | { |
duke@1 | 232 | public Object operate( Object value ) |
duke@1 | 233 | { |
duke@1 | 234 | return value ; |
duke@1 | 235 | } |
duke@1 | 236 | |
duke@1 | 237 | public String toString() { return "stringAction" ; } |
duke@1 | 238 | } |
duke@1 | 239 | |
duke@1 | 240 | private static Operation stringActionImpl = new StringAction() ; |
duke@1 | 241 | |
duke@1 | 242 | private static class ClassAction extends OperationBase |
duke@1 | 243 | { |
duke@1 | 244 | public Object operate( Object value ) |
duke@1 | 245 | { |
duke@1 | 246 | String className = getString( value ) ; |
duke@1 | 247 | |
duke@1 | 248 | try { |
duke@1 | 249 | Class result = ORBClassLoader.loadClass( className ) ; |
duke@1 | 250 | return result ; |
duke@1 | 251 | } catch (Exception exc) { |
duke@1 | 252 | ORBUtilSystemException wrapper = ORBUtilSystemException.get( |
duke@1 | 253 | CORBALogDomains.ORB_LIFECYCLE ) ; |
duke@1 | 254 | throw wrapper.couldNotLoadClass( exc, className ) ; |
duke@1 | 255 | } |
duke@1 | 256 | } |
duke@1 | 257 | |
duke@1 | 258 | public String toString() { return "classAction" ; } |
duke@1 | 259 | } |
duke@1 | 260 | |
duke@1 | 261 | private static Operation classActionImpl = new ClassAction() ; |
duke@1 | 262 | |
duke@1 | 263 | private static class SetFlagAction extends OperationBase |
duke@1 | 264 | { |
duke@1 | 265 | public Object operate( Object value ) |
duke@1 | 266 | { |
duke@1 | 267 | return Boolean.TRUE ; |
duke@1 | 268 | } |
duke@1 | 269 | |
duke@1 | 270 | public String toString() { return "setFlagAction" ; } |
duke@1 | 271 | } |
duke@1 | 272 | |
duke@1 | 273 | private static Operation setFlagActionImpl = new SetFlagAction() ; |
duke@1 | 274 | |
duke@1 | 275 | private static class URLAction extends OperationBase |
duke@1 | 276 | { |
duke@1 | 277 | public Object operate( Object value ) |
duke@1 | 278 | { |
duke@1 | 279 | String val = (String)value ; |
duke@1 | 280 | try { |
duke@1 | 281 | return new URL( val ) ; |
duke@1 | 282 | } catch (MalformedURLException exc) { |
duke@1 | 283 | ORBUtilSystemException wrapper = ORBUtilSystemException.get( |
duke@1 | 284 | CORBALogDomains.ORB_LIFECYCLE ) ; |
duke@1 | 285 | throw wrapper.badUrl( exc, val ) ; |
duke@1 | 286 | } |
duke@1 | 287 | } |
duke@1 | 288 | |
duke@1 | 289 | public String toString() { return "URLAction" ; } |
duke@1 | 290 | } |
duke@1 | 291 | |
duke@1 | 292 | private static Operation URLActionImpl = new URLAction() ; |
duke@1 | 293 | |
duke@1 | 294 | public static Operation identityAction() |
duke@1 | 295 | { |
duke@1 | 296 | return identityActionImpl ; |
duke@1 | 297 | } |
duke@1 | 298 | |
duke@1 | 299 | public static Operation suffixAction() |
duke@1 | 300 | { |
duke@1 | 301 | return suffixActionImpl ; |
duke@1 | 302 | } |
duke@1 | 303 | |
duke@1 | 304 | public static Operation valueAction() |
duke@1 | 305 | { |
duke@1 | 306 | return valueActionImpl ; |
duke@1 | 307 | } |
duke@1 | 308 | |
duke@1 | 309 | public static Operation booleanAction() |
duke@1 | 310 | { |
duke@1 | 311 | return booleanActionImpl ; |
duke@1 | 312 | } |
duke@1 | 313 | |
duke@1 | 314 | public static Operation integerAction() |
duke@1 | 315 | { |
duke@1 | 316 | return integerActionImpl ; |
duke@1 | 317 | } |
duke@1 | 318 | |
duke@1 | 319 | public static Operation stringAction() |
duke@1 | 320 | { |
duke@1 | 321 | return stringActionImpl ; |
duke@1 | 322 | } |
duke@1 | 323 | |
duke@1 | 324 | public static Operation classAction() |
duke@1 | 325 | { |
duke@1 | 326 | return classActionImpl ; |
duke@1 | 327 | } |
duke@1 | 328 | |
duke@1 | 329 | public static Operation setFlagAction() |
duke@1 | 330 | { |
duke@1 | 331 | return setFlagActionImpl ; |
duke@1 | 332 | } |
duke@1 | 333 | |
duke@1 | 334 | public static Operation URLAction() |
duke@1 | 335 | { |
duke@1 | 336 | return URLActionImpl ; |
duke@1 | 337 | } |
duke@1 | 338 | |
duke@1 | 339 | private static class IntegerRangeAction extends OperationBase |
duke@1 | 340 | { |
duke@1 | 341 | private int min ; |
duke@1 | 342 | private int max ; |
duke@1 | 343 | |
duke@1 | 344 | IntegerRangeAction( int min, int max ) |
duke@1 | 345 | { |
duke@1 | 346 | this.min = min ; |
duke@1 | 347 | this.max = max ; |
duke@1 | 348 | } |
duke@1 | 349 | |
duke@1 | 350 | public Object operate( Object value ) |
duke@1 | 351 | { |
duke@1 | 352 | int result = Integer.parseInt( getString( value ) ) ; |
duke@1 | 353 | if ((result >= min) && (result <= max)) |
duke@1 | 354 | return new Integer( result ) ; |
duke@1 | 355 | else |
duke@1 | 356 | throw new IllegalArgumentException( |
duke@1 | 357 | "Property value " + result + " is not in the range " + |
duke@1 | 358 | min + " to " + max ) ; |
duke@1 | 359 | } |
duke@1 | 360 | |
duke@1 | 361 | public String toString() { |
duke@1 | 362 | return "integerRangeAction(" + min + "," + max + ")" ; |
duke@1 | 363 | } |
duke@1 | 364 | } |
duke@1 | 365 | |
duke@1 | 366 | public static Operation integerRangeAction( int min, int max ) |
duke@1 | 367 | { |
duke@1 | 368 | return new IntegerRangeAction( min, max ) ; |
duke@1 | 369 | } |
duke@1 | 370 | |
duke@1 | 371 | private static class ListAction extends OperationBase { |
duke@1 | 372 | private String sep ; |
duke@1 | 373 | private Operation act ; |
duke@1 | 374 | |
duke@1 | 375 | ListAction( String sep, Operation act ) |
duke@1 | 376 | { |
duke@1 | 377 | this.sep = sep ; |
duke@1 | 378 | this.act = act ; |
duke@1 | 379 | } |
duke@1 | 380 | |
duke@1 | 381 | // Note that this method carefully constructs an array of the type |
duke@1 | 382 | // of the first result, rather than just using Object[], which is |
duke@1 | 383 | // not convertible into the correct type. Also note that no tokens |
duke@1 | 384 | // results in a null result. |
duke@1 | 385 | public Object operate( Object value ) |
duke@1 | 386 | { |
duke@1 | 387 | StringTokenizer st = new StringTokenizer( getString( value ), |
duke@1 | 388 | sep ) ; |
duke@1 | 389 | int length = st.countTokens() ; |
duke@1 | 390 | Object result = null ; |
duke@1 | 391 | int ctr = 0 ; |
duke@1 | 392 | while (st.hasMoreTokens()) { |
duke@1 | 393 | String next = st.nextToken() ; |
duke@1 | 394 | Object val = act.operate( next ) ; |
duke@1 | 395 | if (result == null) |
duke@1 | 396 | result = Array.newInstance( val.getClass(), length ) ; |
duke@1 | 397 | Array.set( result, ctr++, val ) ; |
duke@1 | 398 | } |
duke@1 | 399 | |
duke@1 | 400 | return result ; |
duke@1 | 401 | } |
duke@1 | 402 | |
duke@1 | 403 | public String toString() { |
duke@1 | 404 | return "listAction(separator=\"" + sep + |
duke@1 | 405 | "\",action=" + act + ")" ; |
duke@1 | 406 | } |
duke@1 | 407 | } |
duke@1 | 408 | |
duke@1 | 409 | public static Operation listAction( String sep, Operation act ) |
duke@1 | 410 | { |
duke@1 | 411 | return new ListAction( sep, act ) ; |
duke@1 | 412 | } |
duke@1 | 413 | |
duke@1 | 414 | private static class SequenceAction extends OperationBase |
duke@1 | 415 | { |
duke@1 | 416 | private String sep ; |
duke@1 | 417 | private Operation[] actions ; |
duke@1 | 418 | |
duke@1 | 419 | SequenceAction( String sep, Operation[] actions ) |
duke@1 | 420 | { |
duke@1 | 421 | this.sep = sep ; |
duke@1 | 422 | this.actions = actions ; |
duke@1 | 423 | } |
duke@1 | 424 | |
duke@1 | 425 | public Object operate( Object value ) |
duke@1 | 426 | { |
duke@1 | 427 | StringTokenizer st = new StringTokenizer( getString( value ), |
duke@1 | 428 | sep ) ; |
duke@1 | 429 | |
duke@1 | 430 | int numTokens = st.countTokens() ; |
duke@1 | 431 | if (numTokens != actions.length) |
duke@1 | 432 | throw new Error( |
duke@1 | 433 | "Number of tokens and number of actions do not match" ) ; |
duke@1 | 434 | |
duke@1 | 435 | int ctr = 0 ; |
duke@1 | 436 | Object[] result = new Object[ numTokens ] ; |
duke@1 | 437 | while (st.hasMoreTokens()) { |
duke@1 | 438 | Operation act = actions[ctr] ; |
duke@1 | 439 | String next = st.nextToken() ; |
duke@1 | 440 | result[ctr++] = act.operate( next ) ; |
duke@1 | 441 | } |
duke@1 | 442 | |
duke@1 | 443 | return result ; |
duke@1 | 444 | } |
duke@1 | 445 | |
duke@1 | 446 | public String toString() { |
duke@1 | 447 | return "sequenceAction(separator=\"" + sep + |
duke@1 | 448 | "\",actions=" + |
duke@1 | 449 | ObjectUtility.compactObjectToString(actions) + ")" ; |
duke@1 | 450 | } |
duke@1 | 451 | } |
duke@1 | 452 | |
duke@1 | 453 | public static Operation sequenceAction( String sep, |
duke@1 | 454 | Operation[] actions ) |
duke@1 | 455 | { |
duke@1 | 456 | return new SequenceAction( sep, actions ) ; |
duke@1 | 457 | } |
duke@1 | 458 | |
duke@1 | 459 | private static class ComposeAction extends OperationBase |
duke@1 | 460 | { |
duke@1 | 461 | private Operation op1 ; |
duke@1 | 462 | private Operation op2 ; |
duke@1 | 463 | |
duke@1 | 464 | ComposeAction( Operation op1, Operation op2 ) |
duke@1 | 465 | { |
duke@1 | 466 | this.op1 = op1 ; |
duke@1 | 467 | this.op2 = op2 ; |
duke@1 | 468 | } |
duke@1 | 469 | |
duke@1 | 470 | public Object operate( Object value ) |
duke@1 | 471 | { |
duke@1 | 472 | return op2.operate( op1.operate( value ) ) ; |
duke@1 | 473 | } |
duke@1 | 474 | |
duke@1 | 475 | public String toString() { |
duke@1 | 476 | return "composition(" + op1 + "," + op2 + ")" ; |
duke@1 | 477 | } |
duke@1 | 478 | } |
duke@1 | 479 | |
duke@1 | 480 | public static Operation compose( Operation op1, Operation op2 ) |
duke@1 | 481 | { |
duke@1 | 482 | return new ComposeAction( op1, op2 ) ; |
duke@1 | 483 | } |
duke@1 | 484 | |
duke@1 | 485 | private static class MapAction extends OperationBase |
duke@1 | 486 | { |
duke@1 | 487 | Operation op ; |
duke@1 | 488 | |
duke@1 | 489 | MapAction( Operation op ) |
duke@1 | 490 | { |
duke@1 | 491 | this.op = op ; |
duke@1 | 492 | } |
duke@1 | 493 | |
duke@1 | 494 | public Object operate( Object value ) |
duke@1 | 495 | { |
duke@1 | 496 | Object[] values = (Object[])value ; |
duke@1 | 497 | Object[] result = new Object[ values.length ] ; |
duke@1 | 498 | for (int ctr=0; ctr<values.length; ctr++ ) |
duke@1 | 499 | result[ctr] = op.operate( values[ctr] ) ; |
duke@1 | 500 | return result ; |
duke@1 | 501 | } |
duke@1 | 502 | |
duke@1 | 503 | public String toString() { |
duke@1 | 504 | return "mapAction(" + op + ")" ; |
duke@1 | 505 | } |
duke@1 | 506 | } |
duke@1 | 507 | |
duke@1 | 508 | public static Operation mapAction( Operation op ) |
duke@1 | 509 | { |
duke@1 | 510 | return new MapAction( op ) ; |
duke@1 | 511 | } |
duke@1 | 512 | |
duke@1 | 513 | private static class MapSequenceAction extends OperationBase |
duke@1 | 514 | { |
duke@1 | 515 | private Operation[] op ; |
duke@1 | 516 | |
duke@1 | 517 | public MapSequenceAction( Operation[] op ) |
duke@1 | 518 | { |
duke@1 | 519 | this.op = op ; |
duke@1 | 520 | } |
duke@1 | 521 | |
duke@1 | 522 | // XXX Does this correctly handle array types? It seems |
duke@1 | 523 | // that hetereogeneous arrays work this way, while |
duke@1 | 524 | // homogeneous arrays need to use Array.newInstance tricks. |
duke@1 | 525 | public Object operate( Object value ) |
duke@1 | 526 | { |
duke@1 | 527 | Object[] values = (Object[])value ; |
duke@1 | 528 | Object[] result = new Object[ values.length ] ; |
duke@1 | 529 | for (int ctr=0; ctr<values.length; ctr++ ) |
duke@1 | 530 | result[ctr] = op[ctr].operate( values[ctr] ) ; |
duke@1 | 531 | return result ; |
duke@1 | 532 | } |
duke@1 | 533 | |
duke@1 | 534 | public String toString() { |
duke@1 | 535 | return "mapSequenceAction(" + |
duke@1 | 536 | ObjectUtility.compactObjectToString(op) + ")" ; |
duke@1 | 537 | } |
duke@1 | 538 | } |
duke@1 | 539 | |
duke@1 | 540 | public static Operation mapSequenceAction( Operation[] op ) |
duke@1 | 541 | { |
duke@1 | 542 | return new MapSequenceAction( op ) ; |
duke@1 | 543 | } |
duke@1 | 544 | |
duke@1 | 545 | private static class ConvertIntegerToShort extends OperationBase |
duke@1 | 546 | { |
duke@1 | 547 | public Object operate( Object value ) |
duke@1 | 548 | { |
duke@1 | 549 | Integer val = (Integer)value ; |
duke@1 | 550 | return new Short( val.shortValue() ) ; |
duke@1 | 551 | } |
duke@1 | 552 | |
duke@1 | 553 | public String toString() { |
duke@1 | 554 | return "ConvertIntegerToShort" ; |
duke@1 | 555 | } |
duke@1 | 556 | } |
duke@1 | 557 | |
duke@1 | 558 | private static Operation convertIntegerToShortImpl = new ConvertIntegerToShort() ; |
duke@1 | 559 | |
duke@1 | 560 | public static Operation convertIntegerToShort() |
duke@1 | 561 | { |
duke@1 | 562 | return convertIntegerToShortImpl ; |
duke@1 | 563 | } |
duke@1 | 564 | } |