Skip to content

Commit

Permalink
guarantees sorted modes per link
Browse files Browse the repository at this point in the history
fixes #3134
  • Loading branch information
markusstraub committed Mar 4, 2024
1 parent a4b2eca commit ad79705
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions matsim/src/main/java/org/matsim/core/network/LinkImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.matsim.utils.objectattributes.attributable.Attributes;
import org.matsim.utils.objectattributes.attributable.AttributesImpl;

import com.google.common.collect.ImmutableSortedSet;

/*deliberately package*/ class LinkImpl implements Link {

private final static Logger log = LogManager.getLogger(Link.class);
Expand Down Expand Up @@ -131,7 +133,7 @@ private void checkLengthSemantics(){
}
}



//////////////////////////////////////////////////////////////////////
// get methods
Expand Down Expand Up @@ -184,7 +186,7 @@ public double getCapacityPeriod() {
}

// ---

@Override
public double getFreespeed() {
return this.freespeed;
Expand Down Expand Up @@ -281,7 +283,7 @@ public static Set<String> get(final Set<String> set) {
if (set == null) {
return null;
}
return cache.computeIfAbsent(set.hashCode(), key -> Set.copyOf(set));
return cache.computeIfAbsent(set.hashCode(), key -> ImmutableSortedSet.copyOf(set));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public abstract class AbstractNetworkWriterReaderTest {
*/
protected abstract void readNetwork(final Scenario scenario, final InputStream stream);

@Test
void testAllowedModes_manyUnsortedModes() {
doTestAllowedModes(createHashSet("train", "drt", "car", "bus", "walk", "freight"),
utils.getOutputDirectory() + "network.xml");
}

@Test
void testAllowedModes_multipleModes() {
doTestAllowedModes(createHashSet("bus", "train"), utils.getOutputDirectory() + "network.xml");
Expand Down Expand Up @@ -245,6 +251,19 @@ private void doTestAllowedModes(final Set<String> modes, final String filename)
Set<String> modes2 = link1.getAllowedModes();
assertEquals(modes.size(), modes2.size(), "wrong number of allowed modes.");
assertTrue(modes2.containsAll(modes), "wrong mode.");

assertModesSorted(modes2);
}

private static void assertModesSorted(Set<String> modes) {
List<String> originalModes = new ArrayList<>(modes);
List<String> sortedModes = new ArrayList<>(modes);
Collections.sort(sortedModes);

for (int i = 0; i < originalModes.size(); i++) {
assertEquals(sortedModes.get(i), originalModes.get(i),
"modes are not sorted. expected '" + sortedModes + "'' but got '" + originalModes + "'");
}
}

private static <T> Set<T> createHashSet(T... mode) {
Expand Down

0 comments on commit ad79705

Please sign in to comment.