Revision 44748 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
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; |
|
11 | 13 |
import org.gvsig.expressionevaluator.Code; |
12 | 14 |
import org.gvsig.expressionevaluator.Expression; |
13 | 15 |
|
... | ... | |
379 | 381 |
|
380 | 382 |
protected String name; |
381 | 383 |
protected String format; |
382 |
protected List<Value> parameters;
|
|
384 |
protected List<Pair<String,Value>> parameters;
|
|
383 | 385 |
|
384 | 386 |
public FunctionBase(String name, String format) { |
385 | 387 |
this.name = name; |
... | ... | |
389 | 391 |
public FunctionBase(String name) { |
390 | 392 |
this(name,null); |
391 | 393 |
} |
392 |
|
|
394 |
|
|
393 | 395 |
@Override |
394 |
public List<Value> parameters() { |
|
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() { |
|
395 | 429 |
if (this.parameters == null) { |
396 | 430 |
this.parameters = new ArrayList<>(); |
397 | 431 |
} |
... | ... | |
400 | 434 |
|
401 | 435 |
@Override |
402 | 436 |
public Function parameter(Value parameter) { |
403 |
this.parameters().add(parameter);
|
|
437 |
this.parameters().add(new ImmutablePair<>(null,parameter));
|
|
404 | 438 |
return this; |
405 | 439 |
} |
406 | 440 |
|
407 | 441 |
@Override |
442 |
public Function parameter(String name, Value parameter) { |
|
443 |
this.parameters().add(new ImmutablePair<>(name,parameter)); |
|
444 |
return this; |
|
445 |
} |
|
446 |
|
|
447 |
@Override |
|
408 | 448 |
public String name() { |
409 | 449 |
return this.name; |
410 | 450 |
} |
... | ... | |
413 | 453 |
public void accept(Visitor visitor, VisitorFilter filter) { |
414 | 454 |
super.accept(visitor, filter); |
415 | 455 |
if( this.parameters!=null ) { |
416 |
for (Value value : this.parameters) {
|
|
417 |
if( value!=null ) {
|
|
418 |
value.accept(visitor, filter);
|
|
456 |
for (Pair<String,Value> argument : this.parameters) {
|
|
457 |
if( argument!=null ) {
|
|
458 |
argument.getValue().accept(visitor, filter);
|
|
419 | 459 |
} |
420 | 460 |
} |
421 | 461 |
} |
... | ... | |
424 | 464 |
@Override |
425 | 465 |
public void replace(Value target, Value replacement) { |
426 | 466 |
for (int i = 0; i < parameters.size(); i++) { |
427 |
Value value = parameters.get(i);
|
|
428 |
if( value == target ) {
|
|
429 |
parameters.set(i, replacement);
|
|
467 |
Pair<String, Value> argument = parameters.get(i);
|
|
468 |
if( argument.getValue() == target ) {
|
|
469 |
parameters.set(i, new ImmutablePair<>(argument.getKey(),replacement));
|
|
430 | 470 |
} else { |
431 |
value.replace(target, replacement);
|
|
471 |
argument.getValue().replace(target, replacement);
|
|
432 | 472 |
} |
433 | 473 |
} |
434 | 474 |
} |
... | ... | |
449 | 489 |
builder.append("("); |
450 | 490 |
if (this.parameters != null && !this.parameters.isEmpty()) { |
451 | 491 |
boolean first = true; |
452 |
for (Value value : this.parameters) {
|
|
492 |
for (Pair<String,Value> argument : this.parameters) {
|
|
453 | 493 |
if( first ) { |
454 | 494 |
first=false; |
455 |
builder.append(value.toString(formatter)); |
|
495 |
if( argument.getKey()!=null ) { |
|
496 |
builder.append(argument.getKey()); |
|
497 |
builder.append(":"); |
|
498 |
} |
|
499 |
builder.append(argument.getValue().toString(formatter)); |
|
456 | 500 |
} else { |
457 | 501 |
builder.append(", "); |
458 |
builder.append(value.toString(formatter)); |
|
502 |
if( argument.getKey()!=null ) { |
|
503 |
builder.append(argument.getKey()); |
|
504 |
builder.append(":"); |
|
505 |
} |
|
506 |
builder.append(argument.getValue().toString(formatter)); |
|
459 | 507 |
} |
460 | 508 |
} |
461 | 509 |
} |
... | ... | |
464 | 512 |
} |
465 | 513 |
if (this.parameters != null && !this.parameters.isEmpty()) { |
466 | 514 |
List<String> values = new ArrayList<>(); |
467 |
for (Value value : this.parameters) {
|
|
468 |
values.add(value.toString(formatter));
|
|
515 |
for (Pair<String,Value> argument : this.parameters) {
|
|
516 |
values.add(argument.getValue().toString(formatter));
|
|
469 | 517 |
} |
470 | 518 |
return MessageFormat.format(format, values.toArray()); |
471 | 519 |
} else { |
... | ... | |
515 | 563 |
builder.append("("); |
516 | 564 |
if (this.parameters != null && !this.parameters.isEmpty()) { |
517 | 565 |
boolean first = true; |
518 |
for (Value value : this.parameters) {
|
|
566 |
for (Pair<String,Value> argument : this.parameters) {
|
|
519 | 567 |
if( first ) { |
520 | 568 |
first=false; |
521 |
builder.append(value.toString(formatter));
|
|
569 |
builder.append(argument.getValue().toString(formatter));
|
|
522 | 570 |
} else { |
523 | 571 |
builder.append(", "); |
524 |
builder.append(value.toString(formatter));
|
|
572 |
builder.append(argument.getValue().toString(formatter));
|
|
525 | 573 |
} |
526 | 574 |
} |
527 | 575 |
} |
... | ... | |
818 | 866 |
@Override |
819 | 867 |
public List<Variable> variables() { |
820 | 868 |
final Set<Variable> vars = new HashSet<>(); |
821 |
this.accept(new Visitor() { |
|
822 |
@Override |
|
823 |
public void visit(Visitable value) { |
|
824 |
if( !vars.contains((Variable)value) ) { |
|
825 |
vars.add((Variable)value); |
|
826 |
} |
|
827 |
} |
|
869 |
this.accept((Visitable value1) -> { |
|
870 |
if (!vars.contains((Variable) value1)) { |
|
871 |
vars.add((Variable) value1); |
|
872 |
} |
|
828 | 873 |
}, new ClassVisitorFilter(Variable.class)); |
829 | 874 |
List<Variable> lvars = new ArrayList<>(vars); |
830 | 875 |
Collections.sort(lvars); |
... | ... | |
834 | 879 |
@Override |
835 | 880 |
public List<Parameter> parameters() { |
836 | 881 |
final List<Parameter> params = new ArrayList<>(); |
837 |
this.accept(new Visitor() { |
|
838 |
@Override |
|
839 |
public void visit(Visitable value) { |
|
840 |
params.add((Parameter) value); |
|
841 |
} |
|
882 |
this.accept((Visitable value1) -> { |
|
883 |
params.add((Parameter) value1); |
|
842 | 884 |
}, new ClassVisitorFilter(Parameter.class)); |
843 | 885 |
return params; |
844 | 886 |
} |
... | ... | |
1323 | 1365 |
public String repr(Object value) { |
1324 | 1366 |
return this.manager.getReprMethod(value).repr(value); |
1325 | 1367 |
} |
1326 |
|
|
1368 |
|
|
1369 |
@Override |
|
1370 |
public Function getattr(Value object, String attrname) { |
|
1371 |
Function fn = function(FUNCTION_GETATTR); |
|
1372 |
fn.parameter(null, object); |
|
1373 |
fn.parameter(null, constant(attrname)); |
|
1374 |
return fn; |
|
1375 |
} |
|
1376 |
|
|
1327 | 1377 |
} |
Also available in: Unified diff