1 /*
2  * Copyright 2014-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.domain;
17
18 import java.util.List;
19 import java.util.function.Function;
20
21 import org.springframework.lang.Nullable;
22
23 /**
24  * Default implementation of {@link Slice}.
25  *
26  * @author Oliver Gierke
27  * @author Keegan Witt
28  * @since 1.8
29  */

30 public class SliceImpl<T> extends Chunk<T> {
31
32     private static final long serialVersionUID = 867755909294344406L;
33
34     private final boolean hasNext;
35     private final Pageable pageable;
36
37     /**
38      * Creates a new {@link Slice} with the given content and {@link Pageable}.
39      *
40      * @param content the content of this {@link Slice}, must not be {@literal null}.
41      * @param pageable the paging information, must not be {@literal null}.
42      * @param hasNext whether there's another slice following the current one.
43      */

44     public SliceImpl(List<T> content, Pageable pageable, boolean hasNext) {
45
46         super(content, pageable);
47
48         this.hasNext = hasNext;
49         this.pageable = pageable;
50     }
51
52     /**
53      * Creates a new {@link SliceImpl} with the given content. This will result in the created {@link Slice} being
54      * identical to the entire {@link List}.
55      *
56      * @param content must not be {@literal null}.
57      */

58     public SliceImpl(List<T> content) {
59         this(content, Pageable.unpaged(), false);
60     }
61
62     /*
63      * (non-Javadoc)
64      * @see org.springframework.data.domain.Slice#hasNext()
65      */

66     public boolean hasNext() {
67         return hasNext;
68     }
69
70     /*
71      * (non-Javadoc)
72      * @see org.springframework.data.domain.Slice#transform(org.springframework.core.convert.converter.Converter)
73      */

74     @Override
75     public <U> Slice<U> map(Function<? super T, ? extends U> converter) {
76         return new SliceImpl<>(getConvertedContent(converter), pageable, hasNext);
77     }
78
79     /*
80      * (non-Javadoc)
81      * @see java.lang.Object#toString()
82      */

83     @Override
84     public String toString() {
85
86         String contentType = "UNKNOWN";
87         List<T> content = getContent();
88
89         if (content.size() > 0) {
90             contentType = content.get(0).getClass().getName();
91         }
92
93         return String.format("Slice %d containing %s instances", getNumber(), contentType);
94     }
95
96     /*
97      * (non-Javadoc)
98      * @see java.lang.Object#equals(java.lang.Object)
99      */

100     @Override
101     public boolean equals(@Nullable Object obj) {
102
103         if (this == obj) {
104             return true;
105         }
106
107         if (!(obj instanceof SliceImpl<?>)) {
108             return false;
109         }
110
111         SliceImpl<?> that = (SliceImpl<?>) obj;
112
113         return this.hasNext == that.hasNext && super.equals(obj);
114     }
115
116     /*
117      * (non-Javadoc)
118      * @see java.lang.Object#hashCode()
119      */

120     @Override
121     public int hashCode() {
122
123         int result = 17;
124
125         result += 31 * (hasNext ? 1 : 0);
126         result += 31 * super.hashCode();
127
128         return result;
129     }
130 }
131