From 9c978b7adcb808447006392b741dd3114cb7385a Mon Sep 17 00:00:00 2001 From: Josh Cummings <3627351+jzheaux@users.noreply.github.com> Date: Tue, 7 Jan 2025 13:31:17 -0700 Subject: [PATCH] Make NameAwareAttributes Iterable Closes gh-937 --- .../ldap/core/DirContextAdapter.java | 97 ++++++------------- .../ldap/core/NameAwareAttributes.java | 17 +++- .../ldap/core/NameAwareAttributesTests.java | 16 ++- 3 files changed, 58 insertions(+), 72 deletions(-) diff --git a/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java b/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java index 4c26eb86..a5256a68 100644 --- a/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java +++ b/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2013 the original author or authors. + * Copyright 2005-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.ldap.core; import java.util.ArrayList; import java.util.Hashtable; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.SortedSet; @@ -238,25 +239,15 @@ public class DirContextAdapter implements DirContextOperations { List tmpList = new ArrayList(); - NamingEnumeration attributesEnumeration; if (isUpdateMode()) { - attributesEnumeration = this.updatedAttrs.getAll(); - } - else { - attributesEnumeration = this.originalAttrs.getAll(); - } - - try { - while (attributesEnumeration.hasMore()) { - Attribute oneAttribute = attributesEnumeration.next(); - tmpList.add(oneAttribute.getID()); + for (NameAwareAttribute attribute : this.updatedAttrs) { + tmpList.add(attribute.getID()); } } - catch (NamingException ex) { - throw LdapUtils.convertLdapException(ex); - } - finally { - closeNamingEnumeration(attributesEnumeration); + else { + for (NameAwareAttribute attribute : this.originalAttrs) { + tmpList.add(attribute.getID()); + } } return tmpList.toArray(new String[tmpList.size()]); @@ -283,22 +274,8 @@ public class DirContextAdapter implements DirContextOperations { } List tmpList = new LinkedList(); - NamingEnumeration attributesEnumeration = null; - try { - attributesEnumeration = this.updatedAttrs.getAll(); - - // find attributes that have been changed, removed or added - while (attributesEnumeration.hasMore()) { - NameAwareAttribute oneAttr = (NameAwareAttribute) attributesEnumeration.next(); - - collectModifications(oneAttr, tmpList); - } - } - catch (NamingException ex) { - throw LdapUtils.convertLdapException(ex); - } - finally { - closeNamingEnumeration(attributesEnumeration); + for (NameAwareAttribute attribute : this.updatedAttrs) { + collectModifications(attribute, tmpList); } if (log.isDebugEnabled()) { @@ -318,10 +295,8 @@ public class DirContextAdapter implements DirContextOperations { * (removals and additions) will be collected individually. * @param changedAttr the value of the changed attribute. * @param modificationList the list in which to add the modifications. - * @throws NamingException if thrown by called Attribute methods. */ - private void collectModifications(NameAwareAttribute changedAttr, List modificationList) - throws NamingException { + private void collectModifications(NameAwareAttribute changedAttr, List modificationList) { NameAwareAttribute currentAttribute = this.originalAttrs.get(changedAttr.getID()); if (currentAttribute != null && changedAttr.hasValuesAsNames()) { try { @@ -372,17 +347,15 @@ public class DirContextAdapter implements DirContextOperations { } } - private void collectModifications(Attribute originalAttr, Attribute changedAttr, - List modificationList) throws NamingException { + private void collectModifications(NameAwareAttribute originalAttr, NameAwareAttribute changedAttr, + List modificationList) { Attribute originalClone = (Attribute) originalAttr.clone(); Attribute addedValuesAttribute = new NameAwareAttribute(originalAttr.getID()); - NamingEnumeration allValues = changedAttr.getAll(); - while (allValues.hasMoreElements()) { - Object attributeValue = allValues.nextElement(); - if (!originalClone.remove(attributeValue)) { - addedValuesAttribute.add(attributeValue); + for (Object value : changedAttr) { + if (!originalClone.remove(value)) { + addedValuesAttribute.add(value); } } @@ -696,30 +669,15 @@ public class DirContextAdapter implements DirContextOperations { */ @Override public void update() { - NamingEnumeration attributesEnumeration = null; - - try { - attributesEnumeration = this.updatedAttrs.getAll(); - - // find what to update - while (attributesEnumeration.hasMore()) { - Attribute a = attributesEnumeration.next(); - - // if it does not exist it should be added - if (isEmptyAttribute(a)) { - this.originalAttrs.remove(a.getID()); - } - else { - // Otherwise it should be set. - this.originalAttrs.put(a); - } + for (NameAwareAttribute attribute : this.updatedAttrs) { + // if it does not exist it should be added + if (isEmptyAttribute(attribute)) { + this.originalAttrs.remove(attribute.getID()); + } + else { + // Otherwise it should be set. + this.originalAttrs.put(attribute); } - } - catch (NamingException ex) { - throw LdapUtils.convertLdapException(ex); - } - finally { - closeNamingEnumeration(attributesEnumeration); } // Reset the attributes to be updated @@ -1359,8 +1317,9 @@ public class DirContextAdapter implements DirContextOperations { builder.append(" {"); try { - for (NamingEnumeration i = this.originalAttrs.getAll(); i.hasMore();) { - Attribute attribute = i.next(); + Iterator attributes = this.originalAttrs.iterator(); + while (attributes.hasNext()) { + NameAwareAttribute attribute = attributes.next(); if (attribute.size() == 1) { builder.append(attribute.getID()); builder.append('='); @@ -1374,7 +1333,7 @@ public class DirContextAdapter implements DirContextOperations { } } - if (i.hasMore()) { + if (attributes.hasNext()) { builder.append(", "); } } diff --git a/core/src/main/java/org/springframework/ldap/core/NameAwareAttributes.java b/core/src/main/java/org/springframework/ldap/core/NameAwareAttributes.java index dd35bd8d..dc44a375 100644 --- a/core/src/main/java/org/springframework/ldap/core/NameAwareAttributes.java +++ b/core/src/main/java/org/springframework/ldap/core/NameAwareAttributes.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2013 the original author or authors. + * Copyright 2005-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.ldap.core; import java.util.HashMap; +import java.util.Iterator; import java.util.Locale; import java.util.Map; @@ -24,6 +25,7 @@ import javax.naming.NamingEnumeration; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; +import org.springframework.lang.NonNull; import org.springframework.util.Assert; /** @@ -32,7 +34,7 @@ import org.springframework.util.Assert; * @author Mattias Hellborg Arthursson * @since 2.0 */ -public final class NameAwareAttributes implements Attributes { +public final class NameAwareAttributes implements Attributes, Iterable { private Map attributes = new HashMap(); @@ -81,6 +83,17 @@ public final class NameAwareAttributes implements Attributes { return new IterableNamingEnumeration(this.attributes.keySet()); } + /** + * @inheritDoc + * + * @since 3.3 + */ + @NonNull + @Override + public Iterator iterator() { + return this.attributes.values().iterator(); + } + @Override public Attribute put(String attrID, Object val) { Assert.hasLength(attrID, "Attribute ID must not be empty"); diff --git a/core/src/test/java/org/springframework/ldap/core/NameAwareAttributesTests.java b/core/src/test/java/org/springframework/ldap/core/NameAwareAttributesTests.java index 2645eb71..a682cc1d 100644 --- a/core/src/test/java/org/springframework/ldap/core/NameAwareAttributesTests.java +++ b/core/src/test/java/org/springframework/ldap/core/NameAwareAttributesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2023 the original author or authors. + * Copyright 2005-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,9 @@ package org.springframework.ldap.core; +import java.util.List; +import java.util.stream.StreamSupport; + import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -40,4 +43,15 @@ public class NameAwareAttributesTests { assertThat(attributes.size()).isEqualTo(0); } + @Test + public void iteratorWhenAttributesThenIterates() { + NameAwareAttributes attributes = new NameAwareAttributes(); + attributes.put("myID", "value"); + attributes.put("myOtherID", "othervalue"); + List ids = StreamSupport.stream(attributes.spliterator(), false) + .map(NameAwareAttribute::getID) + .toList(); + assertThat(ids).containsOnly("myID", "myOtherID"); + } + }