| 1 | |
package org.webslinger.bsf; |
| 2 | |
|
| 3 | |
import java.util.Arrays; |
| 4 | |
import java.util.Vector; |
| 5 | |
|
| 6 | |
import org.apache.bsf.BSFException; |
| 7 | |
|
| 8 | 0 | public class ApplyKeyDynamic<C> extends ApplyKey<C> { |
| 9 | |
private final String[] parameterNames; |
| 10 | |
private final Object[] parameters; |
| 11 | |
|
| 12 | |
public ApplyKeyDynamic(Object id, String[] parameterNames, Object[] parameters) { |
| 13 | 0 | super(id); |
| 14 | 0 | this.parameterNames = parameterNames; |
| 15 | 0 | this.parameters = parameters; |
| 16 | 0 | } |
| 17 | |
|
| 18 | |
public String[] getParameterNames() { |
| 19 | 0 | return parameterNames; |
| 20 | |
} |
| 21 | |
|
| 22 | |
public Object[] getParameters() { |
| 23 | 0 | return parameters; |
| 24 | |
} |
| 25 | |
|
| 26 | |
public Object[] getArgs(Compiler compiler) { |
| 27 | 0 | Object[] myParameters = getParameters(); |
| 28 | 0 | Object[] args = compiler.newWrapArray(myParameters.length); |
| 29 | 0 | for (int i = 0; i < args.length; i++) { |
| 30 | 0 | Object value = myParameters[i]; |
| 31 | 0 | Class type = value != null ? value.getClass() : null; |
| 32 | 0 | args[i] = compiler.wrap(value, type); |
| 33 | |
} |
| 34 | 0 | return args; |
| 35 | |
} |
| 36 | |
|
| 37 | |
public Key<C> freeze() { |
| 38 | 0 | String[] myParameterNames = getParameterNames(); |
| 39 | 0 | String[] names = new String[myParameterNames.length]; |
| 40 | 0 | System.arraycopy(myParameterNames, 0, names, 0, myParameterNames.length); |
| 41 | 0 | Object[] myParameters = getParameters(); |
| 42 | 0 | Class[] types = new Class[myParameters.length]; |
| 43 | 0 | for (int i = 0; i < types.length; i++) { |
| 44 | 0 | Object arg = myParameters[i]; |
| 45 | 0 | types[i] = arg != null ? arg.getClass() : Void.TYPE; |
| 46 | |
} |
| 47 | 0 | return new ApplyKey<C>(getId(), names, types); |
| 48 | |
} |
| 49 | |
|
| 50 | |
protected int computeHashCode() { |
| 51 | 0 | int tmpHashCode = super.computeHashCode(); |
| 52 | 0 | String[] myParameterNames = getParameterNames(); |
| 53 | 0 | int length = myParameterNames.length; |
| 54 | 0 | for (int i = 0; i < length; i++) { |
| 55 | 0 | tmpHashCode ^= myParameterNames[i].hashCode(); |
| 56 | |
} |
| 57 | 0 | Object[] myParameters = getParameters(); |
| 58 | 0 | length = myParameters.length; |
| 59 | 0 | for (int i = 0; i < length; i++) { |
| 60 | 0 | Object arg = myParameters[i]; |
| 61 | 0 | if (arg == null) { |
| 62 | 0 | tmpHashCode ^= Void.TYPE.hashCode(); |
| 63 | |
} else { |
| 64 | 0 | tmpHashCode ^= arg.getClass().hashCode(); |
| 65 | |
} |
| 66 | |
} |
| 67 | 0 | return tmpHashCode; |
| 68 | |
} |
| 69 | |
|
| 70 | |
protected boolean keyEquals(Key key) { |
| 71 | 0 | if (!super.keyEquals(key)) return false; |
| 72 | 0 | String[] myParameterNames = getParameterNames(); |
| 73 | 0 | Object[] myParameters = getParameters(); |
| 74 | 0 | if (key instanceof ApplyKeyDynamic) { |
| 75 | 0 | ApplyKeyDynamic other = (ApplyKeyDynamic) key; |
| 76 | 0 | String[] otherParameterNames = other.getParameterNames(); |
| 77 | 0 | if (!Arrays.equals(myParameterNames, otherParameterNames)) return false; |
| 78 | 0 | Object[] otherArgs = other.getParameters(); |
| 79 | 0 | return Arrays.equals(myParameters, otherArgs); |
| 80 | |
} else { |
| 81 | 0 | ApplyKey other = (ApplyKey) key; |
| 82 | 0 | String[] otherNames = other.getNames(); |
| 83 | 0 | if (!Arrays.equals(myParameterNames, otherNames)) return false; |
| 84 | 0 | Class[] otherTypes = other.getTypes(); |
| 85 | 0 | if (myParameters.length != otherTypes.length) return false; |
| 86 | 0 | for (int i = 0; i < myParameters.length; i++) { |
| 87 | 0 | Object arg = myParameters[i]; |
| 88 | 0 | Class otherType = otherTypes[i]; |
| 89 | 0 | if (arg == null) { |
| 90 | 0 | if (otherType == Void.TYPE) continue; |
| 91 | 0 | return false; |
| 92 | |
} else { |
| 93 | 0 | if (otherType == Void.TYPE) return false; |
| 94 | 0 | if (!arg.getClass().equals(otherType)) return false; |
| 95 | |
} |
| 96 | |
} |
| 97 | |
} |
| 98 | 0 | return true; |
| 99 | |
} |
| 100 | |
|
| 101 | |
public <V> C compile(Compiler<V, C> compiler, Compiler<V, C>.CompilerContext context) throws BSFException { |
| 102 | 0 | throw new IllegalArgumentException("not implemented"); |
| 103 | |
} |
| 104 | |
} |