in java how do i design an api to be both generic, simple, readable

api, java, oop

Solution

how about something like a fluid api for queries:

List<Customer> matches = find(new CustomerQuery().withName("john(.*)").withId(42));

Problem

My example is this I want my user to call ``` findCustomers(...) ``` now my question is about the argument to this method. I have a ``` Customer.java ``` object and I would like the user to be able to use the library to search by customer name, and customer id. now i would not want to create multiple methods ``` findCustomerById(String id) findCustomerByName(String name) findAllCustomers() ``` Instead what I thought of doing is this a generic findCustomer ``` /** if you pass only custoer.name and rest of fields are null will search by name, if you pass a null custoer object will return all customers, if you pass both custoer id and his name will search by both fields). **/ findCustomer(Customer customer) ``` Now I have a generic single method for api but i don't like that i pass nulls in an object, i don't like nulls. anyone has a clear cut best practice for such an api? thanks

Original source