1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.servlet.mvc.condition;
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import javax.servlet.http.HttpServletRequest;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public final class RequestConditionHolder extends AbstractRequestCondition<RequestConditionHolder> {
38
39 private final RequestCondition<Object> condition;
40
41
42
43
44
45
46 @SuppressWarnings("unchecked")
47 public RequestConditionHolder(RequestCondition<?> requestCondition) {
48 this.condition = (RequestCondition<Object>) requestCondition;
49 }
50
51
52
53
54
55 public RequestCondition<?> getCondition() {
56 return this.condition;
57 }
58
59 @Override
60 protected Collection<?> getContent() {
61 return (this.condition != null ? Collections.singleton(this.condition) : Collections.emptyList());
62 }
63
64 @Override
65 protected String getToStringInfix() {
66 return " ";
67 }
68
69
70
71
72
73
74 @Override
75 public RequestConditionHolder combine(RequestConditionHolder other) {
76 if (this.condition == null && other.condition == null) {
77 return this;
78 }
79 else if (this.condition == null) {
80 return other;
81 }
82 else if (other.condition == null) {
83 return this;
84 }
85 else {
86 assertEqualConditionTypes(other);
87 RequestCondition<?> combined = (RequestCondition<?>) this.condition.combine(other.condition);
88 return new RequestConditionHolder(combined);
89 }
90 }
91
92
93
94
95 private void assertEqualConditionTypes(RequestConditionHolder other) {
96 Class<?> clazz = this.condition.getClass();
97 Class<?> otherClazz = other.condition.getClass();
98 if (!clazz.equals(otherClazz)) {
99 throw new ClassCastException("Incompatible request conditions: " + clazz + " and " + otherClazz);
100 }
101 }
102
103
104
105
106
107
108 @Override
109 public RequestConditionHolder getMatchingCondition(HttpServletRequest request) {
110 if (this.condition == null) {
111 return this;
112 }
113 RequestCondition<?> match = (RequestCondition<?>) this.condition.getMatchingCondition(request);
114 return (match != null ? new RequestConditionHolder(match) : null);
115 }
116
117
118
119
120
121
122 @Override
123 public int compareTo(RequestConditionHolder other, HttpServletRequest request) {
124 if (this.condition == null && other.condition == null) {
125 return 0;
126 }
127 else if (this.condition == null) {
128 return 1;
129 }
130 else if (other.condition == null) {
131 return -1;
132 }
133 else {
134 assertEqualConditionTypes(other);
135 return this.condition.compareTo(other.condition, request);
136 }
137 }
138
139 }