001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2015 Richard "Shred" Körber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j;
015
016import java.util.Arrays;
017
018/**
019 * Status codes of challenges and authorizations.
020 */
021public enum Status {
022
023    PENDING, PROCESSING, VALID, INVALID, REVOKED, DEACTIVATED, GOOD, UNKNOWN;
024
025    /**
026     * Parses the string and returns a corresponding Status object.
027     *
028     * @param str
029     *            String to parse
030     * @return {@link Status} matching the string, or {@link Status#UNKNOWN} if there was
031     *         no match
032     */
033    public static Status parse(String str) {
034        String check = str.toUpperCase();
035        return Arrays.stream(values())
036                .filter(s -> s.name().equals(check))
037                .findFirst()
038                .orElse(Status.UNKNOWN);
039    }
040
041    /**
042     * Parses the string and returns a corresponding Status object.
043     *
044     * @param str
045     *            String to parse
046     * @param def
047     *            Default Status if str is {@code null}
048     * @return {@link Status} matching the string, or {@link Status#UNKNOWN} if there was
049     *         no match, or {@code def} if the str was {@code null}
050     */
051    public static Status parse(String str, Status def) {
052        return str != null ? parse(str) : def;
053    }
054
055}