0

I have a predefined list of phone numbers to block in my application, the list is being created based on a file input in PhoneListFilter constructor.

I need to be able to add and remove items from this list without restarting the application. I was thinking about implementing FileWatcher which could start a thread and check the file for any changes every 5 minutes or so and update the list. Is it a good approach or should I do it differently?

PhoneListFilter.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PhoneListFilter {

private List<String> phoneList;

public PhoneListFilter() {
    phoneList = new ArrayList<>();
    try {
        Scanner s = new Scanner(new File("PhonesToBlockList"));
        while (s.hasNext()) {
            phoneList.add(s.next());
        }
        s.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }
}

public boolean shouldBlock(PhoneRequest phoneRequest) {
    if (phoneList.contains(phoneRequest.getPhoneNumber())) {
        return true;
    }
    return false;
}
}
UO Man
  • 153
  • 4
  • 13
  • 1
    What kind of application is it? Do you have a database? How often is this method called? How large is the file? Note that, if you want top performance with the code you currently have, you should use a HashSet (O(1)) rather than an ArrayList (O(n)). – JB Nizet May 06 '17 at 08:41
  • Rather than checking every 5 minutes, you can use the built-in `WatchService`. See [Watch for single file changes](http://stackoverflow.com/questions/16251273/can-i-watch-for-single-file-change-with-watchservice-not-the-whole-directory). – kagmole May 06 '17 at 08:44
  • I assume you are working on a mobile application. I would use a relational database engine, like [Sqlite](https://developer.android.com/training/basics/data-storage/databases.html) for handling blocked numbers. –  May 06 '17 at 09:08

0 Answers0