Skip to content

Commit 9c978b7

Browse files
committed
Make NameAwareAttributes Iterable
Closes gh-937
1 parent 848831d commit 9c978b7

File tree

3 files changed

+57
-71
lines changed

3 files changed

+57
-71
lines changed

core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2013 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Hashtable;
21+
import java.util.Iterator;
2122
import java.util.LinkedList;
2223
import java.util.List;
2324
import java.util.SortedSet;
@@ -238,26 +239,16 @@ public String[] getNamesOfModifiedAttributes() {
238239

239240
List<String> tmpList = new ArrayList<String>();
240241

241-
NamingEnumeration<? extends Attribute> attributesEnumeration;
242242
if (isUpdateMode()) {
243-
attributesEnumeration = this.updatedAttrs.getAll();
243+
for (NameAwareAttribute attribute : this.updatedAttrs) {
244+
tmpList.add(attribute.getID());
245+
}
244246
}
245247
else {
246-
attributesEnumeration = this.originalAttrs.getAll();
247-
}
248-
249-
try {
250-
while (attributesEnumeration.hasMore()) {
251-
Attribute oneAttribute = attributesEnumeration.next();
252-
tmpList.add(oneAttribute.getID());
248+
for (NameAwareAttribute attribute : this.originalAttrs) {
249+
tmpList.add(attribute.getID());
253250
}
254251
}
255-
catch (NamingException ex) {
256-
throw LdapUtils.convertLdapException(ex);
257-
}
258-
finally {
259-
closeNamingEnumeration(attributesEnumeration);
260-
}
261252

262253
return tmpList.toArray(new String[tmpList.size()]);
263254
}
@@ -283,22 +274,8 @@ public ModificationItem[] getModificationItems() {
283274
}
284275

285276
List<ModificationItem> tmpList = new LinkedList<ModificationItem>();
286-
NamingEnumeration<? extends Attribute> attributesEnumeration = null;
287-
try {
288-
attributesEnumeration = this.updatedAttrs.getAll();
289-
290-
// find attributes that have been changed, removed or added
291-
while (attributesEnumeration.hasMore()) {
292-
NameAwareAttribute oneAttr = (NameAwareAttribute) attributesEnumeration.next();
293-
294-
collectModifications(oneAttr, tmpList);
295-
}
296-
}
297-
catch (NamingException ex) {
298-
throw LdapUtils.convertLdapException(ex);
299-
}
300-
finally {
301-
closeNamingEnumeration(attributesEnumeration);
277+
for (NameAwareAttribute attribute : this.updatedAttrs) {
278+
collectModifications(attribute, tmpList);
302279
}
303280

304281
if (log.isDebugEnabled()) {
@@ -318,10 +295,8 @@ public ModificationItem[] getModificationItems() {
318295
* (removals and additions) will be collected individually.
319296
* @param changedAttr the value of the changed attribute.
320297
* @param modificationList the list in which to add the modifications.
321-
* @throws NamingException if thrown by called Attribute methods.
322298
*/
323-
private void collectModifications(NameAwareAttribute changedAttr, List<ModificationItem> modificationList)
324-
throws NamingException {
299+
private void collectModifications(NameAwareAttribute changedAttr, List<ModificationItem> modificationList) {
325300
NameAwareAttribute currentAttribute = this.originalAttrs.get(changedAttr.getID());
326301
if (currentAttribute != null && changedAttr.hasValuesAsNames()) {
327302
try {
@@ -372,17 +347,15 @@ else if (changedAttr.size() > 0) {
372347
}
373348
}
374349

375-
private void collectModifications(Attribute originalAttr, Attribute changedAttr,
376-
List<ModificationItem> modificationList) throws NamingException {
350+
private void collectModifications(NameAwareAttribute originalAttr, NameAwareAttribute changedAttr,
351+
List<ModificationItem> modificationList) {
377352

378353
Attribute originalClone = (Attribute) originalAttr.clone();
379354
Attribute addedValuesAttribute = new NameAwareAttribute(originalAttr.getID());
380355

381-
NamingEnumeration<?> allValues = changedAttr.getAll();
382-
while (allValues.hasMoreElements()) {
383-
Object attributeValue = allValues.nextElement();
384-
if (!originalClone.remove(attributeValue)) {
385-
addedValuesAttribute.add(attributeValue);
356+
for (Object value : changedAttr) {
357+
if (!originalClone.remove(value)) {
358+
addedValuesAttribute.add(value);
386359
}
387360
}
388361

@@ -696,30 +669,15 @@ public void setAttributeValues(String name, Object[] values, boolean orderMatter
696669
*/
697670
@Override
698671
public void update() {
699-
NamingEnumeration<? extends Attribute> attributesEnumeration = null;
700-
701-
try {
702-
attributesEnumeration = this.updatedAttrs.getAll();
703-
704-
// find what to update
705-
while (attributesEnumeration.hasMore()) {
706-
Attribute a = attributesEnumeration.next();
707-
708-
// if it does not exist it should be added
709-
if (isEmptyAttribute(a)) {
710-
this.originalAttrs.remove(a.getID());
711-
}
712-
else {
713-
// Otherwise it should be set.
714-
this.originalAttrs.put(a);
715-
}
672+
for (NameAwareAttribute attribute : this.updatedAttrs) {
673+
// if it does not exist it should be added
674+
if (isEmptyAttribute(attribute)) {
675+
this.originalAttrs.remove(attribute.getID());
676+
}
677+
else {
678+
// Otherwise it should be set.
679+
this.originalAttrs.put(attribute);
716680
}
717-
}
718-
catch (NamingException ex) {
719-
throw LdapUtils.convertLdapException(ex);
720-
}
721-
finally {
722-
closeNamingEnumeration(attributesEnumeration);
723681
}
724682

725683
// Reset the attributes to be updated
@@ -1359,8 +1317,9 @@ public String toString() {
13591317
builder.append(" {");
13601318

13611319
try {
1362-
for (NamingEnumeration<NameAwareAttribute> i = this.originalAttrs.getAll(); i.hasMore();) {
1363-
Attribute attribute = i.next();
1320+
Iterator<NameAwareAttribute> attributes = this.originalAttrs.iterator();
1321+
while (attributes.hasNext()) {
1322+
NameAwareAttribute attribute = attributes.next();
13641323
if (attribute.size() == 1) {
13651324
builder.append(attribute.getID());
13661325
builder.append('=');
@@ -1374,7 +1333,7 @@ public String toString() {
13741333
}
13751334
}
13761335

1377-
if (i.hasMore()) {
1336+
if (attributes.hasNext()) {
13781337
builder.append(", ");
13791338
}
13801339
}

core/src/main/java/org/springframework/ldap/core/NameAwareAttributes.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2013 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,13 +17,15 @@
1717
package org.springframework.ldap.core;
1818

1919
import java.util.HashMap;
20+
import java.util.Iterator;
2021
import java.util.Locale;
2122
import java.util.Map;
2223

2324
import javax.naming.NamingEnumeration;
2425
import javax.naming.directory.Attribute;
2526
import javax.naming.directory.Attributes;
2627

28+
import org.springframework.lang.NonNull;
2729
import org.springframework.util.Assert;
2830

2931
/**
@@ -32,7 +34,7 @@
3234
* @author Mattias Hellborg Arthursson
3335
* @since 2.0
3436
*/
35-
public final class NameAwareAttributes implements Attributes {
37+
public final class NameAwareAttributes implements Attributes, Iterable<NameAwareAttribute> {
3638

3739
private Map<String, NameAwareAttribute> attributes = new HashMap<String, NameAwareAttribute>();
3840

@@ -81,6 +83,17 @@ public NamingEnumeration<String> getIDs() {
8183
return new IterableNamingEnumeration<String>(this.attributes.keySet());
8284
}
8385

86+
/**
87+
* @inheritDoc
88+
*
89+
* @since 3.3
90+
*/
91+
@NonNull
92+
@Override
93+
public Iterator<NameAwareAttribute> iterator() {
94+
return this.attributes.values().iterator();
95+
}
96+
8497
@Override
8598
public Attribute put(String attrID, Object val) {
8699
Assert.hasLength(attrID, "Attribute ID must not be empty");

core/src/test/java/org/springframework/ldap/core/NameAwareAttributesTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2023 the original author or authors.
2+
* Copyright 2005-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.ldap.core;
1818

19+
import java.util.List;
20+
import java.util.stream.StreamSupport;
21+
1922
import org.junit.Test;
2023

2124
import static org.assertj.core.api.Assertions.assertThat;
@@ -40,4 +43,15 @@ public void removeWhenDifferentCaseThenRemoves() {
4043
assertThat(attributes.size()).isEqualTo(0);
4144
}
4245

46+
@Test
47+
public void iteratorWhenAttributesThenIterates() {
48+
NameAwareAttributes attributes = new NameAwareAttributes();
49+
attributes.put("myID", "value");
50+
attributes.put("myOtherID", "othervalue");
51+
List<String> ids = StreamSupport.stream(attributes.spliterator(), false)
52+
.map(NameAwareAttribute::getID)
53+
.toList();
54+
assertThat(ids).containsOnly("myID", "myOtherID");
55+
}
56+
4357
}

0 commit comments

Comments
 (0)