comparison artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java @ 7383:36a194156f15

Datacage: Added dc:min-number(list) and dc:max-number(list) to extract the numerical minimum/maximum from a list of numbers. Example: dc:min-number(dc:find-all('\d{4}', '1919 1291 1299 3029')) returns 1291
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 18 Oct 2013 18:36:18 +0200
parents 420eb5a5fde4
children ef310e272fb5
comparison
equal deleted inserted replaced
7382:420eb5a5fde4 7383:36a194156f15
183 && needle instanceof String 183 && needle instanceof String
184 ? findAll((String)needle, (String)haystack) 184 ? findAll((String)needle, (String)haystack)
185 : Collections.<String>emptyList(); 185 : Collections.<String>emptyList();
186 } 186 }
187 }); 187 });
188
189 addFunction("max-number", 1, new XPathFunction() {
190 @Override
191 public Object evaluate(List args) throws XPathFunctionException {
192 return maxNumber(args.get(0));
193 }
194 });
195
196 addFunction("min-number", 1, new XPathFunction() {
197 @Override
198 public Object evaluate(List args) throws XPathFunctionException {
199 return minNumber(args.get(0));
200 }
201 });
188 } 202 }
189 203
190 /** 204 /**
191 * Create a new function. 205 * Create a new function.
192 * @param name Name of the function. 206 * @param name Name of the function.
435 while (matcher.find()) { 449 while (matcher.find()) {
436 result.add(matcher.group()); 450 result.add(matcher.group());
437 } 451 }
438 return result; 452 return result;
439 } 453 }
454
455 public Number maxNumber(Object list) {
456 if (list instanceof Collection) {
457 Collection collection = (Collection)list;
458 double max = -Double.MAX_VALUE;
459 for (Object x: collection) {
460 Number n;
461 if (x instanceof Number) {
462 n = (Number)x;
463 }
464 else if (x instanceof String) {
465 try {
466 n = Double.valueOf((String)x);
467 }
468 catch (NumberFormatException nfe) {
469 log.warn("'" + x + "' is not a number.");
470 continue;
471 }
472 }
473 else {
474 log.warn("'" + x + "' is not a number.");
475 continue;
476 }
477
478 double v = n.doubleValue();
479
480 if (v > max) {
481 max = v;
482 }
483 }
484
485 return Double.valueOf(max == -Double.MAX_VALUE
486 ? Double.MAX_VALUE
487 : max);
488 }
489
490 return list instanceof Number
491 ? (Number)list
492 : Double.valueOf(Double.MAX_VALUE);
493 }
494
495 public Number minNumber(Object list) {
496 if (list instanceof Collection) {
497 Collection collection = (Collection)list;
498 double min = Double.MAX_VALUE;
499 for (Object x: collection) {
500 Number n;
501 if (x instanceof Number) {
502 n = (Number)x;
503 }
504 else if (x instanceof String) {
505 try {
506 n = Double.valueOf((String)x);
507 }
508 catch (NumberFormatException nfe) {
509 log.warn("'" + x + "' is not a number.");
510 continue;
511 }
512 }
513 else {
514 log.warn("'" + x + "' is not a number.");
515 continue;
516 }
517
518 double v = n.doubleValue();
519
520 if (v < min) {
521 min = v;
522 }
523 }
524
525 return Double.valueOf(min == Double.MAX_VALUE
526 ? -Double.MAX_VALUE
527 : min);
528 }
529
530 return list instanceof Number
531 ? (Number)list
532 : Double.valueOf(-Double.MAX_VALUE);
533 }
440 } 534 }
441 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 535 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org