Changeset 9711 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-02-01T23:47:27+01:00 (9 years ago)
Author:
simon04
Message:

see #12300 - Provide wrapper classes for transfer data to avoid unchecked casts

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/datatransfer/PrimitiveTransferable.java

    r9563 r9711  
    1919
    2020    /**
    21      * Data flavor for {@link PrimitiveData}.
     21     * A wrapper for a collection of {@link PrimitiveData}.
    2222     */
    23     public static final DataFlavor PRIMITIVE_DATA = new DataFlavor(PrimitiveData.class, PrimitiveData.class.getName());
     23    public static final class Data {
     24        private final Collection<PrimitiveData> primitiveData;
     25
     26        private Data(Collection<PrimitiveData> primitiveData) {
     27            this.primitiveData = primitiveData;
     28        }
     29
     30        /**
     31         * Returns the contained {@link PrimitiveData}
     32         * @return the contained {@link PrimitiveData}
     33         */
     34        public Collection<PrimitiveData> getPrimitiveData() {
     35            return primitiveData;
     36        }
     37    }
     38
     39    /**
     40     * Data flavor for {@link PrimitiveData} which is wrapped in {@link Data}.
     41     */
     42    public static final DataFlavor PRIMITIVE_DATA = new DataFlavor(Data.class, Data.class.getName());
    2443    private final Collection<? extends OsmPrimitive> primitives;
    2544
     
    4766            return getStringData();
    4867        } else if (PRIMITIVE_DATA.equals(flavor)) {
    49             return getRelationMemberData();
     68            return getPrimitiveData();
    5069        }
    5170        throw new UnsupportedFlavorException(flavor);
     
    6382    }
    6483
    65     protected Collection<PrimitiveData> getRelationMemberData() {
     84    protected Data getPrimitiveData() {
    6685        final Collection<PrimitiveData> r = new ArrayList<>(primitives.size());
    6786        for (OsmPrimitive primitive : primitives) {
    6887            r.add(primitive.save());
    6988        }
    70         return r;
     89        return new Data(r);
    7190    }
    7291}
  • trunk/src/org/openstreetmap/josm/gui/datatransfer/RelationMemberTransferable.java

    r9563 r9711  
    1919
    2020    /**
    21      * Data flavor for {@link RelationMemberData}.
     21     * A wrapper for a collection of {@link RelationMemberData}.
    2222     */
    23     public static final DataFlavor RELATION_MEMBER_DATA = new DataFlavor(RelationMemberData.class, RelationMemberData.class.getName());
     23    public static final class Data {
     24        private final Collection<RelationMemberData> relationMemberDatas;
     25
     26        private Data(Collection<RelationMemberData> primitiveData) {
     27            this.relationMemberDatas = primitiveData;
     28        }
     29
     30        /**
     31         * Returns the contained {@link RelationMemberData}
     32         * @return the contained {@link RelationMemberData}
     33         */
     34        public Collection<RelationMemberData> getRelationMemberData() {
     35            return relationMemberDatas;
     36        }
     37    }
     38
     39    /**
     40     * Data flavor for {@link RelationMemberData} which is wrapped in {@link Data}.
     41     */
     42    public static final DataFlavor RELATION_MEMBER_DATA = new DataFlavor(Data.class, Data.class.getName());
    2443    private final Collection<RelationMember> members;
    2544
     
    6483    }
    6584
    66     protected Collection<RelationMemberData> getRelationMemberData() {
     85    protected Data getRelationMemberData() {
    6786        final Collection<RelationMemberData> r = new ArrayList<>(members.size());
    6887        for (RelationMember member : members) {
    6988            r.add(new RelationMemberData(member.getRole(), member.getType(), member.getUniqueId()));
    7089        }
    71         return r;
     90        return new Data(r);
    7291    }
    7392}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTransferHandler.java

    r9379 r9711  
    6464    protected void importRelationMemberData(TransferSupport support, final MemberTable destination, int insertRow)
    6565            throws UnsupportedFlavorException, IOException {
    66         //noinspection unchecked
    67         @SuppressWarnings("unchecked")
    68         final Collection<RelationMemberData> memberData = (Collection<RelationMemberData>)
     66        final RelationMemberTransferable.Data memberData = (RelationMemberTransferable.Data)
    6967                support.getTransferable().getTransferData(RelationMemberTransferable.RELATION_MEMBER_DATA);
    70         importData(destination, insertRow, memberData, new Function<RelationMemberData, RelationMember>() {
     68        importData(destination, insertRow, memberData.getRelationMemberData(), new Function<RelationMemberData, RelationMember>() {
    7169            @Override
    7270            public RelationMember apply(RelationMemberData member) {
     
    8482    protected void importPrimitiveData(TransferSupport support, final MemberTable destination, int insertRow)
    8583            throws UnsupportedFlavorException, IOException {
    86         //noinspection unchecked
    87         @SuppressWarnings("unchecked")
    88         final Collection<PrimitiveData> data = (Collection<PrimitiveData>)
     84        final PrimitiveTransferable.Data data = (PrimitiveTransferable.Data)
    8985                support.getTransferable().getTransferData(PrimitiveTransferable.PRIMITIVE_DATA);
    90         importData(destination, insertRow, data, new Function<PrimitiveData, RelationMember>() {
     86        importData(destination, insertRow, data.getPrimitiveData(), new Function<PrimitiveData, RelationMember>() {
    9187            @Override
    9288            public RelationMember apply(PrimitiveData data) {
Note: See TracChangeset for help on using the changeset viewer.