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