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

org.rapidgraphql.client.extractor.EnumValueExtractor Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package org.rapidgraphql.client.extractor;

import com.fasterxml.jackson.databind.ObjectMapper;
import kong.unirest.core.json.JSONArray;
import kong.unirest.core.json.JSONObject;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class EnumValueExtractor implements ValueExtractor {
    private Map enumConstants = null;

    public EnumValueExtractor(Class fieldClass) {
        if (fieldClass.isEnum()) {
            enumConstants = Arrays.stream(fieldClass.getEnumConstants())
                    .collect(Collectors.toMap(Object::toString, constant -> constant));
        }
    }

    @Override
    public Object extractNotNull(JSONObject data, String fieldName) {
        return Optional.ofNullable(data.getString(fieldName))
                .map(enumConstants::get)
                .orElse(null);
    }

    @Override
    public Object extractNotNull(JSONArray array, int index) {
        return Optional.ofNullable(array.getString(index))
                .map(enumConstants::get)
                .orElse(null);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy