dmolony-DiskBrowser/src/com/bytezone/diskbrowser/duplicates/DuplicateHandler.java

133 lines
3.4 KiB
Java
Raw Normal View History

2016-12-07 10:42:01 +00:00
package com.bytezone.diskbrowser.duplicates;
import java.io.File;
2016-12-09 11:31:03 +00:00
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
2016-12-07 10:42:01 +00:00
import com.bytezone.diskbrowser.utilities.Utility;
public class DuplicateHandler
{
2016-12-10 07:36:44 +00:00
// private static final FileComparator fileComparator = new FileComparator ();
// private static final int MAX_NAME_WIDTH = 34;
// private static final String FORMAT = "%-" + MAX_NAME_WIDTH + "s %,10d%n";
2016-12-07 10:42:01 +00:00
private final File rootFolder;
private int totalDisks;
private int totalFolders;
2016-12-09 11:31:03 +00:00
private final int rootFolderNameLength;
2016-12-10 07:36:44 +00:00
// private final boolean debug = false;
2016-12-07 10:42:01 +00:00
// total files for each suffix
private final Map<String, Integer> typeList = new TreeMap<String, Integer> ();
2016-12-09 11:31:03 +00:00
// list of checksum -> DiskDetails
final Map<Long, DiskDetails> checksumMap = new HashMap<Long, DiskDetails> ();
2016-12-07 10:42:01 +00:00
// list of unique disk names -> File
2016-12-09 11:31:03 +00:00
private final Map<String, DiskDetails> fileNameMap =
new TreeMap<String, DiskDetails> ();
2016-12-07 10:42:01 +00:00
public DuplicateHandler (File rootFolder)
{
this.rootFolder = rootFolder;
2016-12-09 11:31:03 +00:00
rootFolderNameLength = rootFolder.getAbsolutePath ().length ();
}
2016-12-08 01:19:18 +00:00
2016-12-09 11:31:03 +00:00
public Map<String, DiskDetails> getFileNameMap ()
{
return fileNameMap;
2016-12-07 10:42:01 +00:00
}
2016-12-09 11:31:03 +00:00
public Map<Long, DiskDetails> getChecksumMap ()
2016-12-08 01:19:18 +00:00
{
2016-12-09 11:31:03 +00:00
return checksumMap;
2016-12-08 01:19:18 +00:00
}
2016-12-07 10:42:01 +00:00
void countDisks ()
{
2016-12-08 01:19:18 +00:00
traverse (rootFolder);
2016-12-07 10:42:01 +00:00
2016-12-08 01:19:18 +00:00
System.out.printf ("%nFolders ..... %,7d%n", totalFolders);
System.out.printf ("Disks ....... %,7d%n%n", totalDisks);
2016-12-07 10:42:01 +00:00
int grandTotal = 0;
for (String key : typeList.keySet ())
{
int typeTotal = typeList.get (key);
grandTotal += typeTotal;
2016-12-08 01:19:18 +00:00
System.out.printf ("%13.13s %,7d%n", key + " ...........", typeTotal);
2016-12-07 10:42:01 +00:00
}
2016-12-08 01:19:18 +00:00
System.out.printf ("%nTotal ....... %,7d%n%n", grandTotal);
2016-12-07 10:42:01 +00:00
}
2016-12-09 11:31:03 +00:00
File getRootFolder ()
{
return rootFolder;
}
2016-12-08 01:19:18 +00:00
private void traverse (File directory)
2016-12-07 10:42:01 +00:00
{
File[] files = directory.listFiles ();
2016-12-09 11:31:03 +00:00
2016-12-07 10:42:01 +00:00
if (files == null || files.length == 0)
{
System.out.println ("Empty folder : " + directory.getAbsolutePath ());
return;
}
2016-12-09 11:31:03 +00:00
// Arrays.sort (files, fileComparator);
2016-12-07 10:42:01 +00:00
for (File file : files)
{
2016-12-09 11:31:03 +00:00
String fileName = file.getName ().toLowerCase ();
2016-12-07 10:42:01 +00:00
if (file.isDirectory ())
{
++totalFolders;
2016-12-08 01:19:18 +00:00
traverse (file);
2016-12-07 10:42:01 +00:00
}
2016-12-09 11:31:03 +00:00
else if (Utility.validFileType (fileName))
2016-12-07 10:42:01 +00:00
{
++totalDisks;
2016-12-09 11:31:03 +00:00
incrementType (file, fileName);
checkDuplicates (file, fileName);
2016-12-07 10:42:01 +00:00
}
}
}
2016-12-09 11:31:03 +00:00
private void checkDuplicates (File file, String fileName)
2016-12-07 10:42:01 +00:00
{
2016-12-09 11:31:03 +00:00
String rootName = file.getAbsolutePath ().substring (rootFolderNameLength);
DiskDetails diskDetails = new DiskDetails (file, rootName, fileName);
2016-12-07 10:42:01 +00:00
2016-12-09 11:31:03 +00:00
if (fileNameMap.containsKey (fileName))
2016-12-10 07:36:44 +00:00
fileNameMap.get (fileName).addDuplicateName (diskDetails);
2016-12-09 11:31:03 +00:00
else
fileNameMap.put (fileName, diskDetails);
2016-12-07 10:42:01 +00:00
2016-12-10 07:36:44 +00:00
long checksum = diskDetails.getChecksum ();
if (checksumMap.containsKey (checksum))
checksumMap.get (checksum).addDuplicateChecksum (diskDetails);
2016-12-09 11:31:03 +00:00
else
2016-12-10 07:36:44 +00:00
checksumMap.put (checksum, diskDetails);
2016-12-07 10:42:01 +00:00
}
2016-12-09 11:31:03 +00:00
private void incrementType (File file, String fileName)
2016-12-07 10:42:01 +00:00
{
2016-12-09 11:31:03 +00:00
int pos = file.getName ().lastIndexOf ('.');
if (pos > 0)
2016-12-07 10:42:01 +00:00
{
2016-12-09 11:31:03 +00:00
String type = fileName.substring (pos + 1);
if (typeList.containsKey (type))
2016-12-07 10:42:01 +00:00
{
2016-12-09 11:31:03 +00:00
int t = typeList.get (type);
typeList.put (type, ++t);
2016-12-07 10:42:01 +00:00
}
2016-12-09 11:31:03 +00:00
else
typeList.put (type, 1);
2016-12-07 10:42:01 +00:00
}
}
}