All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sdk.nio.http.common.HttpRequestMethod Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.sdk.nio.http.common;

import java.util.HashMap;
import java.util.Map;

public enum HttpRequestMethod {

    ALL(Integer.valueOf(0), "*"),
    GET(Integer.valueOf(1), "get"),
    HEAD(Integer.valueOf(2), "head"),
    POST(Integer.valueOf(3), "post"),
    PUT(Integer.valueOf(4), "put"),
    PATCH(Integer.valueOf(5), "patch"),
    DELETE(Integer.valueOf(6), "delete"),
    OPTIONS(Integer.valueOf(7), "options"),
    TRACE(Integer.valueOf(8), "trace"),
    GET_LARGE(Integer.valueOf(9), "get_large"),
    DELETE_LARGE(Integer.valueOf(10), "delete_large"),
    ;

    private final Integer id;
    private final String method;

    private HttpRequestMethod(Integer id, String method) {
        this.id = id;
        this.method = method;
        appendMapping(this);
    }

    public Integer getId() {
        return id;
    }

    public String getMethod() {
        return method;
    }

    private static void appendMapping(HttpRequestMethod httpRequestMethod) {
        appendNameMapping(httpRequestMethod);
        appendIdMapping(httpRequestMethod);
    }

    private static void appendNameMapping(HttpRequestMethod httpRequestMethod) {
        HttpRequestMethodHolder.NAME_MAPPINGS.put(httpRequestMethod.name(), httpRequestMethod);
    }

    private static void appendIdMapping(HttpRequestMethod httpRequestMethod) {
        HttpRequestMethodHolder.ID_MAPPINGS.put(httpRequestMethod.getId(), httpRequestMethod);
    }

    public static HttpRequestMethod resolve(String method) {
        return resolve(method, false);
    }

    public static HttpRequestMethod resolve(String method, boolean ingoreCase) {
        return method != null ? HttpRequestMethodHolder.NAME_MAPPINGS.get(ingoreCase ? method.toLowerCase() : method) : null;
    }

    public static HttpRequestMethod resolve(Integer id) {
        return HttpRequestMethodHolder.ID_MAPPINGS.get(id);
    }

    public static HttpRequestMethod of(String method) {
        try {
            return valueOf(method);
        } catch (Exception exception) {
            return null;
        }
    }

    public boolean matches(String method) {
        return (this == resolve(method));
    }

    private static class HttpRequestMethodHolder {
        private static final Map NAME_MAPPINGS = new HashMap<>();
        private static final Map ID_MAPPINGS = new HashMap<>();

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy