Changeset 12822 in josm for trunk


Ignore:
Timestamp:
2017-09-11T09:56:28+02:00 (7 years ago)
Author:
bastiK
Message:

add method for performance to CheckParameterUtil

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r12649 r12822  
    148148        static void checkObject(final Object obj) {
    149149            CheckParameterUtil.ensureThat(obj instanceof Expression || obj instanceof String,
    150                     "instance of Exception or String expected, but got " + obj);
     150                    () -> "instance of Exception or String expected, but got " + obj);
    151151        }
    152152
  • trunk/src/org/openstreetmap/josm/tools/CheckParameterUtil.java

    r12713 r12822  
    44import java.text.MessageFormat;
    55import java.util.function.Predicate;
     6import java.util.function.Supplier;
    67
    78import org.openstreetmap.josm.data.coor.EastNorth;
     
    151152     * @param message error message
    152153     * @throws IllegalArgumentException if the condition does not hold
     154     * @see #ensureThat(boolean, Supplier)
    153155     */
    154156    public static void ensureThat(boolean condition, String message) {
    155157        if (!condition)
    156158            throw new IllegalArgumentException(message);
     159    }
     160
     161    /**
     162     * Ensures that the condition {@code condition} holds.
     163     *
     164     * This method can be used when the message is not a plain string literal,
     165     * but somehow constructed. Using a {@link Supplier} improves the performance,
     166     * as the string construction is skipped when the condition holds.
     167     * @param condition The condition to check
     168     * @param messageSupplier supplier of the error message
     169     * @throws IllegalArgumentException if the condition does not hold
     170     * @since 12822
     171     */
     172    public static void ensureThat(boolean condition, Supplier<String> messageSupplier) {
     173        if (!condition)
     174            throw new IllegalArgumentException(messageSupplier.get());
    157175    }
    158176
Note: See TracChangeset for help on using the changeset viewer.