1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 4 5 8 9 public final class BooleanValue extends Value { 10 private boolean value; 11 12 16 17 public BooleanValue(boolean value) { 18 this.value = value; 19 } 20 21 25 26 public String asString() { 27 return (value ? "true" : "false"); 28 } 29 30 34 35 public double asNumber() { 36 return (value ? 1 : 0); 37 } 38 39 43 44 public boolean asBoolean() { 45 return value; 46 } 47 48 49 53 54 public int getDataType() { 55 return Value.BOOLEAN; 56 } 57 58 62 63 public int conversionPreference(Class required) { 64 65 if (required==Object .class) return 50; 66 if (required.isAssignableFrom(BooleanValue.class)) return 0; 67 68 if (required==boolean.class) return 0; 69 if (required==Boolean .class) return 0; 70 if (required==byte.class) return 3; 71 if (required==Byte .class) return 4; 72 if (required==char.class) return Integer.MAX_VALUE; 73 if (required==Character .class) return Integer.MAX_VALUE; 74 if (required==double.class) return 3; 75 if (required==Double .class) return 4; 76 if (required==float.class) return 3; 77 if (required==Float .class) return 4; 78 if (required==int.class) return 3; 79 if (required==Integer .class) return 4; 80 if (required==long.class) return 3; 81 if (required==Long .class) return 4; 82 if (required==short.class) return 3; 83 if (required==Short .class) return 4; 84 if (required==String .class) return 2; 85 86 return Integer.MAX_VALUE; 87 } 88 89 92 93 public Object convertToJava(Class target) throws XPathException { 94 if (target==Object .class) { 95 return new Boolean (value); 96 } else if (target.isAssignableFrom(BooleanValue.class)) { 97 return this; 98 } else if (target==boolean.class) { 99 return new Boolean (value); 100 } else if (target==Boolean .class) { 101 return new Boolean (value); 102 } else if (target==Object .class) { 103 return new Boolean (value); 104 } else if (target==String .class) { 105 return asString(); 106 } else if (target==double.class) { 107 return new Double (asNumber()); 108 } else if (target==Double .class) { 109 return new Double (asNumber()); 110 } else if (target==float.class) { 111 return new Float (asNumber()); 112 } else if (target==Float .class) { 113 return new Float (asNumber()); 114 } else if (target==long.class) { 115 return new Long ((value?1:0)); 116 } else if (target==Long .class) { 117 return new Long ((value?1:0)); 118 } else if (target==int.class) { 119 return new Integer ((value?1:0)); 120 } else if (target==Integer .class) { 121 return new Integer ((value?1:0)); 122 } else if (target==short.class) { 123 return new Short ((short)(value?1:0)); 124 } else if (target==Short .class) { 125 return new Short ((short)(value?1:0)); 126 } else if (target==byte.class) { 127 return new Byte ((byte)(value?1:0)); 128 } else if (target==Byte .class) { 129 return new Byte ((byte)(value?1:0)); 130 } else { 131 throw new XPathException("Conversion of boolean to " + target.getName() + 132 " is not supported"); 133 } 134 } 135 136 139 140 public void display(int level) { 141 System.err.println(indent(level) + "boolean (" + asString() + ")" ); 142 } 143 } 144 145 164 | Popular Tags |