Changeset 9711 in josm


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

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

Location:
trunk
Files:
5 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) {
  • trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/PrimitiveTransferableTest.java

    r9669 r9711  
    44import static org.junit.Assert.assertEquals;
    55import static org.junit.Assert.assertFalse;
    6 import static org.junit.Assert.assertNotNull;
    76import static org.junit.Assert.assertTrue;
    87import static org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable.PRIMITIVE_DATA;
     
    5756     */
    5857    @Test
    59     @SuppressWarnings("unchecked")
    6058    public void testGetTransferDataNominal() throws UnsupportedFlavorException {
    6159        PrimitiveTransferable pt = new PrimitiveTransferable(Collections.singleton(new Node(1)));
    6260        assertEquals("node 1 # incomplete\n", pt.getTransferData(DataFlavor.stringFlavor));
    63         Collection<PrimitiveData> td = (Collection<PrimitiveData>) pt.getTransferData(PRIMITIVE_DATA);
     61        Collection<PrimitiveData> td = ((PrimitiveTransferable.Data) pt.getTransferData(PRIMITIVE_DATA)).getPrimitiveData();
    6462        assertEquals(1, td.size());
    65         assertNotNull(td.iterator().next());
     63        assertTrue(td.iterator().next() instanceof PrimitiveData);
    6664    }
    6765
  • trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/RelationMemberTransferableTest.java

    r9669 r9711  
    44import static org.junit.Assert.assertEquals;
    55import static org.junit.Assert.assertFalse;
    6 import static org.junit.Assert.assertNotNull;
    76import static org.junit.Assert.assertTrue;
    87import static org.openstreetmap.josm.gui.datatransfer.RelationMemberTransferable.RELATION_MEMBER_DATA;
     
    1716import org.openstreetmap.josm.JOSMFixture;
    1817import org.openstreetmap.josm.data.osm.Node;
    19 import org.openstreetmap.josm.data.osm.PrimitiveData;
    2018import org.openstreetmap.josm.data.osm.RelationMember;
     19import org.openstreetmap.josm.data.osm.RelationMemberData;
    2120
    2221/**
     
    5857     */
    5958    @Test
    60     @SuppressWarnings("unchecked")
    6159    public void testGetTransferDataNominal() throws UnsupportedFlavorException {
    6260        RelationMemberTransferable rmt = new RelationMemberTransferable(Collections.singleton(new RelationMember("test", new Node(1))));
    6361        assertEquals("node 1 test # incomplete\n", rmt.getTransferData(DataFlavor.stringFlavor));
    64         Collection<PrimitiveData> td = (Collection<PrimitiveData>) rmt.getTransferData(RELATION_MEMBER_DATA);
     62        Collection<RelationMemberData> td = ((RelationMemberTransferable.Data) rmt.getTransferData(RELATION_MEMBER_DATA)).getRelationMemberData();
    6563        assertEquals(1, td.size());
    66         assertNotNull(td.iterator().next());
     64        assertTrue(td.iterator().next() instanceof RelationMemberData);
    6765    }
    6866
Note: See TracChangeset for help on using the changeset viewer.