Changeset 7170 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2014-05-22T20:55:57+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
r7168 r7170 15 15 import java.util.ArrayList; 16 16 import java.util.Arrays; 17 import java.util.Collection; 18 import java.util.Collections; 17 19 import java.util.List; 18 20 import java.util.regex.Matcher; … … 29 31 import org.openstreetmap.josm.io.XmlWriter; 30 32 import org.openstreetmap.josm.tools.ColorHelper; 33 import org.openstreetmap.josm.tools.Predicates; 31 34 import org.openstreetmap.josm.tools.Utils; 32 35 … … 210 213 211 214 /** 212 * Returns the minimum of the given numbers, or NaN if no number is given.213 * @see Math#min(float, float)214 */215 public static float min(float... args) {216 if (args.length == 0) {217 return Float.NaN;218 } else if (args.length == 1) {219 return args[0];220 } else {221 float min = Math.min(args[0], args[1]);222 for (int i = 2; i < args.length; i++) {223 min = Math.min(min, args[i]);224 }225 return min;226 }227 }228 229 /**230 * Returns the maximum of the given numbers, or NaN if no number is given.231 * @see Math#max(float, float)232 */233 public static float max(float... args) {234 if (args.length == 0) {235 return Float.NaN;236 } else if (args.length == 1) {237 return args[0];238 } else {239 float max = Math.max(args[0], args[1]);240 for (int i = 2; i < args.length; i++) {241 max = Math.max(max, args[i]);242 }243 return max;244 }245 }246 247 /**248 215 * Splits string {@code toSplit} at occurrences of the separator string {@code sep} and returns a list of matches. 249 216 * @see String#split(String) … … 673 640 else if ("length".equals(name) && args.size() == 1) 674 641 return new LengthFunction(args.get(0)); 642 else if ("max".equals(name) && !args.isEmpty()) 643 return new MinMaxFunction(args, true); 644 else if ("min".equals(name) && !args.isEmpty()) 645 return new MinMaxFunction(args, false); 675 646 676 647 for (Method m : arrayFunctions) { … … 795 766 return s.length(); 796 767 return null; 768 } 769 } 770 771 /** 772 * Computes the maximum/minimum value an arbitrary number of floats, or a list of floats. 773 */ 774 public static class MinMaxFunction implements Expression { 775 776 private final List<Expression> args; 777 private final boolean computeMax; 778 779 public MinMaxFunction(final List<Expression> args, final boolean computeMax) { 780 this.args = args; 781 this.computeMax = computeMax; 782 } 783 784 public Float aggregateList(List<?> lst) { 785 final List<Float> floats = Utils.transform(lst, new Utils.Function<Object, Float>() { 786 @Override 787 public Float apply(Object x) { 788 return Cascade.convertTo(x, float.class); 789 } 790 }); 791 final Collection<Float> nonNullList = Utils.filter(floats, Predicates.not(Predicates.isNull())); 792 return computeMax ? Collections.max(nonNullList) : Collections.min(nonNullList); 793 } 794 795 @Override 796 public Object evaluate(final Environment env) { 797 List<?> l = Cascade.convertTo(args.get(0).evaluate(env), List.class); 798 if (args.size() != 1 || l == null) 799 l = Utils.transform(args, new Utils.Function<Expression, Object>() { 800 @Override 801 public Object apply(Expression x) { 802 return x.evaluate(env); 803 } 804 }); 805 return aggregateList(l); 797 806 } 798 807 } -
trunk/src/org/openstreetmap/josm/tools/Predicates.java
r7083 r7170 28 28 29 29 /** 30 * Returns a {@link Predicate} executing {@link Utils#equal}.30 * Returns a {@link Predicate} executing {@link Objects#equals}. 31 31 */ 32 32 public static <T> Predicate<T> equalTo(final T ref) { … … 110 110 }; 111 111 } 112 113 /** 114 * Returns a {@link Predicate} testing whether objects are {@code null}. 115 */ 116 public static <T> Predicate<T> isNull() { 117 return new Predicate<T>() { 118 @Override 119 public boolean evaluate(T object) { 120 return object == null; 121 } 122 }; 123 } 112 124 }
Note:
See TracChangeset
for help on using the changeset viewer.