2

I am trying to fill a list myClassTypes with recursive calls to getMyClassTypes(). But it gets initialized every time there is recursive call to the method. And, I am losing few values because of this. I understand that the problem is with declaring locally: List myClassTypes = new ArrayList<>(); But, I don't want to declare the list at the class level. Here is the code I am working with:

private List<MyClassType> getMyClassTypes(final String path) throws SVNException {

    final Collection<SVNDirEntry> svnDirectoryEntries = svnRepository.getDir(path, -1, null, (Collection<SVNDirEntry>) null);
    final Iterator<SVNDirEntry> svnDirectoryEntryIterator = svnDirectoryEntries.iterator();
    final List<MyClassType> myClassTypes = new ArrayList<>();

    while (svnDirectoryEntryIterator.hasNext()) {
        final SVNDirEntry entry = svnDirectoryEntryIterator.next();
        final String fileName = entry.getName();

        if (!path.isEmpty() && path.matches(SUB_DIRECTORY_NAME_PATTERN) && fileName.endsWith(".xml")) {                
            final MyClassType myClassType = new MyClassType(path, fileName);
            myClassTypes.add(myClassType);
        }

        /*
         * Check if the entry is a directory recursively.
         */
        if (someCondition) {
            getMyClassTypes((path.equals("")) ? entry.getName() : path + "/" + entry.getName());
        }
    }
    return myClassTypes;
}

How to manage the 'myClassTypes' to contain all the values even after 'n times' recursive calls?

1
  • you mean you will call getMyClassTypes n times? Commented Apr 26, 2018 at 11:10

2 Answers 2

5

Pass a List<MyClassType> instance as argument to the method instead of creating it inside the method.

This way to create just one ArrayList instance, and you can even change the return type to void.

private void getMyClassTypes(final String path, List<MyClassType> myClassTypes) throws SVNException 
{
    final Collection<SVNDirEntry> svnDirectoryEntries = svnRepository.getDir(path, -1, null, (Collection<SVNDirEntry>) null);
    final Iterator<SVNDirEntry> svnDirectoryEntryIterator = svnDirectoryEntries.iterator();

    while (svnDirectoryEntryIterator.hasNext()) {
        final SVNDirEntry entry = svnDirectoryEntryIterator.next();
        final String fileName = entry.getName();

        if (!path.isEmpty() && path.matches(SUB_DIRECTORY_NAME_PATTERN) && fileName.endsWith(".xml")) {                
            final MyClassType myClassType = new MyClassType(path, fileName);
            myClassTypes.add(myClassType);
        }

        /*
         * Check if the entry is a directory recursively.
         */
        if (someCondition) {
            getMyClassTypes((path.equals("")) ? entry.getName() : path + "/" + entry.getName(), myClassTypes);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Add the return value of the recursive call to the current myClassTypes and return it.

if (someCondition) {
    myClassTypes.addAll(getMyClassTypes((path.equals("")) ? entry.getName() : path + "/" + entry.getName()));
}

4 Comments

This has the downside of creating multiple ArrayList instances when you only need one of them.
@Eran True. If the recursion depth is too long and it proves to affect performance, then we can pass it as a parameter as you suggest.
@Eran I felt this is a bit more cleaner (not mutate the input parameter)
@Eran: Thanks for the idea. Worked fine. That's a pretty solution. (Sorry that I am not allowed to add +1 to answer as my level is too low on stackoverflow)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.