1 4 package gnu.xquery.util; 5 import gnu.mapping.*; 6 import gnu.lists.*; 7 import gnu.xml.TextUtils; 8 import gnu.kawa.xml.*; 9 import gnu.kawa.functions.*; 10 11 public class MinMax 12 { 13 public static Object min (Object arg, NamedCollator collation) 14 { 15 return minMax(arg, false, collation); 16 } 17 18 public static Object max (Object arg, NamedCollator collation) 19 { 20 return minMax(arg, true, collation); 21 } 22 23 public static Object minMax (Object arg, boolean returnMax, 24 NamedCollator collation) 25 { 26 if (arg instanceof Values) 27 { 28 TreeList tlist = (TreeList) arg; 29 int pos = 0; 30 int flags = returnMax ? Compare.TRUE_IF_GRT : Compare.TRUE_IF_LSS; 31 Object cur = tlist.getPosNext(pos); 32 if (cur == Sequence.eofValue) 33 return Values.empty; 34 Object result = convert(cur); 35 for (;;) 36 { 37 pos = tlist.nextPos(pos); 38 cur = tlist.getPosNext(pos); 39 if (cur == Sequence.eofValue) 40 return result; 41 cur = convert(cur); 42 43 if (result instanceof Number || cur instanceof Number ) 44 { 45 int code1 = Arithmetic.classifyValue(result); 46 int code2 = Arithmetic.classifyValue(cur); 47 int rcode = NumberCompare.compare(result, code1, 48 cur, code2, false); 49 if (rcode == -3) 50 throw new IllegalArgumentException ("values cannot be compared"); 51 int code = code1 < code2 ? code2 : code1; 52 boolean castNeeded; 53 if (rcode == -2) 54 { 55 result = NumberValue.NaN; 56 castNeeded = true; } 58 else if (! NumberCompare.checkCompareCode(rcode, flags)) 59 { 60 castNeeded = code != code2; 61 result = cur; 62 } 63 else 64 { 65 castNeeded = code != code1; 66 } 67 if (castNeeded) 68 result = Arithmetic.convert(result, code); 69 } 70 else 71 { 72 if (! Compare.atomicCompare(flags, result, cur, collation)) 73 result = cur; 74 } 75 } 76 } 77 else 78 { 79 arg = convert(arg); 80 Compare.atomicCompare(Compare.TRUE_IF_GRT, arg, arg, collation); 82 return arg; 83 } 84 } 85 86 static Object convert (Object arg) 87 { 88 arg = KNode.atomicValue(arg); 89 if (arg instanceof UntypedAtomic) 90 arg = (Double ) 91 XDataType.doubleType.valueOf(TextUtils.stringValue(arg)); 92 return arg; 93 } 94 } 95 | Popular Tags |