001package org.fcrepo.migration.pidlist; 002 003import org.slf4j.Logger; 004 005import java.io.BufferedReader; 006import java.io.File; 007import java.io.FileReader; 008import java.io.IOException; 009import java.util.HashSet; 010import java.util.Set; 011 012import static org.slf4j.LoggerFactory.getLogger; 013 014/** 015 * This class "accepts" and PIDs that are included in the user-provided list 016 * 017 * @author awoods 018 * @since 2019-11-08 019 */ 020public class UserProvidedPidListManager implements PidListManager { 021 022 private static final Logger LOGGER = getLogger(UserProvidedPidListManager.class); 023 024 private Set<String> pidList = new HashSet<>(); 025 private Set<String> processedPids = new HashSet<>(); 026 027 /** 028 * Constructor 029 * 030 * @param pidListFile provided by user 031 */ 032 public UserProvidedPidListManager(final File pidListFile) { 033 034 // If arg is null, we will accept all PIDs 035 if (pidListFile != null) { 036 if (!pidListFile.exists() || !pidListFile.canRead()) { 037 throw new IllegalArgumentException("File either does not exist or is inaccessible :" + 038 pidListFile.getAbsolutePath()); 039 } 040 041 try (BufferedReader reader = new BufferedReader(new FileReader(pidListFile));) { 042 reader.lines().forEach(l -> pidList.add(l)); 043 } catch (IOException e) { 044 // Should not happen based on previous check 045 throw new RuntimeException(e); 046 } 047 } 048 } 049 050 051 @Override 052 public boolean accept(final String pid) { 053 final boolean doAccept = pidList.isEmpty() || pidList.contains(pid); 054 LOGGER.debug("PID: {}, accept? {}", pid, doAccept); 055 if (doAccept) { 056 processedPids.add(pid); 057 } 058 return doAccept; 059 } 060 061 public boolean finishedProcessingAllPids() { 062 if (!pidList.isEmpty()) { 063 if (pidList.equals(processedPids)) { 064 return true; 065 } 066 } 067 return false; 068 } 069}