Changeset 32659 in osm for applications/editors/josm


Ignore:
Timestamp:
2016-07-15T15:25:20+02:00 (8 years ago)
Author:
floscher
Message:

[mapillary] Rework join and unjoin command

Location:
applications/editors/josm/plugins/mapillary
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/history/commands/CommandJoin.java

    r31972 r32659  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.util.List;
     6import java.util.Arrays;
    77import java.util.concurrent.ConcurrentSkipListSet;
    88
     
    3030   *           if the List size is different from 2.
    3131   */
    32   public CommandJoin(List<MapillaryAbstractImage> images) {
    33     super(new ConcurrentSkipListSet<>(images));
    34     a = images.get(0);
    35     b = images.get(1);
    36     if (images.size() != 2)
    37       throw new IllegalArgumentException();
     32  public CommandJoin(final MapillaryAbstractImage a, final MapillaryAbstractImage b) {
     33    super(new ConcurrentSkipListSet<>(Arrays.asList(new MapillaryAbstractImage[]{a, b}))); // throws NPE if a or b is null
     34    if (a.getSequence() == b.getSequence()) {
     35      throw new IllegalArgumentException("Both images must be in different sequences for joining.");
     36    }
     37    this.a = a;
     38    this.b = b;
    3839  }
    3940
    4041  @Override
    4142  public void execute() {
    42     this.redo();
     43    redo();
    4344  }
    4445
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/mode/JoinMode.java

    r32383 r32659  
    5151        (this.data.getHighlightedImage().previous() == null && this.lastClick.next() == null
    5252          || this.data.getHighlightedImage().next() == null && this.lastClick.previous() == null)
    53         && (this.data.getHighlightedImage().getSequence() != this.lastClick.getSequence() || this.lastClick.getSequence() == null)
     53        && this.data.getHighlightedImage().getSequence() != this.lastClick.getSequence()
    5454      ) {
    55 
    56         MapillaryRecord.getInstance().addCommand(
    57             new CommandJoin(Arrays.asList(new MapillaryAbstractImage[] {
    58                 this.lastClick, this.data.getHighlightedImage() })));
     55        MapillaryRecord.getInstance().addCommand(new CommandJoin(this.lastClick, this.data.getHighlightedImage()));
    5956      } else if (this.lastClick.next() == this.data.getHighlightedImage()
    6057          || this.lastClick.previous() == this.data.getHighlightedImage()) {
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryUtils.java

    r32593 r32659  
    155155   * Joins two images into the same sequence. One of them must be the last image of a sequence, the other one the beginning of a different one.
    156156   *
    157    * @param mapillaryAbstractImage the first image, into whose sequence the images from the sequence of the second image are merged
    158    * @param mapillaryAbstractImage2 the second image, whose sequence is merged into the sequence of the first image
    159    */
    160   public static synchronized void join(
    161           MapillaryAbstractImage mapillaryAbstractImage,
    162           MapillaryAbstractImage mapillaryAbstractImage2) {
    163     MapillaryAbstractImage firstImage = mapillaryAbstractImage;
    164     MapillaryAbstractImage secondImage = mapillaryAbstractImage2;
    165 
    166     if (mapillaryAbstractImage.next() != null) {
    167       firstImage = mapillaryAbstractImage2;
    168       secondImage = mapillaryAbstractImage;
    169     }
    170     if (firstImage.getSequence() == null) {
    171       MapillarySequence seq = new MapillarySequence();
    172       seq.add(firstImage);
    173       firstImage.setSequence(seq);
    174     }
    175     if (secondImage.getSequence() == null) {
    176       MapillarySequence seq = new MapillarySequence();
    177       seq.add(secondImage);
    178       mapillaryAbstractImage2.setSequence(seq);
    179     }
    180 
    181     for (MapillaryAbstractImage img : secondImage.getSequence().getImages()) {
    182       firstImage.getSequence().add(img);
    183       img.setSequence(firstImage.getSequence());
    184     }
    185     if (Main.main != null)
    186       MapillaryData.dataUpdated();
     157   * @param imgA the first image, into whose sequence the images from the sequence of the second image are merged
     158   * @param imgB the second image, whose sequence is merged into the sequence of the first image
     159   */
     160  public static synchronized void join(MapillaryAbstractImage imgA, MapillaryAbstractImage imgB) {
     161    if (imgA == null || imgB == null) {
     162      throw new IllegalArgumentException("Both images must be non-null for joining.");
     163    }
     164    if (imgA.getSequence() == imgB.getSequence()) {
     165      throw new IllegalArgumentException("You can only join images of different sequences.");
     166    }
     167    if ((imgA.next() != null || imgB.previous() != null) && (imgB.next() != null || imgA.previous() != null)) {
     168      throw new IllegalArgumentException("You can only join an image at the end of a sequence with one at the beginning of another sequence.");
     169    }
     170    if (imgA.next() != null || imgB.previous() != null) {
     171      join(imgB, imgA);
     172    } else {
     173      for (MapillaryAbstractImage img : imgB.getSequence().getImages()) {
     174        imgA.getSequence().add(img);
     175        img.setSequence(imgA.getSequence());
     176      }
     177      if (Main.main != null) {
     178        MapillaryData.dataUpdated();
     179      }
     180    }
    187181  }
    188182
     
    240234   * All others are put into the second new sequence.
    241235   *
    242    * @param mapillaryAbstractImage one of the images marking where to split the sequence
    243    * @param mapillaryAbstractImage2 the other image marking where to split the sequence, needs to be a direct neighbour of {@code mapillaryAbstractImage} in the sequence.
    244    */
    245   public static synchronized void unjoin(
    246           MapillaryAbstractImage mapillaryAbstractImage,
    247           MapillaryAbstractImage mapillaryAbstractImage2) {
    248     MapillaryAbstractImage firstImage = mapillaryAbstractImage;
    249     MapillaryAbstractImage secondImage = mapillaryAbstractImage2;
    250 
    251     if (mapillaryAbstractImage.next() != mapillaryAbstractImage2) {
    252       firstImage = mapillaryAbstractImage2;
    253       secondImage = mapillaryAbstractImage;
    254     }
    255 
    256     ArrayList<MapillaryAbstractImage> firstHalf = new ArrayList<>(
    257             firstImage.getSequence().getImages().subList(0,
    258                     firstImage.getSequence().getImages().indexOf(secondImage)));
    259     ArrayList<MapillaryAbstractImage> secondHalf = new ArrayList<>(
    260             firstImage.getSequence().getImages().subList(
    261                     firstImage.getSequence().getImages().indexOf(secondImage),
    262                     firstImage.getSequence().getImages().size()));
    263 
    264     MapillarySequence seq1 = new MapillarySequence();
    265     MapillarySequence seq2 = new MapillarySequence();
    266 
    267     for (MapillaryAbstractImage img : firstHalf) {
    268       img.setSequence(seq1);
    269       seq1.add(img);
    270     }
    271     for (MapillaryAbstractImage img : secondHalf) {
    272       img.setSequence(seq2);
    273       seq2.add(img);
    274     }
    275     if (Main.main != null)
    276       MapillaryData.dataUpdated();
     236   * @param imgA one of the images marking where to split the sequence
     237   * @param imgB the other image marking where to split the sequence, needs to be a direct neighbour of {@code imgA} in the sequence.
     238   */
     239  public static synchronized void unjoin(MapillaryAbstractImage imgA, MapillaryAbstractImage imgB) {
     240    if (imgA == null || imgB == null) {
     241      throw new IllegalArgumentException("Both images must be non-null for unjoining.");
     242    }
     243    if (imgA.getSequence() != imgB.getSequence()) {
     244      throw new IllegalArgumentException("You can only unjoin with two images from the same sequence.");
     245    }
     246    if (imgB.equals(imgA.next()) && imgA.equals(imgB.next())) {
     247      throw new IllegalArgumentException("When unjoining with two images these must be consecutive in one sequence.");
     248    }
     249
     250    if (imgA.equals(imgB.next())) {
     251      unjoin(imgB, imgA);
     252    } else {
     253      MapillarySequence seqA = new MapillarySequence();
     254      MapillarySequence seqB = new MapillarySequence();
     255      boolean insideFirstHalf = true;
     256      for (MapillaryAbstractImage img : imgA.getSequence().getImages()) {
     257        if (insideFirstHalf) {
     258          img.setSequence(seqA);
     259          seqA.add(img);
     260        } else {
     261          img.setSequence(seqB);
     262          seqB.add(img);
     263        }
     264        if (img.equals(imgA)) {
     265          insideFirstHalf = false;
     266        }
     267      }
     268      if (Main.main != null) {
     269        MapillaryData.dataUpdated();
     270      }
     271    }
    277272  }
    278273
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/history/MapillaryRecordTest.java

    r32653 r32659  
    177177  @Test
    178178  public void commandJoinClass() {
    179     CommandJoin cmd1 = new CommandJoin(Arrays.asList(new MapillaryAbstractImage[]{img1, img2}));
    180     CommandJoin cmd2 = new CommandJoin(Arrays.asList(new MapillaryAbstractImage[]{img2, img3}));
     179    CommandJoin cmd1 = new CommandJoin(img1, img2);
     180    CommandJoin cmd2 = new CommandJoin(img2, img3);
    181181
    182182    this.record.addCommand(cmd1);
     
    189189    assertEquals(3, img1.getSequence().getImages().size());
    190190    assertEquals(img3, img1.next().next());
    191 
    192     try {
    193       this.record.addCommand(new CommandJoin(Arrays.asList(new MapillaryAbstractImage[]{img1, img2, img3})));
    194       fail();
    195     } catch (IllegalArgumentException e) {
    196       // Expected output.
    197     }
     191  }
     192 
     193  @Test(expected=NullPointerException.class)
     194  public void commandJoinNull1() {
     195    new CommandJoin(img1, null);
     196  }
     197 
     198  @Test(expected=NullPointerException.class)
     199  public void commandJoinNull2() {
     200    new CommandJoin(null, img1);
    198201  }
    199202
     
    203206  @Test
    204207  public void commandUnjoinClass() {
    205     CommandJoin join1 = new CommandJoin(
    206             Arrays.asList(new MapillaryAbstractImage[]{this.img1, this.img2}));
    207     CommandJoin join2 = new CommandJoin(
    208             Arrays.asList(new MapillaryAbstractImage[]{this.img2, this.img3}));
     208    CommandJoin join1 = new CommandJoin(this.img1, this.img2);
     209    CommandJoin join2 = new CommandJoin(this.img2, this.img3);
    209210
    210211    CommandUnjoin cmd1 = new CommandUnjoin(
     
    238239  @Test
    239240  public void commandDeleteTest() {
    240     CommandJoin join1 = new CommandJoin(Arrays.asList(new MapillaryAbstractImage[]{img1, img2}));
    241     CommandJoin join2 = new CommandJoin(Arrays.asList(new MapillaryAbstractImage[]{img2, img3}));
     241    CommandJoin join1 = new CommandJoin(img1, img2);
     242    CommandJoin join2 = new CommandJoin(img2, img3);
    242243
    243244    CommandDelete cmd1 = new CommandDelete(
Note: See TracChangeset for help on using the changeset viewer.