Skip to content

Commit

Permalink
Make NameAwareAttributes Iterable
Browse files Browse the repository at this point in the history
Closes gh-937
  • Loading branch information
jzheaux committed Jan 7, 2025
1 parent 848831d commit 9c978b7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
Expand Down Expand Up @@ -238,26 +239,16 @@ public String[] getNamesOfModifiedAttributes() {

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

NamingEnumeration<? extends Attribute> attributesEnumeration;
if (isUpdateMode()) {
attributesEnumeration = this.updatedAttrs.getAll();
for (NameAwareAttribute attribute : this.updatedAttrs) {
tmpList.add(attribute.getID());
}
}
else {
attributesEnumeration = this.originalAttrs.getAll();
}

try {
while (attributesEnumeration.hasMore()) {
Attribute oneAttribute = attributesEnumeration.next();
tmpList.add(oneAttribute.getID());
for (NameAwareAttribute attribute : this.originalAttrs) {
tmpList.add(attribute.getID());
}
}
catch (NamingException ex) {
throw LdapUtils.convertLdapException(ex);
}
finally {
closeNamingEnumeration(attributesEnumeration);
}

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

List<ModificationItem> tmpList = new LinkedList<ModificationItem>();
NamingEnumeration<? extends Attribute> 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()) {
Expand All @@ -318,10 +295,8 @@ public ModificationItem[] getModificationItems() {
* (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<ModificationItem> modificationList)
throws NamingException {
private void collectModifications(NameAwareAttribute changedAttr, List<ModificationItem> modificationList) {
NameAwareAttribute currentAttribute = this.originalAttrs.get(changedAttr.getID());
if (currentAttribute != null && changedAttr.hasValuesAsNames()) {
try {
Expand Down Expand Up @@ -372,17 +347,15 @@ else if (changedAttr.size() > 0) {
}
}

private void collectModifications(Attribute originalAttr, Attribute changedAttr,
List<ModificationItem> modificationList) throws NamingException {
private void collectModifications(NameAwareAttribute originalAttr, NameAwareAttribute changedAttr,
List<ModificationItem> 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);
}
}

Expand Down Expand Up @@ -696,30 +669,15 @@ public void setAttributeValues(String name, Object[] values, boolean orderMatter
*/
@Override
public void update() {
NamingEnumeration<? extends Attribute> 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
Expand Down Expand Up @@ -1359,8 +1317,9 @@ public String toString() {
builder.append(" {");

try {
for (NamingEnumeration<NameAwareAttribute> i = this.originalAttrs.getAll(); i.hasMore();) {
Attribute attribute = i.next();
Iterator<NameAwareAttribute> attributes = this.originalAttrs.iterator();
while (attributes.hasNext()) {
NameAwareAttribute attribute = attributes.next();
if (attribute.size() == 1) {
builder.append(attribute.getID());
builder.append('=');
Expand All @@ -1374,7 +1333,7 @@ public String toString() {
}
}

if (i.hasMore()) {
if (attributes.hasNext()) {
builder.append(", ");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,13 +17,15 @@
package org.springframework.ldap.core;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;

import org.springframework.lang.NonNull;
import org.springframework.util.Assert;

/**
Expand All @@ -32,7 +34,7 @@
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
public final class NameAwareAttributes implements Attributes {
public final class NameAwareAttributes implements Attributes, Iterable<NameAwareAttribute> {

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

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

/**
* @inheritDoc
*
* @since 3.3
*/
@NonNull
@Override
public Iterator<NameAwareAttribute> iterator() {
return this.attributes.values().iterator();
}

@Override
public Attribute put(String attrID, Object val) {
Assert.hasLength(attrID, "Attribute ID must not be empty");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -40,4 +43,15 @@ public void removeWhenDifferentCaseThenRemoves() {
assertThat(attributes.size()).isEqualTo(0);
}

@Test
public void iteratorWhenAttributesThenIterates() {
NameAwareAttributes attributes = new NameAwareAttributes();
attributes.put("myID", "value");
attributes.put("myOtherID", "othervalue");
List<String> ids = StreamSupport.stream(attributes.spliterator(), false)
.map(NameAwareAttribute::getID)
.toList();
assertThat(ids).containsOnly("myID", "myOtherID");
}

}

0 comments on commit 9c978b7

Please sign in to comment.