Revision 44750 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/DefaultExpressionBuilder.java

View differences:

DefaultExpressionBuilder.java
8 8
import java.util.Objects;
9 9
import java.util.Set;
10 10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.lang3.tuple.ImmutablePair;
12
import org.apache.commons.lang3.tuple.Pair;
13 11
import org.gvsig.expressionevaluator.Code;
14 12
import org.gvsig.expressionevaluator.Expression;
15 13

  
......
381 379

  
382 380
        protected String name;
383 381
        protected String format;
384
        protected List<Pair<String,Value>> parameters;
382
        protected List<Value> parameters;
385 383

  
386 384
        public FunctionBase(String name, String format) {
387 385
            this.name = name;
......
393 391
        }
394 392

  
395 393
        @Override
396
        public Value getParameter(String name) {
397
          for (Pair<String, Value> parameter : parameters) {
398
            if( parameter!=null && StringUtils.equalsIgnoreCase(name, parameter.getKey()) ) {
399
              return parameter.getValue();
400
            }
401
          }
402
          return null;
403
        }
404

  
405
        @Override
406
        public boolean containsParameter(String name, int index) {
407
          String argNameX = name + String.valueOf(index).trim();
408
          for (Pair<String, Value> arg : this.parameters) {
409
            if( StringUtils.equalsIgnoreCase(arg.getKey(), argNameX) ) {
410
              return true;
411
            }
412
          }
413
          return false;
414
        }
415

  
416
        @Override
417
        public Value getParameter(String name, int index) {
418
          String argNameX = name + String.valueOf(index).trim();
419
          for (Pair<String, Value> arg : this.parameters) {
420
            if( StringUtils.equalsIgnoreCase(arg.getKey(), argNameX) ) {
421
              return arg.getValue();
422
            }
423
          }
424
          return null;
425
        }
426

  
427
        @Override
428
        public List<Pair<String,Value>> parameters() {
394
        public List<Value> parameters() {
429 395
            if (this.parameters == null) {
430 396
                this.parameters = new ArrayList<>();
431 397
            }
......
434 400

  
435 401
        @Override
436 402
        public Function parameter(Value parameter) {
437
            this.parameters().add(new ImmutablePair<>(null,parameter));
403
            this.parameters().add(parameter);
438 404
            return this;
439 405
        }
440 406

  
441 407
        @Override
442
        public Function parameter(String name, Value parameter) {
443
            this.parameters().add(new ImmutablePair<>(name,parameter));
444
            return this;
445
        }
446

  
447
        @Override
448 408
        public String name() {
449 409
            return this.name;
450 410
        }
......
453 413
        public void accept(Visitor visitor, VisitorFilter filter) {
454 414
            super.accept(visitor, filter);
455 415
            if( this.parameters!=null ) {
456
                for (Pair<String,Value> argument : this.parameters) {
416
                for (Value argument : this.parameters) {
457 417
                    if( argument!=null ) {
458
                        argument.getValue().accept(visitor, filter);
418
                        argument.accept(visitor, filter);
459 419
                    }
460 420
                }
461 421
            }
......
463 423

  
464 424
        @Override
465 425
        public void replace(Value target, Value replacement) {
426
          if( this.parameters!=null ) {
466 427
            for (int i = 0; i < parameters.size(); i++) {
467
                Pair<String, Value> argument = parameters.get(i);
468
                if( argument.getValue() == target ) {
469
                    parameters.set(i, new ImmutablePair<>(argument.getKey(),replacement));
428
                Value argument = parameters.get(i);
429
                if( argument == target ) {
430
                    parameters.set(i, replacement);
470 431
                } else {
471
                    argument.getValue().replace(target, replacement);
432
                    argument.replace(target, replacement);
472 433
                }
473 434
            }
435
          }
474 436
        }
475 437
        
476 438
        @Override
......
489 451
                builder.append("(");
490 452
                if (this.parameters != null && !this.parameters.isEmpty()) {
491 453
                    boolean first = true;
492
                    for (Pair<String,Value> argument : this.parameters) {
454
                    for (Value argument : this.parameters) {
493 455
                        if( first ) {
494 456
                            first=false;
495
                            if( argument.getKey()!=null ) {
496
                              builder.append(argument.getKey());
497
                              builder.append(":");  
498
                            }
499
                            builder.append(argument.getValue().toString(formatter));
457
                            builder.append(argument.toString(formatter));
500 458
                        } else {
501 459
                            builder.append(", ");
502
                            if( argument.getKey()!=null ) {
503
                              builder.append(argument.getKey());
504
                              builder.append(":");  
505
                            }
506
                            builder.append(argument.getValue().toString(formatter));
460
                            builder.append(argument.toString(formatter));
507 461
                        }
508 462
                    }
509 463
                }
......
512 466
            }
513 467
            if (this.parameters != null && !this.parameters.isEmpty()) {
514 468
                List<String> values = new ArrayList<>();
515
                for (Pair<String,Value> argument : this.parameters) {
516
                    values.add(argument.getValue().toString(formatter));
469
                for (Value argument : this.parameters) {
470
                    values.add(argument.toString(formatter));
517 471
                }
518 472
                return MessageFormat.format(format, values.toArray());
519 473
            } else {
......
563 517
            builder.append("(");
564 518
            if (this.parameters != null && !this.parameters.isEmpty()) {
565 519
                boolean first = true;
566
                for (Pair<String,Value> argument : this.parameters) {
520
                for (Value argument : this.parameters) {
567 521
                    if( first ) {
568 522
                        first=false;
569
                        builder.append(argument.getValue().toString(formatter));
523
                        builder.append(argument.toString(formatter));
570 524
                    } else {
571 525
                        builder.append(", ");
572
                        builder.append(argument.getValue().toString(formatter));
526
                        builder.append(argument.toString(formatter));
573 527
                    }
574 528
                }
575 529
            }
......
1367 1321
    }
1368 1322

  
1369 1323
  @Override
1370
  public Function getattr(Value object, String attrname) {
1324
  public Function getattr(String objectId, String attributeId) {
1371 1325
        Function fn = function(FUNCTION_GETATTR);
1372
        fn.parameter(null, object);
1373
        fn.parameter(null, constant(attrname));
1326
        fn.parameter(variable(objectId));
1327
        fn.parameter(variable(attributeId));
1374 1328
        return fn;
1375 1329
  }
1376 1330

  
1331
  @Override
1332
  public Function date(Value date) {
1333
    return function(FUNCTION_DATE, date);
1334
  }
1335

  
1336
  @Override
1337
  public Function time(Value time) {
1338
    return function(FUNCTION_TIME, time);
1339
  }
1340

  
1341
  @Override
1342
  public Function timestamp(Value timestamp) {
1343
    return function(FUNCTION_TIMESTAMP, timestamp);
1344
  }
1345

  
1346
  @Override
1347
  public Function current_date() {
1348
    return function(FUNCTION_CURRENT_DATE);
1349
  }
1350

  
1351
  @Override
1352
  public Function current_time() {
1353
    return function(FUNCTION_CURRENT_TIME);
1354
  }
1355

  
1356
  @Override
1357
  public Function current_timestamp() {
1358
    return function(FUNCTION_CURRENT_TIMESTAMP);
1359
  }
1360

  
1361
  @Override
1362
  public Function date_add(Value datefield, Value valueToAdd, Value date) {
1363
    return function(FUNCTION_DATEADD, datefield, valueToAdd, date);
1364
  }
1365

  
1366
  @Override
1367
  public Function date_diff(Value datefield, Value valueToSubst, Value date) {
1368
    return function(FUNCTION_DATEDIFF, datefield, valueToSubst, date);
1369
  }
1370

  
1371
  @Override
1372
  public Function to_date(Value date, Value format) {
1373
    return function(FUNCTION_TO_DATE, date, format);
1374
  }
1375

  
1376
  @Override
1377
  public Function to_timestamp(Value timestamp, Value format) {
1378
    return function(FUNCTION_TO_TIMESTAMP, timestamp, format);
1379
  }
1380

  
1381
  @Override
1382
  public Function extract(Value datefield, Value source) {
1383
    return function(FUNCTION_EXTRACT, datefield, source);
1384
  }
1385
  
1377 1386
}

Also available in: Unified diff