@Test public void testNameClash() { DownloadRootPathsNormalizer n = new DownloadRootPathsNormalizer(); final List<TransferItem> list = new ArrayList<TransferItem>(); final Path fa = new Path("/f/a", EnumSet.of(Path.Type.file)); list.add(new TransferItem(fa, new NullLocal(PreferencesFactory.get().getProperty("tmp.dir"), fa.getName()) { @Override public boolean exists() { return false; } })); { final Path ga = new Path("/g/a", EnumSet.of(Path.Type.file)); list.add(new TransferItem(ga, new NullLocal(PreferencesFactory.get().getProperty("tmp.dir"), ga.getName()) { @Override public boolean exists() { return false; } })); final List<TransferItem> normalized = n.normalize(list); assertEquals(2, normalized.size()); final Iterator<TransferItem> iterator = normalized.iterator(); assertEquals(new NullLocal(PreferencesFactory.get().getProperty("tmp.dir"), "a"), iterator.next().local); assertEquals(new NullLocal(PreferencesFactory.get().getProperty("tmp.dir"), "a-1"), iterator.next().local); } }
@Override public List<TransferItem> normalize(final List<TransferItem> roots) { final List<TransferItem> normalized = new ArrayList<TransferItem>(); for(final TransferItem download : roots) { boolean duplicate = false; for(Iterator<TransferItem> iter = normalized.iterator(); iter.hasNext(); ) { TransferItem n = iter.next(); if(download.remote.isChild(n.remote)) { duplicate = true; break; } if(n.remote.isChild(download.remote)) { iter.remove(); } if(download.local.equals(n.local)) { final String parent = download.local.getParent().getAbsolute(); final String filename = download.remote.getName(); String proposal; int no = 0; Local local; do { proposal = String.format("%s-%d", FilenameUtils.getBaseName(filename), ++no); if(StringUtils.isNotBlank(Path.getExtension(filename))) { proposal += String.format(".%s", Path.getExtension(filename)); } local = LocalFactory.get(parent, proposal); } while(local.exists()); if(log.isInfoEnabled()) { log.info(String.format("Changed local name from %s to %s", filename, local.getName())); } download.local = local; } } if(!duplicate) { normalized.add(new TransferItem(download.remote, download.local, download.lockId, download.checksum)); } } return normalized; }
DownloadRootPathsNormalizer implements RootPathsNormalizer<List<TransferItem>> { @Override public List<TransferItem> normalize(final List<TransferItem> roots) { final List<TransferItem> normalized = new ArrayList<TransferItem>(); for(final TransferItem download : roots) { boolean duplicate = false; for(Iterator<TransferItem> iter = normalized.iterator(); iter.hasNext(); ) { TransferItem n = iter.next(); if(download.remote.isChild(n.remote)) { duplicate = true; break; } if(n.remote.isChild(download.remote)) { iter.remove(); } if(download.local.equals(n.local)) { final String parent = download.local.getParent().getAbsolute(); final String filename = download.remote.getName(); String proposal; int no = 0; Local local; do { proposal = String.format("%s-%d", FilenameUtils.getBaseName(filename), ++no); if(StringUtils.isNotBlank(Path.getExtension(filename))) { proposal += String.format(".%s", Path.getExtension(filename)); } local = LocalFactory.get(parent, proposal); } while(local.exists()); if(log.isInfoEnabled()) { log.info(String.format("Changed local name from %s to %s", filename, local.getName())); } download.local = local; } } if(!duplicate) { normalized.add(new TransferItem(download.remote, download.local, download.lockId, download.checksum)); } } return normalized; } }
DownloadRootPathsNormalizer implements RootPathsNormalizer<List<TransferItem>> { @Override public List<TransferItem> normalize(final List<TransferItem> roots) { final List<TransferItem> normalized = new ArrayList<TransferItem>(); for(final TransferItem download : roots) { boolean duplicate = false; for(Iterator<TransferItem> iter = normalized.iterator(); iter.hasNext(); ) { TransferItem n = iter.next(); if(download.remote.isChild(n.remote)) { duplicate = true; break; } if(n.remote.isChild(download.remote)) { iter.remove(); } if(download.local.equals(n.local)) { final String parent = download.local.getParent().getAbsolute(); final String filename = download.remote.getName(); String proposal; int no = 0; Local local; do { proposal = String.format("%s-%d", FilenameUtils.getBaseName(filename), ++no); if(StringUtils.isNotBlank(Path.getExtension(filename))) { proposal += String.format(".%s", Path.getExtension(filename)); } local = LocalFactory.get(parent, proposal); } while(local.exists()); if(log.isInfoEnabled()) { log.info(String.format("Changed local name from %s to %s", filename, local.getName())); } download.local = local; } } if(!duplicate) { normalized.add(new TransferItem(download.remote, download.local, download.lockId, download.checksum)); } } return normalized; }  }
DownloadRootPathsNormalizer implements RootPathsNormalizer<List<TransferItem>> { @Override public List<TransferItem> normalize(final List<TransferItem> roots) { final List<TransferItem> normalized = new ArrayList<TransferItem>(); for(final TransferItem download : roots) { boolean duplicate = false; for(Iterator<TransferItem> iter = normalized.iterator(); iter.hasNext(); ) { TransferItem n = iter.next(); if(download.remote.isChild(n.remote)) { duplicate = true; break; } if(n.remote.isChild(download.remote)) { iter.remove(); } if(download.local.equals(n.local)) { final String parent = download.local.getParent().getAbsolute(); final String filename = download.remote.getName(); String proposal; int no = 0; Local local; do { proposal = String.format("%s-%d", FilenameUtils.getBaseName(filename), ++no); if(StringUtils.isNotBlank(Path.getExtension(filename))) { proposal += String.format(".%s", Path.getExtension(filename)); } local = LocalFactory.get(parent, proposal); } while(local.exists()); if(log.isInfoEnabled()) { log.info(String.format("Changed local name from %s to %s", filename, local.getName())); } download.local = local; } } if(!duplicate) { normalized.add(new TransferItem(download.remote, download.local, download.lockId, download.checksum)); } } return normalized; }  @Override List<TransferItem> normalize(final List<TransferItem> roots); }
DownloadRootPathsNormalizer implements RootPathsNormalizer<List<TransferItem>> { @Override public List<TransferItem> normalize(final List<TransferItem> roots) { final List<TransferItem> normalized = new ArrayList<TransferItem>(); for(final TransferItem download : roots) { boolean duplicate = false; for(Iterator<TransferItem> iter = normalized.iterator(); iter.hasNext(); ) { TransferItem n = iter.next(); if(download.remote.isChild(n.remote)) { duplicate = true; break; } if(n.remote.isChild(download.remote)) { iter.remove(); } if(download.local.equals(n.local)) { final String parent = download.local.getParent().getAbsolute(); final String filename = download.remote.getName(); String proposal; int no = 0; Local local; do { proposal = String.format("%s-%d", FilenameUtils.getBaseName(filename), ++no); if(StringUtils.isNotBlank(Path.getExtension(filename))) { proposal += String.format(".%s", Path.getExtension(filename)); } local = LocalFactory.get(parent, proposal); } while(local.exists()); if(log.isInfoEnabled()) { log.info(String.format("Changed local name from %s to %s", filename, local.getName())); } download.local = local; } } if(!duplicate) { normalized.add(new TransferItem(download.remote, download.local, download.lockId, download.checksum)); } } return normalized; }  @Override List<TransferItem> normalize(final List<TransferItem> roots);  }