Externalize to a function I have the following code block which I'd like to refactor into a method, I just don't see a good way to get the following code into a method which doesn't have multiple responsibilities.  Here is the code block:

int maxResults = pageable.getMaxResults(userPrefs);
int firstResult;

if (pageData.isLastPage()) {
    int count = getCountFromDb(); //'Heavy' cost associated with this, so we only 
                                  //determine the count if they are on the last page...
    firstResult = getFirstResult(count, maxResults);
    if (count &gt; 0) {
        boolean isFinalPageFull = (count % maxResults) == 0;
        int pageNum = count / maxResults;
        pageData.setPageNumber((isFinalPage) ? pageNum - 1 : pageNum;
    }
} else {
    firstResult = maxResults * pageNumber;
}


I'd like to move this out into a method or two, but I need to  only do this count one time.  This limitation is clouding my vision, as I can't think of any function which follows the Single Responsibility Principle.  Anything I can do to achieve my goal?
You could make count a field, then have a function to determineFirstResult().  Then the setPageNumber block can be separated out.

if (pageData.isLastPage()) {
    count = getCountFromDb();
    if (count &gt; 0) {
        boolean isFinalPageFull = (count % maxResults) == 0;
        int pageNum = count / maxResults;
        pageData.setPageNumber((isFinalPage) ? pageNum - 1 : pageNum;
    }
}
firstResult = determineFirstResult();


...

int determineFirstResult(int maxResults) {
    if (pageData.isLastPage()) return getFirstResult(count, maxResults);
    return maxResults * pageNumber;
}


But now you've got a dependency; count has to be determined before calling determineFirstResult() as written.  So maybe make count a lazy-loaded field; if it's requested and null, then (and only then) do you getCountFromDb.