replace occurences of vector with arraylist

Hi there!

I’m sifting through sonar qube to create JIRA issues from the rules that are broken in the code.

I see that there are lots of uses of synchronized classes and wanted to talk about how to best clean that up.

Am I right to say that whenever we use one of the synchronized classes like Vector for local variables we can safely replace them with the non-synchronized such as ArrayList since those local variables are not shared state between threads there is no need to synchronize every operation on this container.

Such a use can be seen here

According to a github search for vector there are 45 occurences of vector. Should I create one JIRA issue with subtasks or simply one JIRA issue with separate pull requests one for each class thats affected?

1 Like

I would go for one JIRA issue to have one pull request dealing with this replacement.

Yes unless there is another part of the code where it expects a Vector. I tried this at work and had to revert because the in-house framework internals expect Vectors. Sad.

I’d say a single issue.

thanks! yes I see that Vector's are often just used as an temp variable, internally in methods but not really part of any public API that is used. the public APIs of OpenMRS at least where I checked expect List, so that should work then :slight_smile:

If you have the time you can also get rid of this temp collections:

Example:

public Collection<ConceptName> getNames(Locale locale) {
		Collection<ConceptName> localeNames = new Vector<ConceptName>();
		for (ConceptName possibleName : getNames()) {
			if (possibleName.getLocale().equals(locale)) {
				localeNames.add(possibleName);
			}
		}
		return localeNames;
	}

Can be simplified to:

	public Collection<ConceptName> getNames(Locale locale) {
          return getNames().stream().filter( p -> p.getLocale().equals(locale));
    }
1 Like

awesome :slight_smile: loving the streams :heart_eyes: will do! if you have more ideas or things you remember would be great to clean up, improve, please feel free to create an issue and label it with ‘hackathon-germany’

Make all the Hibernate*DAO extend HibernateOpenmrsObjectDAO Remove all the sessionFactory.getCurrentSession() prefixes. Invoke get/saveOrUpdate/… of the parent. Remove the sessionFactory instance variable because it won’t be used anymore.

1 Like

http://files.zeroturnaround.com/pdf/zt_java8_streams_cheat_sheet.pdf