Changeset 16056 in josm for trunk/src


Ignore:
Timestamp:
2020-03-07T13:08:10+01:00 (4 years ago)
Author:
simon04
Message:

CheckParameterUtil: replace ensure() with ensureThat()

Makes code easier to understand; gets rid of one level of indirection.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/WMSEndpointTileSource.java

    r14399 r16056  
    4141    public WMSEndpointTileSource(ImageryInfo info, Projection tileProjection) {
    4242        super(info, tileProjection);
    43         CheckParameterUtil.ensure(info, "imageryType", x -> ImageryType.WMS_ENDPOINT == x.getImageryType());
     43        CheckParameterUtil.ensureThat(info.getImageryType() == ImageryType.WMS_ENDPOINT, "imageryType");
    4444        try {
    4545            wmsi = new WMSImagery(info.getUrl(), info.getCustomHttpHeaders());
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/ParentRelationLoadingTask.java

    r14153 r16056  
    7676    public ParentRelationLoadingTask(Relation child, OsmDataLayer layer, boolean full, PleaseWaitProgressMonitor monitor) {
    7777        super(tr("Download referring relations"), monitor, false /* don't ignore exception */);
    78         CheckParameterUtil.ensure(child, "child", "id > 0", ch -> ch.getUniqueId() > 0);
     78        CheckParameterUtil.ensureThat(child.getUniqueId() > 0, "id > 0");
    7979        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    8080        if (!layer.isDownloadable()) {
  • trunk/src/org/openstreetmap/josm/gui/history/HistoryLoadTask.java

    r14764 r16056  
    8282     */
    8383    public HistoryLoadTask add(PrimitiveId pid) {
    84         CheckParameterUtil.ensure(pid, "pid", "pid > 0", id -> id.getUniqueId() > 0);
     84        CheckParameterUtil.ensureThat(pid.getUniqueId() > 0, "id > 0");
    8585        toLoad.add(pid);
    8686        return this;
     
    120120     */
    121121    public HistoryLoadTask add(OsmPrimitive primitive) {
    122         CheckParameterUtil.ensure(primitive, "primitive", "id > 0", prim -> prim.getOsmId() > 0);
     122        CheckParameterUtil.ensureThat(primitive.getOsmId() > 0, "id > 0");
    123123        return add(primitive.getOsmPrimitiveId());
    124124    }
  • trunk/src/org/openstreetmap/josm/gui/layer/imagery/TileSourceDisplaySettings.java

    r15734 r16056  
    245245
    246246    private void setDisplacement(EastNorth displacement) {
    247         CheckParameterUtil.ensure(displacement, "displacement", EastNorth::isValid);
     247        CheckParameterUtil.ensureThat(displacement.isValid(), () -> displacement + " invalid");
    248248        this.displacement = displacement;
    249249        fireSettingsChange(DISPLACEMENT);
  • trunk/src/org/openstreetmap/josm/io/OsmServerBackreferenceReader.java

    r15426 r16056  
    5454     */
    5555    public OsmServerBackreferenceReader(OsmPrimitive primitive) {
    56         CheckParameterUtil.ensure(primitive, "primitive", "id > 0", prim -> prim.getUniqueId() > 0);
     56        CheckParameterUtil.ensureThat(primitive.getUniqueId() > 0, "id > 0");
    5757        this.id = primitive.getId();
    5858        this.primitiveType = OsmPrimitiveType.from(primitive);
  • trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java

    r14214 r16056  
    9696
    9797    protected OsmServerObjectReader(PrimitiveId id, boolean full, int version) {
    98         CheckParameterUtil.ensure(id, "id", "id > 0", pid -> pid.getUniqueId() > 0);
     98        CheckParameterUtil.ensureThat(id.getUniqueId() > 0, "id > 0");
    9999        this.id = id;
    100100        this.full = full;
  • trunk/src/org/openstreetmap/josm/tools/CheckParameterUtil.java

    r13173 r16056  
    33
    44import java.text.MessageFormat;
    5 import java.util.function.Predicate;
    65import java.util.function.Supplier;
    76
     
    1514    private CheckParameterUtil() {
    1615        // Hide default constructor for utils classes
    17     }
    18 
    19     /**
    20      * Ensures that a parameter is not null and that a certain condition holds.
    21      * @param <T> parameter type
    22      * @param obj parameter value
    23      * @param parameterName parameter name
    24      * @param conditionMsg string, stating the condition
    25      * @param condition the condition to check
    26      * @throws IllegalArgumentException in case the object is null or the condition
    27      * is violated
    28      * @since 12713
    29      */
    30     public static <T> void ensure(T obj, String parameterName, String conditionMsg, Predicate<T> condition) {
    31         ensureParameterNotNull(obj, parameterName);
    32         if (!condition.test(obj))
    33             throw new IllegalArgumentException(
    34                     MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, violated condition: ''{2}'', got ''{3}''",
    35                             parameterName,
    36                             obj.getClass().getCanonicalName(),
    37                             conditionMsg,
    38                             obj));
    39     }
    40 
    41     /**
    42      * Ensures that a parameter is not null and that a certain condition holds.
    43      * @param <T> parameter type
    44      * @param obj parameter value
    45      * @param parameterName parameter name
    46      * @param condition the condition to check
    47      * @throws IllegalArgumentException in case the object is null or the condition
    48      * is violated
    49      * @since 12713
    50      */
    51     public static <T> void ensure(T obj, String parameterName, Predicate<T> condition) {
    52         ensureParameterNotNull(obj, parameterName);
    53         if (!condition.test(obj))
    54             throw new IllegalArgumentException(
    55                     MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, got ''{2}''",
    56                             parameterName,
    57                             obj.getClass().getCanonicalName(),
    58                             obj));
    5916    }
    6017
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r15959 r16056  
    294294    public static EastNorth getSegmentSegmentIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    295295
    296         CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
    297         CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
    298         CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
    299         CheckParameterUtil.ensure(p4, "p4", EastNorth::isValid);
     296        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
     297        CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
     298        CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
     299        CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");
    300300
    301301        double x1 = p1.getX();
     
    359359    public static EastNorth getLineLineIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    360360
    361         CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
    362         CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
    363         CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
    364         CheckParameterUtil.ensure(p4, "p4", EastNorth::isValid);
     361        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
     362        CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
     363        CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
     364        CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");
    365365
    366366        // Basically, the formula from wikipedia is used:
     
    402402    public static boolean segmentsParallel(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    403403
    404         CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
    405         CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
    406         CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
    407         CheckParameterUtil.ensure(p4, "p4", EastNorth::isValid);
     404        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
     405        CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
     406        CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
     407        CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");
    408408
    409409        // Convert line from (point, point) form to ax+by=c
     
    488488    public static boolean angleIsClockwise(EastNorth commonNode, EastNorth firstNode, EastNorth secondNode) {
    489489
    490         CheckParameterUtil.ensure(commonNode, "commonNode", EastNorth::isValid);
    491         CheckParameterUtil.ensure(firstNode, "firstNode", EastNorth::isValid);
    492         CheckParameterUtil.ensure(secondNode, "secondNode", EastNorth::isValid);
     490        CheckParameterUtil.ensureThat(commonNode.isValid(), () -> commonNode + " invalid");
     491        CheckParameterUtil.ensureThat(firstNode.isValid(), () -> firstNode + " invalid");
     492        CheckParameterUtil.ensureThat(secondNode.isValid(), () -> secondNode + " invalid");
    493493
    494494        double dy1 = firstNode.getY() - commonNode.getY();
     
    834834    public static double getSegmentAngle(EastNorth p1, EastNorth p2) {
    835835
    836         CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
    837         CheckParameterUtil.ensure(p2, "p2", EastNorth::isValid);
     836        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
     837        CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");
    838838
    839839        return Math.atan2(p2.north() - p1.north(), p2.east() - p1.east());
     
    850850    public static double getCornerAngle(EastNorth p1, EastNorth common, EastNorth p3) {
    851851
    852         CheckParameterUtil.ensure(p1, "p1", EastNorth::isValid);
    853         CheckParameterUtil.ensure(common, "p2", EastNorth::isValid);
    854         CheckParameterUtil.ensure(p3, "p3", EastNorth::isValid);
     852        CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");
     853        CheckParameterUtil.ensureThat(common.isValid(), () -> common + " invalid");
     854        CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");
    855855
    856856        double result = getSegmentAngle(common, p1) - getSegmentAngle(common, p3);
  • trunk/src/org/openstreetmap/josm/tools/HiDPISupport.java

    r14436 r16056  
    6969     */
    7070    public static Image getMultiResolutionImage(List<Image> imgs) {
    71         CheckParameterUtil.ensure(imgs, "imgs", "not empty", ls -> !ls.isEmpty());
     71        CheckParameterUtil.ensureThat(!imgs.isEmpty(), "imgs is empty");
    7272        Optional<Constructor<? extends Image>> baseMrImageConstructor = getBaseMultiResolutionImageConstructor();
    7373        if (baseMrImageConstructor.isPresent()) {
Note: See TracChangeset for help on using the changeset viewer.