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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r11383 r11553  
    99import java.util.List;
    1010import java.util.Map;
     11import java.util.Optional;
    1112import java.util.Set;
    1213import java.util.stream.Collectors;
     
    264265            List<RelationMember> newMembers = new ArrayList<>();
    265266            for (RelationMemberData member : relationData.getMembers()) {
    266                 OsmPrimitive primitive = getDataSet().getPrimitiveById(member);
    267                 if (primitive == null)
    268                     throw new AssertionError("Data consistency problem - relation with missing member detected");
    269                 newMembers.add(new RelationMember(member.getRole(), primitive));
     267                newMembers.add(new RelationMember(member.getRole(), Optional.ofNullable(getDataSet().getPrimitiveById(member))
     268                        .orElseThrow(() -> new AssertionError("Data consistency problem - relation with missing member detected"))));
    270269            }
    271270            setMembers(newMembers);
  • trunk/src/org/openstreetmap/josm/data/osm/RelationMember.java

    r10662 r11553  
    44import java.util.Arrays;
    55import java.util.Objects;
     6import java.util.Optional;
    67
    78import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    133134    public RelationMember(String role, OsmPrimitive member) {
    134135        CheckParameterUtil.ensureParameterNotNull(member, "member");
    135         if (role == null) {
    136             role = "";
    137         }
    138         this.role = role;
     136        this.role = Optional.ofNullable(role).orElse("");
    139137        this.member = member;
    140138    }
     
    150148    }
    151149
    152     @Override public String toString() {
     150    @Override
     151    public String toString() {
    153152        return '"' + role + "\"=" + member;
    154153    }
  • trunk/src/org/openstreetmap/josm/data/osm/TagMap.java

    r10755 r11553  
    1313import java.util.Map;
    1414import java.util.NoSuchElementException;
     15import java.util.Objects;
    1516import java.util.Set;
    1617
     
    193194    @Override
    194195    public synchronized String put(String key, String value) {
    195         if (key == null) {
    196             throw new NullPointerException();
    197         }
    198         if (value == null) {
    199             throw new NullPointerException();
    200         }
     196        Objects.requireNonNull(key);
     197        Objects.requireNonNull(value);
    201198        int index = indexOfKey(tags, key);
    202199        int newTagArrayLength = tags.length;
  • trunk/src/org/openstreetmap/josm/data/osm/event/DataChangedEvent.java

    r5170 r11553  
    1313    private final List<AbstractDatasetChangedEvent> events;
    1414
     15    /**
     16     * Constructs a new {@code DataChangedEvent}
     17     * @param dataSet data set
     18     * @param events list of change events
     19     */
    1520    public DataChangedEvent(DataSet dataSet, List<AbstractDatasetChangedEvent> events) {
    1621        super(dataSet);
     
    1823    }
    1924
     25    /**
     26     * Constructs a new {@code DataChangedEvent}
     27     * @param dataSet data set
     28     */
    2029    public DataChangedEvent(DataSet dataSet) {
    2130        this(dataSet, null);
     
    2938    @Override
    3039    public Collection<OsmPrimitive> getPrimitives() {
    31         if (dataSet == null)
    32             return Collections.emptyList();
    33         else
    34             return dataSet.allPrimitives();
     40        return dataSet == null ? Collections.emptyList() : dataSet.allPrimitives();
    3541    }
    3642
     
    4147
    4248    /**
    43      *
     49     * Returns list of events that caused this DataChangedEvent.
    4450     * @return List of events that caused this DataChangedEvent. Might be null
    4551     */
     
    4753        return events;
    4854    }
    49 
    5055}
  • trunk/src/org/openstreetmap/josm/data/osm/history/HistoryOsmPrimitive.java

    r10378 r11553  
    197197
    198198    /**
    199      * Sets the tags for this history primitive. Removes all
    200      * tags if <code>tags</code> is null.
     199     * Sets the tags for this history primitive. Removes all tags if <code>tags</code> is null.
    201200     *
    202201     * @param tags the tags. May be null.
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/PaintColors.java

    r10874 r11553  
    66import java.awt.Color;
    77import java.util.List;
     8import java.util.Optional;
    89
    910import org.openstreetmap.josm.data.preferences.CachingProperty;
     
    7980    }
    8081
     82    /**
     83     * Returns the background color.
     84     * @return the background color
     85     */
    8186    public static Color getBackgroundColor() {
    8287        if (backgroundColorCache != null)
     
    9297            }
    9398        }
    94         if (backgroundColorCache == null) {
    95             return BACKGROUND.get();
    96         } else {
    97             return backgroundColorCache;
    98         }
     99        return Optional.ofNullable(backgroundColorCache).orElseGet(BACKGROUND::get);
    99100    }
    100101
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    r11452 r11553  
    11181118                    lastNode = tmp;
    11191119                } else {
    1120                     onewayvia = OsmUtils.getOsmBoolean(onewayviastr);
    1121                     if (onewayvia == null) {
    1122                         onewayvia = Boolean.FALSE;
    1123                     }
     1120                    onewayvia = Optional.ofNullable(OsmUtils.getOsmBoolean(onewayviastr)).orElse(Boolean.FALSE);
    11241121                }
    11251122            }
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/relations/Multipolygon.java

    r11385 r11553  
    1111import java.util.Iterator;
    1212import java.util.List;
     13import java.util.Optional;
    1314import java.util.Set;
    1415
     
    668669
    669670            for (PolyData pdInner: innerPolygons) {
    670                 PolyData o = findOuterPolygon(pdInner, combinedPolygons);
    671                 if (o == null) {
    672                     o = outerPolygons.get(0);
    673                 }
    674                 o.addInner(pdInner);
     671                Optional.ofNullable(findOuterPolygon(pdInner, combinedPolygons)).orElseGet(() -> outerPolygons.get(0))
     672                    .addInner(pdInner);
    675673            }
    676674        }
Note: See TracChangeset for help on using the changeset viewer.