First of all, let's modify the IDataSource
interface, adding to it a method for returning a selected range of celebrities:
public interface IDataSource
{
List<Celebrity> getAllCelebrities();
Celebrity getCelebrityById(long id);
void addCelebrity(Celebrity c);
List<Celebrity> getRange(int indexFrom, int indexTo);
}
Next, we need to implement this method in the available implementation of this interface. Add the following method to the MockDataSource
class:
public List<Celebrity> getRange(int indexFrom, int indexTo) { List<Celebrity> result = new ArrayList<Celebrity>(); for (int i = indexFrom; i <= indexTo; i++) { result.add(celebrities.get(i)); } return result; }
The code is quite simple, we are returning a subset of the existing collection starting from one index value and ending with the other. In a real-life implementation, we would probably check whether indexTo
is bigger than indexFrom
, but here, let's keep things simple.
Here is one...