|
1 | 1 | package org.springdoc.demo.app2.repository;
|
2 | 2 |
|
| 3 | +import org.springframework.beans.BeanWrapper; |
| 4 | +import org.springframework.beans.BeanWrapperImpl; |
| 5 | +import org.springframework.data.domain.Pageable; |
| 6 | +import org.springframework.data.domain.Sort; |
3 | 7 | import org.springframework.data.repository.CrudRepository;
|
4 | 8 | import org.springframework.data.repository.NoRepositoryBean;
|
5 | 9 | import org.springframework.util.Assert;
|
6 | 10 |
|
| 11 | +import java.beans.BeanInfo; |
| 12 | +import java.beans.PropertyDescriptor; |
7 | 13 | import java.util.*;
|
8 | 14 |
|
9 | 15 | @NoRepositoryBean
|
@@ -13,6 +19,12 @@ public abstract class HashMapRepository<T, ID> implements CrudRepository<T, ID>
|
13 | 19 |
|
14 | 20 | abstract <S extends T> ID getEntityId(S entity);
|
15 | 21 |
|
| 22 | +private final BeanWrapper entityBeanInfo; |
| 23 | + |
| 24 | +protected HashMapRepository(Class<T> clazz) { |
| 25 | +entityBeanInfo = new BeanWrapperImpl(clazz); |
| 26 | +} |
| 27 | + |
16 | 28 | @Override
|
17 | 29 | public <S extends T> S save(S entity) {
|
18 | 30 | Assert.notNull(entity, "entity cannot be null");
|
@@ -34,6 +46,58 @@ public Collection<T> findAll() {
|
34 | 46 | return entities.values();
|
35 | 47 | }
|
36 | 48 |
|
| 49 | +public List<T> findAll(Pageable pageable) { |
| 50 | +final List<T> result; |
| 51 | +final Sort sort = pageable.getSort(); |
| 52 | +if (sort != null) { |
| 53 | +Comparator<T> comp = new Comparator<T>() { |
| 54 | +@Override |
| 55 | +public int compare(T t1, T t2) { |
| 56 | +int result = 0; |
| 57 | +for (Sort.Order o : sort) { |
| 58 | +final String prop = o.getProperty(); |
| 59 | +PropertyDescriptor propDesc = entityBeanInfo.getPropertyDescriptor(prop); |
| 60 | +result = ((Comparable<T>) propDesc.createPropertyEditor(t1).getValue()) |
| 61 | +.compareTo((T) propDesc.createPropertyEditor(t2).getValue()); |
| 62 | +if (o.isDescending()) { |
| 63 | +result = -result; |
| 64 | +} |
| 65 | +if (result != 0) break; |
| 66 | +} |
| 67 | +return result; |
| 68 | +} |
| 69 | +}; |
| 70 | +final Set<T> set = new TreeSet<>(comp); |
| 71 | +set.addAll(entities.values()); |
| 72 | +result = getPageSlice(pageable, set); |
| 73 | +} else { |
| 74 | +result = getPageSlice(pageable, entities.values()); |
| 75 | +} |
| 76 | +return result; |
| 77 | +} |
| 78 | + |
| 79 | +private List<T> getPageSlice(Pageable pageable, Collection<T> col) { |
| 80 | +final ArrayList<T> all = new ArrayList<>(col); |
| 81 | +final int size = all.size(); |
| 82 | +final int psize = pageable.getPageSize(); |
| 83 | +final int pnum = pageable.getPageNumber(); |
| 84 | +if (pnum < 1) { |
| 85 | +throw new IllegalArgumentException("page number must be 1 or more"); |
| 86 | +} |
| 87 | +if (psize < 1) { |
| 88 | +throw new IllegalArgumentException("page size must be 1 or more"); |
| 89 | +} |
| 90 | +// inclusive |
| 91 | +final int begin = (pnum - 1) * psize; |
| 92 | +// exclusive |
| 93 | +final int end = Math.min(begin + psize, size); |
| 94 | +if (size < begin) { |
| 95 | +return new ArrayList<>(); |
| 96 | +} |
| 97 | +// return of slice is valid because all is local to this method |
| 98 | +return all.subList(begin, end); |
| 99 | +} |
| 100 | + |
37 | 101 | @Override
|
38 | 102 | public long count() {
|
39 | 103 | return entities.keySet().size();
|
|
0 commit comments