001    /*
002     * Copyright 2003-2008 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     *
016     * You are receiving this code free of charge, which represents many hours of
017     * effort from other individuals and corporations.  As a responsible member 
018     * of the community, you are asked (but not required) to donate any 
019     * enhancements or improvements back to the community under a similar open 
020     * source license.  Thank you. -TMN
021     */
022    package groovyx.net.http;
023    
024    /**
025     * Mapping of HTTP response codes to a constant 'success' or 'failure' value.
026     * @author tnichols
027     */
028    public enum Status {
029            SUCCESS ( 100, 399 ),           
030            FAILURE ( 400, 999 );
031    
032            private final int min, max;
033            
034            @Override public String toString() {
035                    return super.toString().toLowerCase();
036            }
037            
038            public boolean matches( int code ) {
039                    return min <= code && code <= max;
040            }
041            
042            /**
043             * Find the Status value that matches the given status code. 
044             * @param code HTTP response code
045             * @return a 'success' or 'failure' Status value
046             */
047            public static Status find( int code ) {
048                    for ( Status s : Status.values() ) 
049                            if ( s.matches( code ) ) return s;
050                    throw new IllegalArgumentException( "Unknown status: " + code );
051            }
052            
053            private Status( int min, int max ) {
054                    this.min = min; this.max = max;
055            }
056    }