Source:
<code>public void validate(UsernamePasswordCredentials credentials) { CommonHelper.assertNotNull("mongoClient", this.mongoClient); CommonHelper.assertNotNull("usernameAttribute", this.usernameAttribute); CommonHelper.assertNotNull("passwordAttribute", this.passwordAttribute); CommonHelper.assertNotNull("usersDatabase", this.usersDatabase); CommonHelper.assertNotNull("usersCollection", this.usersCollection); CommonHelper.assertNotNull("attributes", this.attributes); CommonHelper.assertNotNull("passwordEncoder", this.passwordEncoder); final String username = credentials.getUsername(); final MongoDatabase db = mongoClient.getDatabase(usersDatabase); final MongoCollection<Document> collection = db.getCollection(usersCollection); final MongoCursor<Document> cursor = collection.find(eq(usernameAttribute, username)).iterator(); final List<Document> users = new ArrayList<>(); try { int i= 0; while (cursor.hasNext() && i <= 2) { users.add(cursor.next()); i++; } } finally { <START> cursor.close(); <END> } if (users.size() == 0) { throw new AccountNotFoundException("No account found for: " + username); } else if (users.size() > 1) { throw new MultipleAccountsFoundException("Too many accounts found for: " + username); } else { final Map<String, Object> user = users.get(0); final String expectedPassword = passwordEncoder.encode(credentials.getPassword()); final String returnedPassword = (String) user.get(passwordAttribute); if (CommonHelper.areNotEquals(returnedPassword, expectedPassword)) { throw new BadCredentialsException("Bad credentials for: " + username); } else { final MongoProfile profile = createProfile(username, attributes.split(","), user); credentials.setUserProfile(profile); } } }</code><technical_language>this written try-resource syntax</technical_language>

Target:
public void validate(UsernamePasswordCredentials credentials) { CommonHelper.assertNotNull("mongoClient", this.mongoClient); CommonHelper.assertNotNull("usernameAttribute", this.usernameAttribute); CommonHelper.assertNotNull("passwordAttribute", this.passwordAttribute); CommonHelper.assertNotNull("usersDatabase", this.usersDatabase); CommonHelper.assertNotNull("usersCollection", this.usersCollection); CommonHelper.assertNotNull("attributes", this.attributes); CommonHelper.assertNotNull("passwordEncoder", this.passwordEncoder); final String username = credentials.getUsername(); final MongoDatabase db = mongoClient.getDatabase(usersDatabase); final MongoCollection<Document> collection = db.getCollection(usersCollection); final List<Document> users = new ArrayList<>(); try (final MongoCursor<Document> cursor = collection.find(eq(usernameAttribute, username)).iterator()) { int i= 0; while (cursor.hasNext() && i <= 2) { users.add(cursor.next()); i++; } } if (users.size() == 0) { throw new AccountNotFoundException("No account found for: " + username); } else if (users.size() > 1) { throw new MultipleAccountsFoundException("Too many accounts found for: " + username); } else { final Map<String, Object> user = users.get(0); final String expectedPassword = passwordEncoder.encode(credentials.getPassword()); final String returnedPassword = (String) user.get(passwordAttribute); if (CommonHelper.areNotEquals(returnedPassword, expectedPassword)) { throw new BadCredentialsException("Bad credentials for: " + username); } else { final MongoProfile profile = createProfile(username, attributes.split(","), user); credentials.setUserProfile(profile); } } }