MMapURI implements Serializable { @Nonnull public MMapURI replaceBaseInPath(final boolean replaceHost, @Nonnull final URI newBase, int currentNumberOfResourceItemsTheLasIsZero) throws URISyntaxException { final String newURIPath = newBase.getPath(); final String[] splittedNewPath = newURIPath.split("\\/"); final String[] splittedOldPath = this.uri.getPath().split("\\/"); final List<String> resultPath = new ArrayList<String>(); resultPath.addAll(Arrays.asList(splittedNewPath)); currentNumberOfResourceItemsTheLasIsZero = currentNumberOfResourceItemsTheLasIsZero + 1; int oldPathIndex = splittedOldPath.length - currentNumberOfResourceItemsTheLasIsZero; while (oldPathIndex < splittedOldPath.length) { if (oldPathIndex >= 0) { resultPath.add(splittedOldPath[oldPathIndex]); } oldPathIndex++; } final StringBuilder buffer = new StringBuilder(); for (int i = 0; i < resultPath.size(); i++) { if (i > 0) { buffer.append('/'); } buffer.append(resultPath.get(i)); } final URI newURI = new URI(replaceHost ? newBase.getScheme() : this.uri.getScheme(), replaceHost ? newBase.getUserInfo() : this.uri.getUserInfo(), replaceHost ? extractHost(newBase) : extractHost(this.uri), replaceHost ? newBase.getPort() : this.uri.getPort(), buffer.toString(), this.uri.getQuery(), this.uri.getFragment() ); return new MMapURI(newURI, this.fileUriFlag, this.parameters); } MMapURI(@Nonnull final String uri);  MMapURI(@Nonnull final URI uri); private  MMapURI(@Nonnull final URI uri, final boolean isFile, @Nullable final Properties properties);  MMapURI(@Nullable final File nullableBase, @Nonnull final File file, @Nullable final Properties nullableParameters); @SuppressWarnings("ConstantConditions") @Nonnull static MMapURI makeFromFilePath(@Nullable final File base, @Nonnull final String filePath, @Nullable final Properties properties); @Override int hashCode(); @Override boolean equals(@Nullable final Object that); @Nonnull MMapURI replaceBaseInPath(final boolean replaceHost, @Nonnull final URI newBase, int currentNumberOfResourceItemsTheLasIsZero); @Nonnull MMapURI replaceName(@Nonnull final String newName); @Nonnull URI asURI(); @Nonnull String getExtension(); @Nonnull String asString(final boolean ascII, final boolean addPropertiesAsQuery); @Nonnull File asFile(@Nullable final File base); @Nonnull Properties getParameters(); boolean isAbsolute(); @Override @Nonnull String toString(); static final long serialVersionUID; }
@Test public void testReplaceBaseInPath() throws Exception { assertEquals("hello/world/test", new MMapURI("test").replaceBaseInPath(false, new URI("hello/world"), 0).asURI().toString()); assertEquals("hello/world/some/test", new MMapURI("some/test").replaceBaseInPath(false, new URI("hello/world"), 1).asURI().toString()); assertEquals("http: assertEquals("file: assertEquals("/newfolder/newfolder1/hello/universe/and/world", new MMapURI("server/folder/hello/universe/and/world").replaceBaseInPath(false, new URI("file: }