1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  package org.apache.hc.client5.http.cache;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  
33  
34  
35  
36  
37  
38  
39  @Contract(threading = ThreadingBehavior.IMMUTABLE)
40  public final class RequestCacheControl implements CacheControl {
41  
42      private final long maxAge;
43      private final long maxStale;
44      private final long minFresh;
45      private final boolean noCache;
46      private final boolean noStore;
47      private final boolean onlyIfCached;
48      private final long staleIfError;
49  
50      RequestCacheControl(final long maxAge, final long maxStale, final long minFresh, final boolean noCache,
51                                 final boolean noStore, final boolean onlyIfCached, final long staleIfError) {
52          this.maxAge = maxAge;
53          this.maxStale = maxStale;
54          this.minFresh = minFresh;
55          this.noCache = noCache;
56          this.noStore = noStore;
57          this.onlyIfCached = onlyIfCached;
58          this.staleIfError = staleIfError;
59      }
60  
61      
62  
63  
64  
65  
66      @Override
67      public long getMaxAge() {
68          return maxAge;
69      }
70  
71      
72  
73  
74  
75  
76      public long getMaxStale() {
77          return maxStale;
78      }
79  
80      
81  
82  
83  
84  
85      public long getMinFresh() {
86          return minFresh;
87      }
88  
89      
90  
91  
92  
93  
94      @Override
95      public boolean isNoCache() {
96          return noCache;
97      }
98  
99      
100 
101 
102 
103 
104     @Override
105     public boolean isNoStore() {
106         return noStore;
107     }
108 
109     
110 
111 
112 
113 
114     public boolean isOnlyIfCached() {
115         return onlyIfCached;
116     }
117 
118     
119 
120 
121 
122 
123     @Override
124     public long getStaleIfError() {
125         return staleIfError;
126     }
127 
128     @Override
129     public String toString() {
130         final StringBuilder buf = new StringBuilder();
131         buf.append("[");
132         if (maxAge >= 0) {
133             buf.append("max-age=").append(maxAge).append(",");
134         }
135         if (maxStale >= 0) {
136             buf.append("max-stale=").append(maxStale).append(",");
137         }
138         if (minFresh >= 0) {
139             buf.append("min-fresh=").append(minFresh).append(",");
140         }
141         if (noCache) {
142             buf.append("no-cache").append(",");
143         }
144         if (noStore) {
145             buf.append("no-store").append(",");
146         }
147         if (onlyIfCached) {
148             buf.append("only-if-cached").append(",");
149         }
150         if (staleIfError >= 0) {
151             buf.append("stale-if-error").append(staleIfError).append(",");
152         }
153         if (buf.charAt(buf.length() - 1) == ',') {
154             buf.setLength(buf.length() - 1);
155         }
156         buf.append("]");
157         return buf.toString();
158     }
159 
160     public static Builder builder() {
161         return new Builder();
162     }
163 
164     public static final RequestCacheControl DEFAULT = builder().build();
165 
166     public static class Builder {
167 
168         private long maxAge = -1;
169         private long maxStale = -1;
170         private long minFresh = -1;
171         private boolean noCache;
172         private boolean noStore;
173         private boolean onlyIfCached;
174         private long staleIfError = -1;
175 
176         Builder() {
177         }
178 
179         public long getMaxAge() {
180             return maxAge;
181         }
182 
183         public Builder setMaxAge(final long maxAge) {
184             this.maxAge = maxAge;
185             return this;
186         }
187 
188         public long getMaxStale() {
189             return maxStale;
190         }
191 
192         public Builder setMaxStale(final long maxStale) {
193             this.maxStale = maxStale;
194             return this;
195         }
196 
197         public long getMinFresh() {
198             return minFresh;
199         }
200 
201         public Builder setMinFresh(final long minFresh) {
202             this.minFresh = minFresh;
203             return this;
204         }
205 
206         public boolean isNoCache() {
207             return noCache;
208         }
209 
210         public Builder setNoCache(final boolean noCache) {
211             this.noCache = noCache;
212             return this;
213         }
214 
215         public boolean isNoStore() {
216             return noStore;
217         }
218 
219         public Builder setNoStore(final boolean noStore) {
220             this.noStore = noStore;
221             return this;
222         }
223 
224         public boolean isOnlyIfCached() {
225             return onlyIfCached;
226         }
227 
228         public Builder setOnlyIfCached(final boolean onlyIfCached) {
229             this.onlyIfCached = onlyIfCached;
230             return this;
231         }
232 
233         public long getStaleIfError() {
234             return staleIfError;
235         }
236 
237         public Builder setStaleIfError(final long staleIfError) {
238             this.staleIfError = staleIfError;
239             return this;
240         }
241 
242         public RequestCacheControl build() {
243             return new RequestCacheControl(maxAge, maxStale, minFresh, noCache, noStore, onlyIfCached, staleIfError);
244         }
245 
246     }
247 }