Ignore:
Timestamp:
2017-09-03T12:32:11+02:00 (2 years ago)
Author:
bastiK
Message:

see #15229 - remove dependencies of CheckParameterUtil on various data classes

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/CheckParameterUtil.java

    r9231 r12713  
    33
    44import java.text.MessageFormat;
     5import java.util.function.Predicate;
    56
    67import org.openstreetmap.josm.data.coor.EastNorth;
     
    2122
    2223    /**
     24     * Ensures that a parameter is not null and that a certain condition holds.
     25     * @param <T> parameter type
     26     * @param obj parameter value
     27     * @param parameterName parameter name
     28     * @param conditionMsg string, stating the condition
     29     * @param condition the condition to check
     30     * @throws IllegalArgumentException in case the object is null or the condition
     31     * is violated
     32     * @since 12713
     33     */
     34    public static <T> void ensure(T obj, String parameterName, String conditionMsg, Predicate<T> condition) {
     35        ensureParameterNotNull(obj, parameterName);
     36        if (!condition.test(obj))
     37            throw new IllegalArgumentException(
     38                    MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, violated condition: ''{2}'', got ''{3}''",
     39                            parameterName,
     40                            obj.getClass().getCanonicalName(),
     41                            conditionMsg,
     42                            obj));
     43    }
     44
     45    /**
     46     * Ensures that a parameter is not null and that a certain condition holds.
     47     * @param <T> parameter type
     48     * @param obj parameter value
     49     * @param parameterName parameter name
     50     * @param condition the condition to check
     51     * @throws IllegalArgumentException in case the object is null or the condition
     52     * is violated
     53     * @since 12713
     54     */
     55    public static <T> void ensure(T obj, String parameterName, Predicate<T> condition) {
     56        ensureParameterNotNull(obj, parameterName);
     57        if (!condition.test(obj))
     58            throw new IllegalArgumentException(
     59                    MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, got ''{2}''",
     60                            parameterName,
     61                            obj.getClass().getCanonicalName(),
     62                            obj));
     63    }
     64
     65    /**
    2366     * Ensures an OSM primitive ID is valid
    2467     * @param id The id to check
    2568     * @param parameterName The parameter name
    2669     * @throws IllegalArgumentException if the primitive ID is not valid (negative or zero)
     70     * @deprecated use {@link #ensure(Object, String, String, Predicate)}
    2771     */
     72    @Deprecated
    2873    public static void ensureValidPrimitiveId(PrimitiveId id, String parameterName) {
    2974        ensureParameterNotNull(id, parameterName);
     
    3984     * @throws IllegalArgumentException if the lat/lon are {@code null} or not valid
    4085     * @since 5980
     86     * @deprecated use {@link #ensure(Object, String, Predicate)}
    4187     */
     88    @Deprecated
    4289    public static void ensureValidCoordinates(LatLon latlon, String parameterName) {
    4390        ensureParameterNotNull(latlon, parameterName);
     
    53100     * @throws IllegalArgumentException if the east/north are {@code null} or not valid
    54101     * @since 5980
     102     * @deprecated use {@link #ensure(Object, String, Predicate)}
    55103     */
     104    @Deprecated
    56105    public static void ensureValidCoordinates(EastNorth eastnorth, String parameterName) {
    57106        ensureParameterNotNull(eastnorth, parameterName);
     
    66115     * @param parameterName The parameter name
    67116     * @throws IllegalArgumentException if the version is not valid (negative)
     117     * @deprecated use {@link #ensure(Object, String, String, Predicate)}
    68118     */
     119    @Deprecated
    69120    public static void ensureValidVersion(long version, String parameterName) {
    70121        if (version < 0)
     
    113164     * @throws IllegalArgumentException if id is null
    114165     * @throws IllegalArgumentException if id.getType() != NODE
     166     * @deprecated use {@link #ensure(Object, String, String, Predicate)}
    115167     */
     168    @Deprecated
    116169    public static void ensureValidNodeId(PrimitiveId id, String parameterName) {
    117170        ensureParameterNotNull(id, parameterName);
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r12636 r12713  
    292292    public static EastNorth getSegmentSegmentIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    293293
    294         CheckParameterUtil.ensureValidCoordinates(p1, "p1");
    295         CheckParameterUtil.ensureValidCoordinates(p2, "p2");
    296         CheckParameterUtil.ensureValidCoordinates(p3, "p3");
    297         CheckParameterUtil.ensureValidCoordinates(p4, "p4");
     294        CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
     295        CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
     296        CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
     297        CheckParameterUtil.ensure(p4, "p4", EastNorth::isValid);
    298298
    299299        double x1 = p1.getX();
     
    357357    public static EastNorth getLineLineIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    358358
    359         CheckParameterUtil.ensureValidCoordinates(p1, "p1");
    360         CheckParameterUtil.ensureValidCoordinates(p2, "p2");
    361         CheckParameterUtil.ensureValidCoordinates(p3, "p3");
    362         CheckParameterUtil.ensureValidCoordinates(p4, "p4");
     359        CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
     360        CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
     361        CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
     362        CheckParameterUtil.ensure(p4, "p4", EastNorth::isValid);
    363363
    364364        // Basically, the formula from wikipedia is used:
     
    400400    public static boolean segmentsParallel(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    401401
    402         CheckParameterUtil.ensureValidCoordinates(p1, "p1");
    403         CheckParameterUtil.ensureValidCoordinates(p2, "p2");
    404         CheckParameterUtil.ensureValidCoordinates(p3, "p3");
    405         CheckParameterUtil.ensureValidCoordinates(p4, "p4");
     402        CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
     403        CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
     404        CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
     405        CheckParameterUtil.ensure(p4, "p4", EastNorth::isValid);
    406406
    407407        // Convert line from (point, point) form to ax+by=c
     
    486486    public static boolean angleIsClockwise(EastNorth commonNode, EastNorth firstNode, EastNorth secondNode) {
    487487
    488         CheckParameterUtil.ensureValidCoordinates(commonNode, "commonNode");
    489         CheckParameterUtil.ensureValidCoordinates(firstNode, "firstNode");
    490         CheckParameterUtil.ensureValidCoordinates(secondNode, "secondNode");
     488        CheckParameterUtil.ensure(commonNode, "commonNode", EastNorth::isValid);
     489        CheckParameterUtil.ensure(firstNode, "firstNode", EastNorth::isValid);
     490        CheckParameterUtil.ensure(secondNode, "secondNode", EastNorth::isValid);
    491491
    492492        double dy1 = firstNode.getY() - commonNode.getY();
     
    771771    public static double getSegmentAngle(EastNorth p1, EastNorth p2) {
    772772
    773         CheckParameterUtil.ensureValidCoordinates(p1, "p1");
    774         CheckParameterUtil.ensureValidCoordinates(p2, "p2");
     773        CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
     774        CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
    775775
    776776        return Math.atan2(p2.north() - p1.north(), p2.east() - p1.east());
     
    787787    public static double getCornerAngle(EastNorth p1, EastNorth p2, EastNorth p3) {
    788788
    789         CheckParameterUtil.ensureValidCoordinates(p1, "p1");
    790         CheckParameterUtil.ensureValidCoordinates(p2, "p2");
    791         CheckParameterUtil.ensureValidCoordinates(p3, "p3");
     789        CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
     790        CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
     791        CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
    792792
    793793        Double result = getSegmentAngle(p2, p1) - getSegmentAngle(p2, p3);
Note: See TracChangeset for help on using the changeset viewer.