import java.io.File;
import java.util.HashMap;
import java.util.Map;
class FileMetadataAttacher {
/**
* Attaches metadata (file path) to a map. Handles edge cases like null or empty paths.
*
* @param filePaths A variable number of file paths to attach.
* @return A map where keys are arbitrary identifiers and values are the file paths.
* Returns an empty map if filePaths is null or empty.
*/
public static Map<String, String> attachFileMetadata(String... filePaths) {
Map<String, String> metadata = new HashMap<>();
if (filePaths == null || filePaths.length == 0) {
return metadata; // Return empty map for null or empty input
}
for (int i = 0; i < filePaths.length; i++) {
String filePath = filePaths[i];
if (filePath == null || filePath.trim().isEmpty()) {
continue; // Skip null or empty paths
}
//Ensure the path is valid
File file = new File(filePath);
if (!file.exists()) {
continue; // Skip if file doesn't exist
}
metadata.put("file_" + i, filePath); // Use a unique identifier
}
return metadata;
}
public static void main(String[] args) {
//Example Usage
Map<String, String> metadata = attachFileMetadata("path/to/file1.txt", "path/to/file2.txt", null, "");
System.out.println(metadata); // Output the metadata map
}
}
Add your comment