Location implements Serializable { public Location toggleTrailingSlash() { assert !segments.isEmpty(); String lastSegment = segments.get(segments.size() - 1); if (segments.size() == 1 && "".equals(lastSegment)) { throw new IllegalArgumentException( "Can't toggle ending slash for the \"\" location"); } if (lastSegment.isEmpty()) { return new Location(segments.subList(0, segments.size() - 1), queryParameters); } else { List<String> newSegments = new ArrayList<>(segments); newSegments.add(""); return new Location(newSegments, queryParameters); } } Location(String location);  Location(String location, QueryParameters queryParameters);  Location(List<String> segments);  Location(List<String> segments, QueryParameters queryParameters); List<String> getSegments(); QueryParameters getQueryParameters(); String getFirstSegment(); Optional<Location> getSubLocation(); String getPath(); String getPathWithQueryParameters(); Location toggleTrailingSlash();  }
@Test public void toggleTrailingSlash() { assertEquals("foo", new Location("foo/").toggleTrailingSlash().getPath()); assertEquals("foo/", new Location("foo").toggleTrailingSlash().getPath()); }
@Test(expected = IllegalArgumentException.class) public void toggleTrailingSlash_emtpyLocation() { new Location("").toggleTrailingSlash(); }