Skip to content

Commit

Permalink
FIXUP to 'Storage structure for passthrough-points used in a search.'
Browse files Browse the repository at this point in the history
Fix API in BitSetPassthroughPoints to not use code outside Raptor package.
  • Loading branch information
Johan Torin authored and bartosz committed Jul 20, 2023
1 parent bec6ef2 commit e9f2e4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import org.opentripplanner.transit.model.site.StopLocations;

public class BitSetPassthroughPoints implements PassthroughPoints {

Expand All @@ -15,16 +15,16 @@ private BitSetPassthroughPoints(final List<BitSet> passthroughPoints) {
this.passthroughPoints = passthroughPoints;
}

public static PassthroughPoints create(final List<StopLocations> passthroughStops) {
public static PassthroughPoints create(final List<int[]> passthroughStops) {
if (passthroughStops.isEmpty()) {
return PassthroughPoints.NO_PASSTHROUGH_POINTS;
}

return passthroughStops
.stream()
.map(vs -> {
.map(pts -> {
final BitSet tmpBitSet = new BitSet();
vs.stream().forEach(sl -> tmpBitSet.set(sl.getIndex()));
Arrays.stream(pts).forEach(si -> tmpBitSet.set(si));
return tmpBitSet;
})
.collect(collectingAndThen(toList(), vps -> new BitSetPassthroughPoints(vps)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,59 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static shadow.org.assertj.core.util.Lists.emptyList;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.StopLocations;

class BitSetPassthroughPointsTest {

public static final RegularStop STOP_11 = RegularStop.of(FeedScopedId.parseId("1:1")).build();
public static final RegularStop STOP_12 = RegularStop.of(FeedScopedId.parseId("1:2")).build();
public static final RegularStop STOP_13 = RegularStop.of(FeedScopedId.parseId("1:3")).build();
public static final StopLocations STOPLOCATIONS_1 = new StopLocations(
List.of(STOP_11, STOP_12, STOP_13)
);
public static final RegularStop STOP_21 = RegularStop.of(FeedScopedId.parseId("2:1")).build();
public static final RegularStop STOP_22 = RegularStop.of(FeedScopedId.parseId("2:2")).build();
public static final RegularStop STOP_23 = RegularStop.of(FeedScopedId.parseId("2:3")).build();
public static final StopLocations STOPLOCATIONS_2 = new StopLocations(
List.of(STOP_21, STOP_22, STOP_23)
);
public static final RegularStop STOP_31 = RegularStop.of(FeedScopedId.parseId("3:1")).build();
public static final int STOP_11 = 0;
public static final int STOP_12 = 1;
public static final int STOP_13 = 2;
public static final int[] STOPS_1 = new int[] { STOP_11, STOP_12, STOP_13 };
public static final int STOP_21 = 3;
public static final int STOP_22 = 4;
public static final int STOP_23 = 5;
public static final int[] STOPS_2 = new int[] { STOP_21, STOP_22, STOP_23 };
public static final int STOP_31 = 6;
private static PassthroughPoints PASSTHROUGH_POINTS = BitSetPassthroughPoints.create(
List.of(STOPLOCATIONS_1, STOPLOCATIONS_2)
List.of(STOPS_1, STOPS_2)
);
private static PassthroughPoints EMPTY_PASSTHROUGH_POINTS = BitSetPassthroughPoints.create(
emptyList()
);

@Test
void passthroughPoint() {
assertTrue(PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_11.getIndex()));
assertTrue(PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_11));
}

@Test
void passthroughPoint_secondPoint() {
assertTrue(PASSTHROUGH_POINTS.isPassthroughPoint(1, STOP_22.getIndex()));
assertTrue(PASSTHROUGH_POINTS.isPassthroughPoint(1, STOP_22));
}

@Test
void notAPassthroughPoint() {
assertFalse(PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_31.getIndex()));
assertFalse(PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_31));
}

@Test
void notAPassthroughPoint_passthroughPointOnIncorrectPosition() {
assertFalse(PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_21.getIndex()));
assertFalse(PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_21));
}

@Test
void notAPassthroughPoint_incorrectPassthroughPointIndex() {
final IndexOutOfBoundsException indexOutOfBoundsException = assertThrows(
IndexOutOfBoundsException.class,
() -> PASSTHROUGH_POINTS.isPassthroughPoint(99, STOP_11.getIndex())
() -> PASSTHROUGH_POINTS.isPassthroughPoint(99, STOP_11)
);
assertEquals("Index 99 out of bounds for length 2", indexOutOfBoundsException.getMessage());
}

@Test
void notAPassthroughPoint_empty() {
assertFalse(EMPTY_PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_11.getIndex()));
assertFalse(EMPTY_PASSTHROUGH_POINTS.isPassthroughPoint(0, STOP_11));
}
}

0 comments on commit e9f2e4a

Please sign in to comment.