DATACMNS-867 - Second draft.

This commit is contained in:
Oliver Gierke
2016-06-21 16:52:28 +02:00
parent 57ed50a730
commit d4811e29d9
222 changed files with 2297 additions and 2138 deletions

View File

@@ -71,7 +71,7 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getOffset()
*/
public int getOffset() {
public long getOffset() {
return page * size;
}

View File

@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
@@ -34,14 +35,14 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
private static final long serialVersionUID = 867755909294344406L;
private final List<T> content = new ArrayList<T>();
private final List<T> content = new ArrayList<>();
private final Pageable pageable;
/**
* Creates a new {@link Chunk} with the given content and the given governing {@link Pageable}.
*
* @param content must not be {@literal null}.
* @param pageable can be {@literal null}.
* @param pageable must not be {@literal null}.
*/
public Chunk(List<T> content, Pageable pageable) {
@@ -56,7 +57,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
* @see org.springframework.data.domain.Slice#getNumber()
*/
public int getNumber() {
return pageable == null ? 0 : pageable.getPageNumber();
return pageable == Pageable.NONE ? 0 : pageable.getPageNumber();
}
/*
@@ -64,7 +65,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
* @see org.springframework.data.domain.Slice#getSize()
*/
public int getSize() {
return pageable == null ? 0 : pageable.getPageSize();
return pageable == Pageable.NONE ? 0 : pageable.getPageSize();
}
/*
@@ -104,7 +105,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
* @see org.springframework.data.domain.Slice#nextPageable()
*/
public Pageable nextPageable() {
return hasNext() ? pageable.next() : null;
return hasNext() ? pageable.next() : Pageable.NONE;
}
/*
@@ -112,12 +113,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
* @see org.springframework.data.domain.Slice#previousPageable()
*/
public Pageable previousPageable() {
if (hasPrevious()) {
return pageable.previousOrFirst();
}
return null;
return hasPrevious() ? pageable.previousOrFirst() : Pageable.NONE;
}
/*
@@ -140,8 +136,9 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
* (non-Javadoc)
* @see org.springframework.data.domain.Slice#getSort()
*/
@Override
public Sort getSort() {
return pageable == null ? null : pageable.getSort();
return pageable.getSort();
}
/*
@@ -158,17 +155,11 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
* @param converter must not be {@literal null}.
* @return
*/
protected <S> List<S> getConvertedContent(Converter<? super T, ? extends S> converter) {
protected <U> List<U> getConvertedContent(Converter<? super T, ? extends U> converter) {
Assert.notNull(converter, "Converter must not be null!");
List<S> result = new ArrayList<S>(content.size());
for (T element : this) {
result.add(converter.convert(element));
}
return result;
return this.stream().map(it -> converter.convert(it)).collect(Collectors.toList());
}
/*
@@ -189,7 +180,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
Chunk<?> that = (Chunk<?>) obj;
boolean contentEqual = this.content.equals(that.content);
boolean pageableEqual = this.pageable == null ? that.pageable == null : this.pageable.equals(that.pageable);
boolean pageableEqual = this.pageable.equals(that.pageable);
return contentEqual && pageableEqual;
}
@@ -203,7 +194,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
int result = 17;
result += 31 * (pageable == null ? 0 : pageable.hashCode());
result += 31 * pageable.hashCode();
result += 31 * content.hashCode();
return result;

View File

@@ -48,7 +48,7 @@ public class Example<T> {
* @return
*/
public static <T> Example<T> of(T probe) {
return new Example<T>(probe, ExampleMatcher.matching());
return new Example<>(probe, ExampleMatcher.matching());
}
/**
@@ -59,7 +59,7 @@ public class Example<T> {
* @return
*/
public static <T> Example<T> of(T probe, ExampleMatcher matcher) {
return new Example<T>(probe, matcher);
return new Example<>(probe, matcher);
}
/**

View File

@@ -112,7 +112,7 @@ public class ExampleMatcher {
Assert.notEmpty(ignoredPaths, "IgnoredPaths must not be empty!");
Assert.noNullElements(ignoredPaths, "IgnoredPaths must not contain null elements!");
Set<String> newIgnoredPaths = new LinkedHashSet<String>(this.ignoredPaths);
Set<String> newIgnoredPaths = new LinkedHashSet<>(this.ignoredPaths);
newIgnoredPaths.addAll(Arrays.asList(ignoredPaths));
return new ExampleMatcher(nullHandler, defaultStringMatcher, propertySpecifiers, newIgnoredPaths, defaultIgnoreCase,

View File

@@ -47,5 +47,5 @@ public interface Page<T> extends Slice<T> {
* @return a new {@link Page} with the content of the current one mapped by the given {@link Converter}.
* @since 1.10
*/
<S> Page<S> map(Converter<? super T, ? extends S> converter);
<U> Page<U> map(Converter<? super T, ? extends U> converter);
}

View File

@@ -16,6 +16,7 @@
package org.springframework.data.domain;
import java.util.List;
import java.util.Optional;
import org.springframework.core.convert.converter.Converter;
@@ -45,8 +46,13 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
super(content, pageable);
this.pageable = pageable;
this.total = !content.isEmpty() && pageable != null && pageable.getOffset() + pageable.getPageSize() > total
? pageable.getOffset() + content.size() : total;
Optional<Pageable> foo = Pageable.NONE == pageable ? Optional.empty() : Optional.of(pageable);
this.total = foo.filter(it -> !content.isEmpty())//
.filter(it -> it.getOffset() + it.getPageSize() > total)//
.map(it -> it.getOffset() + content.size())//
.orElse(total);
}
/**
@@ -56,7 +62,7 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
* @param content must not be {@literal null}.
*/
public PageImpl(List<T> content) {
this(content, null, null == content ? 0 : content.size());
this(content, Pageable.NONE, null == content ? 0 : content.size());
}
/*
@@ -100,8 +106,8 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
* @see org.springframework.data.domain.Slice#transform(org.springframework.core.convert.converter.Converter)
*/
@Override
public <S> Page<S> map(Converter<? super T, ? extends S> converter) {
return new PageImpl<S>(getConvertedContent(converter), pageable, total);
public <U> Page<U> map(Converter<? super T, ? extends U> converter) {
return new PageImpl<>(getConvertedContent(converter), pageable, total);
}
/*

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.domain;
import java.util.Optional;
import org.springframework.data.domain.Sort.Direction;
/**
@@ -35,9 +37,11 @@ public class PageRequest extends AbstractPageRequest {
*
* @param page zero-based page index.
* @param size the size of the page to be returned.
* @deprecated use {@link #of(int, int)} instead.
*/
@Deprecated
public PageRequest(int page, int size) {
this(page, size, null);
this(page, size, Sort.unsorted());
}
/**
@@ -47,7 +51,9 @@ public class PageRequest extends AbstractPageRequest {
* @param size the size of the page to be returned.
* @param direction the direction of the {@link Sort} to be specified, can be {@literal null}.
* @param properties the properties to sort by, must not be {@literal null} or empty.
* @deprecated use {@link #of(int, int, Direction, String...)} instead.
*/
@Deprecated
public PageRequest(int page, int size, Direction direction, String... properties) {
this(page, size, new Sort(direction, properties));
}
@@ -58,12 +64,28 @@ public class PageRequest extends AbstractPageRequest {
* @param page zero-based page index.
* @param size the size of the page to be returned.
* @param sort can be {@literal null}.
* @deprecated use {@link #of(int, int, Optional)} instead.
*/
@Deprecated
public PageRequest(int page, int size, Sort sort) {
super(page, size);
this.sort = sort;
}
public static PageRequest of(int page, int site) {
return of(page, site, Sort.unsorted());
}
public static PageRequest of(int page, int site, Sort sort) {
return new PageRequest(page, site, sort);
}
public static PageRequest of(int page, int size, Direction direction, String... properties) {
return of(page, size, new Sort(direction, properties));
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getSort()
@@ -113,9 +135,7 @@ public class PageRequest extends AbstractPageRequest {
PageRequest that = (PageRequest) obj;
boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort);
return super.equals(that) && sortEqual;
return super.equals(that) && this.sort.equals(that.sort);
}
/*
@@ -124,7 +144,7 @@ public class PageRequest extends AbstractPageRequest {
*/
@Override
public int hashCode() {
return 31 * super.hashCode() + (null == sort ? 0 : sort.hashCode());
return 31 * super.hashCode() + sort.hashCode();
}
/*
@@ -133,7 +153,6 @@ public class PageRequest extends AbstractPageRequest {
*/
@Override
public String toString() {
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(),
sort == null ? null : sort.toString());
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(), sort);
}
}

View File

@@ -41,7 +41,7 @@ public interface Pageable {
*
* @return the offset to be taken
*/
int getOffset();
long getOffset();
/**
* Returns the sorting parameters.
@@ -50,6 +50,10 @@ public interface Pageable {
*/
Sort getSort();
default Sort getSortOr(Sort sort) {
return getSort().isSorted() ? getSort() : sort;
}
/**
* Returns the {@link Pageable} requesting the next {@link Page}.
*
@@ -78,4 +82,47 @@ public interface Pageable {
* @return
*/
boolean hasPrevious();
public static Pageable NONE = new Pageable() {
@Override
public Pageable previousOrFirst() {
return this;
}
@Override
public Pageable next() {
return this;
}
@Override
public boolean hasPrevious() {
return false;
}
@Override
public Sort getSort() {
return Sort.unsorted();
}
@Override
public int getPageSize() {
throw new UnsupportedOperationException();
}
@Override
public int getPageNumber() {
throw new UnsupportedOperationException();
}
@Override
public long getOffset() {
throw new UnsupportedOperationException();
}
@Override
public Pageable first() {
return this;
}
};
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.domain;
import java.util.List;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.util.Streamable;
/**
* A slice of data that indicates whether there's a next or previous slice available. Allows to obtain a
@@ -26,7 +27,7 @@ import org.springframework.core.convert.converter.Converter;
* @author Oliver Gierke
* @since 1.8
*/
public interface Slice<T> extends Iterable<T> {
public interface Slice<T> extends Streamable<T> {
/**
* Returns the number of the current {@link Slice}. Is always non-negative.
@@ -123,5 +124,5 @@ public interface Slice<T> extends Iterable<T> {
* @return a new {@link Slice} with the content of the current one mapped by the given {@link Converter}.
* @since 1.10
*/
<S> Slice<S> map(Converter<? super T, ? extends S> converter);
<U> Slice<U> map(Converter<? super T, ? extends U> converter);
}

View File

@@ -42,6 +42,7 @@ public class SliceImpl<T> extends Chunk<T> {
public SliceImpl(List<T> content, Pageable pageable, boolean hasNext) {
super(content, pageable);
this.hasNext = hasNext;
this.pageable = pageable;
}
@@ -69,8 +70,8 @@ public class SliceImpl<T> extends Chunk<T> {
* @see org.springframework.data.domain.Slice#transform(org.springframework.core.convert.converter.Converter)
*/
@Override
public <S> Slice<S> map(Converter<? super T, ? extends S> converter) {
return new SliceImpl<S>(getConvertedContent(converter), pageable, hasNext);
public <U> Slice<U> map(Converter<? super T, ? extends U> converter) {
return new SliceImpl<>(getConvertedContent(converter), pageable, hasNext);
}
/*

View File

@@ -18,10 +18,14 @@ package org.springframework.data.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -34,6 +38,9 @@ import org.springframework.util.StringUtils;
public class Sort implements Iterable<org.springframework.data.domain.Sort.Order>, Serializable {
private static final long serialVersionUID = 5737186511678863905L;
private static final Sort UNSORTED = new Sort(new Order[0]);
public static final Direction DEFAULT_DIRECTION = Direction.ASC;
private final List<Order> orders;
@@ -43,6 +50,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
*
* @param orders must not be {@literal null}.
*/
@Deprecated
public Sort(Order... orders) {
this(Arrays.asList(orders));
}
@@ -51,21 +59,23 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
* Creates a new {@link Sort} instance.
*
* @param orders must not be {@literal null} or contain {@literal null}.
* @deprecated see {@link Sort#by(List)}
*/
@Deprecated
public Sort(List<Order> orders) {
if (null == orders || orders.isEmpty()) {
throw new IllegalArgumentException("You have to provide at least one sort property to sort by!");
}
Assert.notNull(orders, "Orders must not be null!");
this.orders = orders;
this.orders = Collections.unmodifiableList(orders);
}
/**
* Creates a new {@link Sort} instance. Order defaults to {@value Direction#ASC}.
*
* @param properties must not be {@literal null} or contain {@literal null} or empty strings
* @deprecated use {@link Sort#by(String...)}
*/
@Deprecated
public Sort(String... properties) {
this(DEFAULT_DIRECTION, properties);
}
@@ -77,7 +87,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
* @param properties must not be {@literal null}, empty or contain {@literal null} or empty strings.
*/
public Sort(Direction direction, String... properties) {
this(direction, properties == null ? new ArrayList<String>() : Arrays.asList(properties));
this(direction, properties == null ? new ArrayList<>() : Arrays.asList(properties));
}
/**
@@ -92,13 +102,45 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
throw new IllegalArgumentException("You have to provide at least one property to sort by!");
}
this.orders = new ArrayList<Order>(properties.size());
this.orders = new ArrayList<>(properties.size());
for (String property : properties) {
this.orders.add(new Order(direction, property));
}
}
public static Sort by(String... properties) {
return properties.length == 0 ? Sort.unsorted() : new Sort(properties);
}
public static Sort by(List<Order> orders) {
return orders.isEmpty() ? Sort.unsorted() : new Sort(orders);
}
public static Sort by(Order... orders) {
return new Sort(orders);
}
public static Sort unsorted() {
return UNSORTED;
}
public Sort descending() {
return withDirection(Direction.DESC);
}
public Sort ascending() {
return withDirection(Direction.ASC);
}
private Sort withDirection(Direction direction) {
return new Sort(orders.stream().map(it -> new Order(direction, it.getProperty())).collect(Collectors.toList()));
}
public boolean isSorted() {
return !orders.isEmpty();
}
/**
* Returns a new {@link Sort} consisting of the {@link Order}s of the current {@link Sort} combined with the given
* ones.
@@ -112,7 +154,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
return this;
}
ArrayList<Order> these = new ArrayList<Order>(this.orders);
ArrayList<Order> these = new ArrayList<>(this.orders);
for (Order order : sort) {
these.add(order);
@@ -184,7 +226,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
*/
@Override
public String toString() {
return StringUtils.collectionToCommaDelimitedString(orders);
return orders.isEmpty() ? "UNSORTED" : StringUtils.collectionToCommaDelimitedString(orders);
}
/**
@@ -240,12 +282,12 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
* @param value
* @return
*/
public static Direction fromStringOrNull(String value) {
public static Optional<Direction> fromOptionalString(String value) {
try {
return fromString(value);
return Optional.of(fromString(value));
} catch (IllegalArgumentException e) {
return null;
return Optional.empty();
}
}
}

View File

@@ -21,7 +21,6 @@ import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.jaxb.SpringDataJaxb.OrderDto;
import org.springframework.data.domain.jaxb.SpringDataJaxb.PageRequestDto;
import org.springframework.data.domain.jaxb.SpringDataJaxb.SortDto;
@@ -43,7 +42,7 @@ class PageableAdapter extends XmlAdapter<PageRequestDto, Pageable> {
SortDto sortDto = SortAdapter.INSTANCE.marshal(request.getSort());
PageRequestDto dto = new PageRequestDto();
dto.orders = sortDto == null ? Collections.<OrderDto> emptyList() : sortDto.orders;
dto.orders = sortDto == null ? Collections.<OrderDto>emptyList() : sortDto.orders;
dto.page = request.getPageNumber();
dto.size = request.getPageSize();
@@ -58,13 +57,12 @@ class PageableAdapter extends XmlAdapter<PageRequestDto, Pageable> {
public Pageable unmarshal(PageRequestDto v) {
if (v.orders.isEmpty()) {
return new PageRequest(v.page, v.size);
return PageRequest.of(v.page, v.size);
}
SortDto sortDto = new SortDto();
sortDto.orders = v.orders;
Sort sort = SortAdapter.INSTANCE.unmarshal(sortDto);
return new PageRequest(v.page, v.size, sort);
return PageRequest.of(v.page, v.size, SortAdapter.INSTANCE.unmarshal(sortDto));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2016 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.
@@ -52,6 +52,6 @@ public class SortAdapter extends XmlAdapter<SortDto, Sort> {
*/
@Override
public Sort unmarshal(SortDto source) {
return source == null ? null : new Sort(SpringDataJaxb.unmarshal(source.orders, OrderAdapter.INSTANCE));
return source == null ? Sort.unsorted() : Sort.by(SpringDataJaxb.unmarshal(source.orders, OrderAdapter.INSTANCE));
}
}

View File

@@ -60,7 +60,7 @@ public abstract class SpringDataJaxb {
public static class PageRequestDto {
@XmlAttribute int page, size;
@XmlElement(name = "order", namespace = NAMESPACE) List<OrderDto> orders = new ArrayList<OrderDto>();
@XmlElement(name = "order", namespace = NAMESPACE) List<OrderDto> orders = new ArrayList<>();
}
/**
@@ -72,7 +72,7 @@ public abstract class SpringDataJaxb {
@XmlAccessorType(XmlAccessType.FIELD)
public static class SortDto {
@XmlElement(name = "order", namespace = SpringDataJaxb.NAMESPACE) List<OrderDto> orders = new ArrayList<OrderDto>();
@XmlElement(name = "order", namespace = SpringDataJaxb.NAMESPACE) List<OrderDto> orders = new ArrayList<>();
}
/**
@@ -116,7 +116,7 @@ public abstract class SpringDataJaxb {
return Collections.emptyList();
}
List<T> result = new ArrayList<T>(source.size());
List<T> result = new ArrayList<>(source.size());
for (S element : source) {
try {
@@ -144,7 +144,7 @@ public abstract class SpringDataJaxb {
return Collections.emptyList();
}
List<S> result = new ArrayList<S>();
List<S> result = new ArrayList<>();
for (T element : source) {
try {