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/io
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/DiffResultProcessor.java

    r10818 r11553  
    1212import java.util.HashSet;
    1313import java.util.Map;
     14import java.util.Optional;
    1415import java.util.Set;
    1516
     
    6162     */
    6263    public DiffResultProcessor(Collection<? extends OsmPrimitive> primitives) {
    63         if (primitives == null) {
    64             primitives = Collections.emptyList();
    65         }
    66         this.primitives = primitives;
     64        this.primitives = Optional.ofNullable(primitives).orElseGet(Collections::emptyList);
    6765        this.processed = new HashSet<>();
    6866    }
  • trunk/src/org/openstreetmap/josm/io/GpxExporter.java

    r11035 r11553  
    1313import java.text.MessageFormat;
    1414import java.time.Year;
     15import java.util.Optional;
    1516
    1617import javax.swing.JButton;
     
    223224        if (enable) {
    224225            if (copyrightYear.getText().isEmpty()) {
    225                 String sCopyrightYear = data.getString(META_COPYRIGHT_YEAR);
    226                 if (sCopyrightYear == null) {
    227                     sCopyrightYear = Year.now().toString();
    228                 }
    229                 copyrightYear.setText(sCopyrightYear);
     226                copyrightYear.setText(Optional.ofNullable(data.getString(META_COPYRIGHT_YEAR)).orElseGet(
     227                        () -> Year.now().toString()));
    230228            }
    231229            if (copyright.getText().isEmpty()) {
    232                 String sCopyright = data.getString(META_COPYRIGHT_LICENSE);
    233                 if (sCopyright == null) {
    234                     sCopyright = Main.pref.get("lastCopyright", "https://creativecommons.org/licenses/by-sa/2.5");
    235                 }
    236                 copyright.setText(sCopyright);
     230                copyright.setText(Optional.ofNullable(data.getString(META_COPYRIGHT_LICENSE)).orElseGet(
     231                        () -> Main.pref.get("lastCopyright", "https://creativecommons.org/licenses/by-sa/2.5")));
    237232                copyright.setCaretPosition(0);
    238233            }
     
    282277            emailLabel.setEnabled(b);
    283278            if (b) {
    284                 String sAuthorName = data.getString(META_AUTHOR_NAME);
    285                 if (sAuthorName == null) {
    286                     sAuthorName = Main.pref.get("lastAuthorName");
    287                 }
    288                 authorName.setText(sAuthorName);
    289                 String sEmail = data.getString(META_AUTHOR_EMAIL);
    290                 if (sEmail == null) {
    291                     sEmail = Main.pref.get("lastAuthorEmail");
    292                 }
    293                 email.setText(sEmail);
     279                authorName.setText(Optional.ofNullable(data.getString(META_AUTHOR_NAME)).orElseGet(() -> Main.pref.get("lastAuthorName")));
     280                email.setText(Optional.ofNullable(data.getString(META_AUTHOR_EMAIL)).orElseGet(() -> Main.pref.get("lastAuthorEmail")));
    294281            } else {
    295282                authorName.setText("");
  • trunk/src/org/openstreetmap/josm/io/NmeaReader.java

    r11374 r11553  
    1313import java.util.Collections;
    1414import java.util.Date;
     15import java.util.Optional;
    1516
    1617import org.openstreetmap.josm.Main;
     
    112113
    113114    private Date readTime(String p) {
    114         Date d = rmcTimeFmt.parse(p, new ParsePosition(0));
    115         if (d == null) {
    116             d = rmcTimeFmtStd.parse(p, new ParsePosition(0));
    117         }
     115        Date d = Optional.ofNullable(rmcTimeFmt.parse(p, new ParsePosition(0)))
     116                .orElseGet(() -> rmcTimeFmtStd.parse(p, new ParsePosition(0)));
    118117        if (d == null)
    119118            throw new JosmRuntimeException("Date is malformed");
  • trunk/src/org/openstreetmap/josm/io/NoteReader.java

    r11453 r11553  
    1010import java.util.List;
    1111import java.util.Locale;
     12import java.util.Optional;
    1213
    1314import javax.xml.parsers.ParserConfigurationException;
     
    110111                break;
    111112            case "comment":
    112                 String uidStr = attrs.getValue("uid");
    113                 if (uidStr == null) {
    114                     commentUid = 0;
    115                 } else {
    116                     commentUid = Long.parseLong(uidStr);
    117                 }
     113                commentUid = Long.parseLong(Optional.ofNullable(attrs.getValue("uid")).orElse("0"));
    118114                commentUsername = attrs.getValue("user");
    119115                noteAction = Action.valueOf(attrs.getValue("action").toUpperCase(Locale.ENGLISH));
    120116                commentCreateDate = DateUtils.fromString(attrs.getValue("timestamp"));
    121                 String isNew = attrs.getValue("is_new");
    122                 if (isNew == null) {
    123                     commentIsNew = false;
    124                 } else {
    125                     commentIsNew = Boolean.parseBoolean(isNew);
    126                 }
     117                commentIsNew = Boolean.parseBoolean(Optional.ofNullable(attrs.getValue("is_new")).orElse("false"));
    127118                break;
    128119            default: // Do nothing
  • trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java

    r9227 r11553  
    77import java.io.InputStream;
    88import java.net.URLConnection;
     9import java.util.Optional;
    910
    1011import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     
    3132    public ProgressInputStream(InputStream in, long size, ProgressMonitor progressMonitor) {
    3233        CheckParameterUtil.ensureParameterNotNull(in, "in");
    33         if (progressMonitor == null) {
    34             progressMonitor = NullProgressMonitor.INSTANCE;
    35         }
    36         this.updater = new StreamProgressUpdater(size, progressMonitor, tr("Downloading data..."));
     34        this.updater = new StreamProgressUpdater(size,
     35                Optional.ofNullable(progressMonitor).orElse(NullProgressMonitor.INSTANCE), tr("Downloading data..."));
    3736        this.in = in;
    3837    }
  • trunk/src/org/openstreetmap/josm/io/StreamProgressUpdater.java

    r10161 r11553  
    33
    44import java.util.Locale;
     5import java.util.Optional;
    56
    67import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     
    1718
    1819    StreamProgressUpdater(long size, ProgressMonitor progressMonitor, String taskTitle) {
    19         if (progressMonitor == null) {
    20             progressMonitor = NullProgressMonitor.INSTANCE;
    21         }
    2220        this.size = size;
    23         this.progressMonitor = progressMonitor;
     21        this.progressMonitor = Optional.ofNullable(progressMonitor).orElse(NullProgressMonitor.INSTANCE);
    2422        this.taskTitle = taskTitle;
    2523        initProgressMonitor();
  • trunk/src/org/openstreetmap/josm/io/UTFInputStreamReader.java

    r10182 r11553  
    77import java.io.PushbackInputStream;
    88import java.io.UnsupportedEncodingException;
     9import java.util.Optional;
    910
    1011/**
     
    6768            pushbackStream.unread(bom, 0, 0);
    6869        }
    69 
    70         if (encoding == null) {
    71             encoding = "UTF-8";
    72         }
    73         return new UTFInputStreamReader(pushbackStream, encoding);
     70        return new UTFInputStreamReader(pushbackStream, Optional.ofNullable(encoding).orElse("UTF-8"));
    7471    }
    7572}
  • trunk/src/org/openstreetmap/josm/io/XmlStreamParsingException.java

    r10235 r11553  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.util.Optional;
    57
    68import javax.xml.stream.Location;
     
    3739    @Override
    3840    public String getMessage() {
    39         String msg = super.getMessage();
    40         if (msg == null) {
    41             msg = getClass().getName();
    42         }
     41        String msg = Optional.ofNullable(super.getMessage()).orElseGet(() -> getClass().getName());
    4342        if (getLocation() == null)
    4443            return msg;
Note: See TracChangeset for help on using the changeset viewer.