001/*
002 *   Copyright 2024 Vonage
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 */
016package com.vonage.client.insight;
017
018import com.fasterxml.jackson.annotation.JsonCreator;
019import com.fasterxml.jackson.annotation.JsonValue;
020import java.util.Arrays;
021
022/**
023 * Enum representing whether all information about a phone number has been returned.
024 * <p>
025 * {@code 0} is success,
026 * {@code 1} is a partial success (some fields populated),
027 * {@code 2} is failure.
028 */
029public enum LookupOutcome {
030        UNKNOWN(Integer.MAX_VALUE),
031        SUCCESS(0),
032        PARTIAL_SUCCESS(1),
033        FAILED(2);
034
035        private int code;
036
037        LookupOutcome(int code) {
038                this.code = code;
039        }
040
041        /**
042         * @return The code used to create this enum.
043         */
044        @JsonValue
045        public int getCode() {
046                return code;
047        }
048
049        @JsonCreator
050        public static LookupOutcome fromInt(Integer code) {
051                if (code == null) return null;
052                return Arrays.stream(LookupOutcome.values())
053                                .filter(lo -> lo.code == code)
054                                .findFirst().orElseGet(() -> {
055                                        LookupOutcome wildcard = UNKNOWN;
056                                        wildcard.code = code;
057                                        return wildcard;
058                                });
059        }
060}