Ignore:
Timestamp:
2017-02-12T16:32:18+01:00 (3 years ago)
Author:
Don-vip
Message:

refactor handling of null values - use Java 8 Optional where possible

Location:
trunk/src/org/openstreetmap/josm/actions
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/CombineWayAction.java

    r11389 r11553  
    1717import java.util.Map;
    1818import java.util.Objects;
     19import java.util.Optional;
    1920import java.util.Set;
    2021import java.util.Stack;
     
    520521
    521522        protected List<NodePair> getOutboundPairs(Node node) {
    522             List<NodePair> l = successors.get(node);
    523             if (l == null)
    524                 return Collections.emptyList();
    525             return l;
     523            return Optional.ofNullable(successors.get(node)).orElseGet(Collections::emptyList);
    526524        }
    527525
  • trunk/src/org/openstreetmap/josm/actions/HelpAction.java

    r9320 r11553  
    88import java.awt.event.ActionEvent;
    99import java.awt.event.KeyEvent;
     10import java.util.Optional;
    1011
    1112import javax.swing.SwingUtilities;
     
    6263                topic = HelpUtil.getContextSpecificHelpTopic(SwingUtilities.getDeepestComponentAt(Main.parent, mouse.x, mouse.y));
    6364            }
    64             if (topic == null) {
    65                 HelpBrowser.setUrlForHelpTopic("/");
    66             } else {
    67                 HelpBrowser.setUrlForHelpTopic(topic);
    68             }
     65            HelpBrowser.setUrlForHelpTopic(Optional.ofNullable(topic).orElse("/"));
    6966        } else {
    7067            HelpBrowser.setUrlForHelpTopic("/");
  • trunk/src/org/openstreetmap/josm/actions/MergeNodesAction.java

    r11481 r11553  
    1515import java.util.List;
    1616import java.util.Objects;
     17import java.util.Optional;
    1718import java.util.Set;
    1819
     
    176177            lastNode = n;
    177178        }
    178         if (targetNode == null) {
    179             targetNode = oldestNode != null ? oldestNode : lastNode;
    180         }
    181         return targetNode;
    182     }
    183 
     179        return Optional.ofNullable(targetNode).orElse(oldestNode != null ? oldestNode : lastNode);
     180    }
    184181
    185182    /**
  • trunk/src/org/openstreetmap/josm/actions/PurgeAction.java

    r11343 r11553  
    307307    protected void updateEnabledState() {
    308308        DataSet ds = getLayerManager().getEditDataSet();
    309         if (ds == null) {
    310             setEnabled(false);
    311         } else {
    312             setEnabled(!ds.selectionEmpty());
    313         }
     309        setEnabled(ds != null && !ds.selectionEmpty());
    314310    }
    315311
  • trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java

    r11452 r11553  
    1818import java.util.LinkedList;
    1919import java.util.List;
     20import java.util.Optional;
    2021import java.util.Set;
    2122import java.util.concurrent.atomic.AtomicInteger;
     
    562563            }
    563564            Relation c = null;
    564             String type = r.get("type");
    565             if (type == null) {
    566                 type = "";
    567             }
     565            String type = Optional.ofNullable(r.get("type")).orElse("");
    568566
    569567            int ic = 0;
  • trunk/src/org/openstreetmap/josm/actions/UpdateSelectionAction.java

    r10548 r11553  
    1010import java.util.Collection;
    1111import java.util.Collections;
     12import java.util.Optional;
    1213
    1314import javax.swing.JOptionPane;
     
    7273    public static void updatePrimitive(PrimitiveId id) {
    7374        ensureParameterNotNull(id, "id");
    74         if (Main.getLayerManager().getEditLayer() == null)
    75             throw new IllegalStateException(tr("No current dataset found"));
    76         OsmPrimitive primitive = Main.getLayerManager().getEditLayer().data.getPrimitiveById(id);
    77         if (primitive == null)
    78             throw new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id));
    79         updatePrimitives(Collections.singleton(primitive));
     75        updatePrimitives(Collections.singleton(Optional.ofNullable(Optional.ofNullable(
     76                Main.getLayerManager().getEditLayer()).orElseThrow(
     77                        () -> new IllegalStateException(tr("No current dataset found")))
     78                .data.getPrimitiveById(id)).orElseThrow(
     79                        () -> new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id)))));
    8080    }
    8181
  • trunk/src/org/openstreetmap/josm/actions/ValidateAction.java

    r10880 r11553  
    1010import java.util.Collection;
    1111import java.util.List;
     12import java.util.Optional;
    1213
    1314import org.openstreetmap.josm.Main;
     
    5556     * <p>
    5657     * If getSelectedItems is true, the selected items (or all items, if no one
    57      * is selected) are validated. If it is false, last selected items are
    58      * revalidated
     58     * is selected) are validated. If it is false, last selected items are revalidated
    5959     *
    6060     * @param getSelectedItems If selected or last selected items must be validated
     
    8383            }
    8484        } else {
    85             if (lastSelection == null) {
    86                 selection = Main.getLayerManager().getEditDataSet().allNonDeletedPrimitives();
    87             } else {
    88                 selection = lastSelection;
    89             }
     85            selection = Optional.ofNullable(lastSelection).orElseGet(
     86                    () -> Main.getLayerManager().getEditDataSet().allNonDeletedPrimitives());
    9087        }
    9188
    92         ValidationTask task = new ValidationTask(tests, selection, lastSelection);
    93         Main.worker.submit(task);
     89        Main.worker.submit(new ValidationTask(tests, selection, lastSelection));
    9490    }
    9591
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    r10436 r11553  
    88import java.util.ArrayList;
    99import java.util.Collection;
     10import java.util.Optional;
    1011import java.util.concurrent.Future;
    1112import java.util.regex.Matcher;
     
    270271            OsmDataLayer layer = addNewLayerIfRequired(newLayerName);
    271272            if (layer == null) {
    272                 layer = getEditLayer();
    273                 if (layer == null) {
    274                     layer = getFirstDataLayer();
    275                 }
     273                layer = Optional.ofNullable(getEditLayer()).orElseGet(this::getFirstDataLayer);
    276274                layer.mergeFrom(dataSet);
    277275                if (zoomAfterDownload) {
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r11452 r11553  
    12611261     */
    12621262    protected static <T> Collection<T> asColl(T o) {
    1263         if (o == null)
    1264             return Collections.emptySet();
    1265         return Collections.singleton(o);
     1263        return o == null ? Collections.emptySet() : Collections.singleton(o);
    12661264    }
    12671265}
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r11453 r11553  
    1616import java.util.Locale;
    1717import java.util.Map;
     18import java.util.Optional;
    1819import java.util.function.Predicate;
    1920import java.util.regex.Matcher;
     
    419420        @Override
    420421        public boolean match(Tagged osm) {
    421             Boolean ret = OsmUtils.getOsmBoolean(osm.get(key));
    422             if (ret == null)
    423                 return defaultValue;
    424             else
    425                 return ret;
     422            return Optional.ofNullable(OsmUtils.getOsmBoolean(osm.get(key))).orElse(defaultValue);
    426423        }
    427424
     
    949946            this.type = OsmPrimitiveType.from(type);
    950947            if (this.type == null)
    951                 throw new ParseError(tr("Unknown primitive type: {0}. Allowed values are node, way or relation",
    952                         type));
     948                throw new ParseError(tr("Unknown primitive type: {0}. Allowed values are node, way or relation", type));
    953949        }
    954950
     
    16231619     */
    16241620    public Match parse() throws ParseError {
    1625         Match m = parseExpression();
     1621        Match m = Optional.ofNullable(parseExpression()).orElse(Always.INSTANCE);
    16261622        if (!tokenizer.readIfEqual(Token.EOF))
    16271623            throw new ParseError(tr("Unexpected token: {0}", tokenizer.nextToken()));
    1628         if (m == null)
    1629             m = Always.INSTANCE;
    16301624        Main.debug("Parsed search expression is {0}", m);
    16311625        return m;
     
    17581752
    17591753    private Match parseFactor(String errorMessage) throws ParseError {
    1760         Match fact = parseFactor();
    1761         if (fact == null)
    1762             throw new ParseError(errorMessage);
    1763         else
    1764             return fact;
     1754        return Optional.ofNullable(parseFactor()).orElseThrow(() -> new ParseError(errorMessage));
    17651755    }
    17661756
Note: See TracChangeset for help on using the changeset viewer.