@Override public double get(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop) { return get(fromStop, toStop, Double.NaN); }
@Override public double set(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop, double seconds) { if (Double.isNaN(seconds) || Time.isUndefinedTime(seconds)) { return remove(fromStop, toStop); } Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.computeIfAbsent(fromStop, key -> new ConcurrentHashMap<>()); Double value = innerMap.put(toStop, seconds); if (value == null) { return Double.NaN; } return 0; }
@Test public void testGetWithDefault() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); double defaultSeconds = 60.0; Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(60.0, mtt.get(this.stopId1, this.stopId2, defaultSeconds), 0.0); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2, defaultSeconds), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3, defaultSeconds), 0.0); Assert.assertEquals(defaultSeconds, mtt.get(this.stopId1, this.stopId4, defaultSeconds), 0.0); }
@Test public void testNotTransitive() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId2, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId2, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId3, this.stopId1), 0.0); mtt.set(this.stopId1, this.stopId3, 480.0); Assert.assertEquals(480.0, mtt.get(this.stopId1, this.stopId3), 0.0); }
@Test public void testSetGet() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); // overwrite a value mtt.set(this.stopId1, this.stopId2, 300.0); Assert.assertEquals(300.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId4), 0.0); }
@Test public void testNotBidirection() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId2, this.stopId1), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId3, this.stopId1), 0.0); mtt.set(this.stopId3, this.stopId1, 120.0); Assert.assertEquals(120.0, mtt.get(this.stopId3, this.stopId1), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); }
@Test public void testIterator_empty() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); MinimalTransferTimes.MinimalTransferTimesIterator iter = mtt.iterator(); Assert.assertFalse(iter.hasNext()); try { iter.getFromStopId(); Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} try { iter.getToStopId(); Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} try { iter.getSeconds(); Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} try { iter.next(); Assert.fail("expected Exception"); } catch (NoSuchElementException expected) {} try { iter.getFromStopId(); // there should still be an exception after calling next() Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} }
@Test public void testRemove() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(180.0, mtt.remove(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.remove(this.stopId1, this.stopId4), 0.0); // we never set it, let's not throw an exception Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId4), 0.0); }
@Test public void testIterator_withRemove() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); mtt.set(this.stopId1, this.stopId2, 180); mtt.set(this.stopId2, this.stopId3, 240);
@Test public void testIterator() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl();