1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.http;
17 
18 import java.util.stream.Stream;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 
21 /**
22  * The set of HTTP status families defined by the standard. A code can be converted to its family with {@link #of(int)}.
23  */
24 @SdkProtectedApi
25 public enum HttpStatusFamily {
26     /**
27      * 1xx response family.
28      */
29     INFORMATIONAL,
30 
31     /**
32      * 2xx response family.
33      */
34     SUCCESSFUL,
35 
36     /**
37      * 3xx response family.
38      */
39     REDIRECTION,
40 
41     /**
42      * 4xx response family.
43      */
44     CLIENT_ERROR,
45 
46     /**
47      * 5xx response family.
48      */
49     SERVER_ERROR,
50 
51     /**
52      * The family for any status code outside of the range 100-599.
53      */
54     OTHER;
55 
56     /**
57      * Retrieve the HTTP status family for the given HTTP status code.
58      */
of(int httpStatusCode)59     public static HttpStatusFamily of(int httpStatusCode) {
60         switch (httpStatusCode / 100) {
61             case 1: return INFORMATIONAL;
62             case 2: return SUCCESSFUL;
63             case 3: return REDIRECTION;
64             case 4: return CLIENT_ERROR;
65             case 5: return SERVER_ERROR;
66             default: return OTHER;
67         }
68     }
69 
70     /**
71      * Determine whether this HTTP status family is in the list of provided families.
72      *
73      * @param families The list of families to check against this family.
74      * @return True if any of the families in the list match this one.
75      */
isOneOf(HttpStatusFamily... families)76     public boolean isOneOf(HttpStatusFamily... families) {
77         return families != null && Stream.of(families).anyMatch(family -> family == this);
78     }
79 }
80