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

com.algolia.model.usage.StatisticValue Maven / Gradle / Ivy

// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost
// - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.

package com.algolia.model.usage;

import com.algolia.exceptions.AlgoliaRuntimeException;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

/** StatisticValue */
@JsonDeserialize(using = StatisticValue.Deserializer.class)
public interface StatisticValue {
  // StatisticValue as Integer wrapper.
  static StatisticValue of(Integer value) {
    return new IntegerWrapper(value);
  }

  // StatisticValue as Map wrapper.
  static StatisticValue of(Map value) {
    return new MapOfStringIntegerWrapper(value);
  }

  // StatisticValue as Integer wrapper.
  @JsonSerialize(using = IntegerWrapper.Serializer.class)
  class IntegerWrapper implements StatisticValue {

    private final Integer value;

    IntegerWrapper(Integer value) {
      this.value = value;
    }

    public Integer getValue() {
      return value;
    }

    static class Serializer extends JsonSerializer {

      @Override
      public void serialize(IntegerWrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeObject(value.getValue());
      }
    }
  }

  // StatisticValue as Map wrapper.
  @JsonSerialize(using = MapOfStringIntegerWrapper.Serializer.class)
  class MapOfStringIntegerWrapper implements StatisticValue {

    private final Map value;

    MapOfStringIntegerWrapper(Map value) {
      this.value = value;
    }

    public Map getValue() {
      return value;
    }

    static class Serializer extends JsonSerializer {

      @Override
      public void serialize(MapOfStringIntegerWrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeObject(value.getValue());
      }
    }
  }

  class Deserializer extends JsonDeserializer {

    private static final Logger LOGGER = Logger.getLogger(Deserializer.class.getName());

    @Override
    public StatisticValue deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
      JsonNode tree = jp.readValueAsTree();
      // deserialize Integer
      if (tree.isInt()) {
        try (JsonParser parser = tree.traverse(jp.getCodec())) {
          Integer value = parser.readValueAs(Integer.class);
          return new StatisticValue.IntegerWrapper(value);
        } catch (Exception e) {
          // deserialization failed, continue
          LOGGER.finest("Failed to deserialize oneOf Integer (error: " + e.getMessage() + ") (type: Integer)");
        }
      }
      // deserialize Map
      if (tree.isObject()) {
        try (JsonParser parser = tree.traverse(jp.getCodec())) {
          Map value = parser.readValueAs(new TypeReference>() {});
          return new StatisticValue.MapOfStringIntegerWrapper(value);
        } catch (Exception e) {
          // deserialization failed, continue
          LOGGER.finest("Failed to deserialize oneOf Map (error: " + e.getMessage() + ") (type: Map)");
        }
      }
      throw new AlgoliaRuntimeException(String.format("Failed to deserialize json element: %s", tree));
    }

    /** Handle deserialization of the 'null' value. */
    @Override
    public StatisticValue getNullValue(DeserializationContext ctxt) throws JsonMappingException {
      throw new JsonMappingException(ctxt.getParser(), "StatisticValue cannot be null");
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy