Skip to content

Commit 0ea9561

Browse files
committed
DATALDAP-181 - Polishing.
Reorder methods. Add overrides to LdapRepository returning List. Consistent override formatting. Add package-info for repository package. Revert import order changes. Original pull request: #20.
1 parent a7dd511 commit 0ea9561

File tree

4 files changed

+135
-78
lines changed

4 files changed

+135
-78
lines changed

src/main/java/org/springframework/data/ldap/repository/LdapRepository.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.ldap.repository;
1717

18+
import java.util.List;
1819
import java.util.Optional;
1920

2021
import javax.naming.Name;
@@ -23,13 +24,34 @@
2324
import org.springframework.ldap.query.LdapQuery;
2425

2526
/**
26-
* Ldap specific extensions to CrudRepository.
27+
* Ldap specific extensions to {@link CrudRepository}.
2728
*
2829
* @author Mattias Hellborg Arthursson
2930
* @author Mark Paluch
3031
*/
3132
public interface LdapRepository<T> extends CrudRepository<T, Name> {
3233

34+
/*
35+
* (non-Javadoc)
36+
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
37+
*/
38+
@Override
39+
<S extends T> List<S> saveAll(Iterable<S> entities);
40+
41+
/*
42+
* (non-Javadoc)
43+
* @see org.springframework.data.repository.CrudRepository#findAll()
44+
*/
45+
@Override
46+
List<T> findAll();
47+
48+
/*
49+
* (non-Javadoc)
50+
* @see org.springframework.data.repository.CrudRepository#findAllById()
51+
*/
52+
@Override
53+
List<T> findAllById(Iterable<Name> names);
54+
3355
/**
3456
* Find one entry matching the specified query.
3557
*
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* LDAP specific repository implementation.
3+
*/
4+
@org.springframework.lang.NonNullApi
5+
package org.springframework.data.ldap.repository;

src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ protected Object getTargetRepository(RepositoryInformation information) {
9696
information.getDomainType());
9797
}
9898

99-
/* (non-Javadoc)
99+
/*
100+
* (non-Javadoc)
100101
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider)
101102
*/
102103
@Override

src/main/java/org/springframework/data/ldap/repository/support/SimpleLdapRepository.java

Lines changed: 105 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
*/
1616
package org.springframework.data.ldap.repository.support;
1717

18+
import static org.springframework.ldap.query.LdapQueryBuilder.*;
19+
20+
import java.util.List;
21+
import java.util.Optional;
22+
import java.util.stream.Collectors;
23+
import java.util.stream.StreamSupport;
24+
25+
import javax.naming.Name;
26+
1827
import org.springframework.dao.EmptyResultDataAccessException;
1928
import org.springframework.data.domain.Persistable;
2029
import org.springframework.data.ldap.repository.LdapRepository;
@@ -28,14 +37,6 @@
2837
import org.springframework.ldap.query.LdapQuery;
2938
import org.springframework.util.Assert;
3039

31-
import javax.naming.Name;
32-
import java.util.List;
33-
import java.util.Optional;
34-
import java.util.stream.Collectors;
35-
import java.util.stream.StreamSupport;
36-
37-
import static org.springframework.ldap.query.LdapQueryBuilder.*;
38-
3940
/**
4041
* Base repository implementation for LDAP.
4142
*
@@ -55,8 +56,8 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
5556
* Creates a new {@link SimpleLdapRepository}.
5657
*
5758
* @param ldapOperations must not be {@literal null}.
58-
* @param odm must not be {@literal null}.
59-
* @param entityType must not be {@literal null}.
59+
* @param odm must not be {@literal null}.
60+
* @param entityType must not be {@literal null}.
6061
*/
6162
public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm, Class<T> entityType) {
6263

@@ -69,31 +70,12 @@ public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper
6970
this.entityType = entityType;
7071
}
7172

72-
/* (non-Javadoc)
73-
* @see org.springframework.data.repository.CrudRepository#count()
74-
*/
75-
@Override
76-
public long count() {
77-
78-
Filter filter = odm.filterFor(entityType, null);
79-
CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler();
80-
LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter);
81-
ldapOperations.search(query, callback);
82-
83-
return callback.getNoOfRows();
84-
}
85-
86-
private <S extends T> boolean isNew(S entity, @Nullable Name id) {
73+
// -------------------------------------------------------------------------
74+
// Methods from CrudRepository
75+
// -------------------------------------------------------------------------
8776

88-
if (entity instanceof Persistable) {
89-
Persistable<?> persistable = (Persistable<?>) entity;
90-
return persistable.isNew();
91-
} else {
92-
return id == null;
93-
}
94-
}
95-
96-
/* (non-Javadoc)
77+
/*
78+
* (non-Javadoc)
9779
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
9880
*/
9981
@Override
@@ -112,18 +94,20 @@ public <S extends T> S save(S entity) {
11294
return entity;
11395
}
11496

115-
/* (non-Javadoc)
97+
/*
98+
* (non-Javadoc)
11699
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
117100
*/
118101
@Override
119-
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
102+
public <S extends T> List<S> saveAll(Iterable<S> entities) {
120103

121104
return StreamSupport.stream(entities.spliterator(), false) //
122105
.map(this::save) //
123106
.collect(Collectors.toList());
124107
}
125108

126-
/* (non-Javadoc)
109+
/*
110+
* (non-Javadoc)
127111
* @see org.springframework.data.repository.CrudRepository#findById(java.io.Serializable)
128112
*/
129113
@Override
@@ -138,32 +122,8 @@ public Optional<T> findById(Name name) {
138122
}
139123
}
140124

141-
/* (non-Javadoc)
142-
* @see org.springframework.data.ldap.repository.LdapRepository#findAll(org.springframework.ldap.query.LdapQuery)
143-
*/
144-
@Override
145-
public List<T> findAll(LdapQuery ldapQuery) {
146-
147-
Assert.notNull(ldapQuery, "LdapQuery must not be null");
148-
return ldapOperations.find(ldapQuery, entityType);
149-
}
150-
151-
/* (non-Javadoc)
152-
* @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery)
153-
*/
154-
@Override
155-
public Optional<T> findOne(LdapQuery ldapQuery) {
156-
157-
Assert.notNull(ldapQuery, "LdapQuery must not be null");
158-
159-
try {
160-
return Optional.ofNullable(ldapOperations.findOne(ldapQuery, entityType));
161-
} catch (EmptyResultDataAccessException e) {
162-
return Optional.empty();
163-
}
164-
}
165-
166-
/* (non-Javadoc)
125+
/*
126+
* (non-Javadoc)
167127
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
168128
*/
169129
@Override
@@ -174,27 +134,45 @@ public boolean existsById(Name name) {
174134
return findById(name).isPresent();
175135
}
176136

177-
/* (non-Javadoc)
137+
/*
138+
* (non-Javadoc)
178139
* @see org.springframework.data.repository.CrudRepository#findAll()
179140
*/
180141
@Override
181142
public List<T> findAll() {
182143
return ldapOperations.findAll(entityType);
183144
}
184145

185-
/* (non-Javadoc)
146+
/*
147+
* (non-Javadoc)
186148
* @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
187149
*/
188150
@Override
189-
public List<T> findAllById(final Iterable<Name> names) {
151+
public List<T> findAllById(Iterable<Name> names) {
190152

191153
return StreamSupport.stream(names.spliterator(), false) //
192154
.map(this::findById) //
193155
.flatMap(Optionals::toStream) //
194156
.collect(Collectors.toList());
195157
}
196158

197-
/* (non-Javadoc)
159+
/*
160+
* (non-Javadoc)
161+
* @see org.springframework.data.repository.CrudRepository#count()
162+
*/
163+
@Override
164+
public long count() {
165+
166+
Filter filter = odm.filterFor(entityType, null);
167+
CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler();
168+
LdapQuery query = query().attributes(OBJECTCLASS_ATTRIBUTE).filter(filter);
169+
ldapOperations.search(query, callback);
170+
171+
return callback.getNoOfRows();
172+
}
173+
174+
/*
175+
* (non-Javadoc)
198176
* @see org.springframework.data.repository.CrudRepository#deleteById(java.io.Serializable)
199177
*/
200178
@Override
@@ -205,7 +183,8 @@ public void deleteById(Name name) {
205183
ldapOperations.unbind(name);
206184
}
207185

208-
/* (non-Javadoc)
186+
/*
187+
* (non-Javadoc)
209188
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
210189
*/
211190
@Override
@@ -216,7 +195,20 @@ public void delete(T entity) {
216195
ldapOperations.delete(entity);
217196
}
218197

219-
/* (non-Javadoc)
198+
/*
199+
* (non-Javadoc)
200+
* @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
201+
*/
202+
@Override
203+
public void deleteAllById(Iterable<? extends Name> names) {
204+
205+
Assert.notNull(names, "Names must not be null.");
206+
207+
names.forEach(this::deleteById);
208+
}
209+
210+
/*
211+
* (non-Javadoc)
220212
* @see org.springframework.data.repository.CrudRepository#deleteAll(java.lang.Iterable)
221213
*/
222214
@Override
@@ -227,19 +219,56 @@ public void deleteAll(Iterable<? extends T> entities) {
227219
entities.forEach(this::delete);
228220
}
229221

222+
/*
223+
* (non-Javadoc)
224+
* @see org.springframework.data.repository.CrudRepository#deleteAll()
225+
*/
230226
@Override
231-
public void deleteAllById(Iterable<? extends Name> names) {
227+
public void deleteAll() {
228+
deleteAll(findAll());
229+
}
232230

233-
Assert.notNull(names, "Names must not be null.");
231+
// -------------------------------------------------------------------------
232+
// Methods from LdapRepository
233+
// ------------------------------------------------------------------------
234234

235-
names.forEach(this::deleteById);
235+
/*
236+
* (non-Javadoc)
237+
* @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery)
238+
*/
239+
@Override
240+
public Optional<T> findOne(LdapQuery ldapQuery) {
241+
242+
Assert.notNull(ldapQuery, "LdapQuery must not be null");
243+
244+
try {
245+
return Optional.ofNullable(ldapOperations.findOne(ldapQuery, entityType));
246+
} catch (EmptyResultDataAccessException e) {
247+
return Optional.empty();
248+
}
236249
}
237250

238-
/* (non-Javadoc)
239-
* @see org.springframework.data.repository.CrudRepository#deleteAll()
251+
/*
252+
* (non-Javadoc)
253+
* @see org.springframework.data.ldap.repository.LdapRepository#findAll(org.springframework.ldap.query.LdapQuery)
240254
*/
241255
@Override
242-
public void deleteAll() {
243-
deleteAll(findAll());
256+
public List<T> findAll(LdapQuery ldapQuery) {
257+
258+
Assert.notNull(ldapQuery, "LdapQuery must not be null");
259+
return ldapOperations.find(ldapQuery, entityType);
244260
}
261+
262+
263+
private <S extends T> boolean isNew(S entity, @Nullable Name id) {
264+
265+
if (entity instanceof Persistable) {
266+
Persistable<?> persistable = (Persistable<?>) entity;
267+
return persistable.isNew();
268+
} else {
269+
return id == null;
270+
}
271+
}
272+
273+
245274
}

0 commit comments

Comments
 (0)