content: /*
   * Adyen Checkout API
   *
   * The version of the OpenAPI document: 71
   *
   *
   * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
   * https://openapi-generator.tech
   * Do not edit the class manually.
   */
  
  package com.adyen.model.checkout;
  
  import com.fasterxml.jackson.core.JsonGenerator;
  import com.fasterxml.jackson.core.JsonParser;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.core.JsonToken;
  import com.fasterxml.jackson.databind.DeserializationContext;
  import com.fasterxml.jackson.databind.JsonMappingException;
  import com.fasterxml.jackson.databind.JsonNode;
  import com.fasterxml.jackson.databind.MapperFeature;
  import com.fasterxml.jackson.databind.SerializerProvider;
  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
  import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
  import com.fasterxml.jackson.databind.ser.std.StdSerializer;
  import jakarta.ws.rs.core.GenericType;
  import java.io.IOException;
  import java.util.*;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.HashSet;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  @JsonDeserialize(using = DonationPaymentMethod.DonationPaymentMethodDeserializer.class)
  @JsonSerialize(using = DonationPaymentMethod.DonationPaymentMethodSerializer.class)
  public class DonationPaymentMethod extends AbstractOpenApiSchema {
    private static final Logger log = Logger.getLogger(DonationPaymentMethod.class.getName());
  
    public static class DonationPaymentMethodSerializer extends StdSerializer<DonationPaymentMethod> {
      public DonationPaymentMethodSerializer(Class<DonationPaymentMethod> t) {
        super(t);
      }
  
      public DonationPaymentMethodSerializer() {
        this(null);
      }
  
      @Override
      public void serialize(
          DonationPaymentMethod value, JsonGenerator jgen, SerializerProvider provider)
          throws IOException, JsonProcessingException {
        jgen.writeObject(value.getActualInstance());
      }
    }
  
    public static class DonationPaymentMethodDeserializer
        extends StdDeserializer<DonationPaymentMethod> {
      public DonationPaymentMethodDeserializer() {
        this(DonationPaymentMethod.class);
      }
  
      public DonationPaymentMethodDeserializer(Class<?> vc) {
        super(vc);
      }
  
      @Override
      public DonationPaymentMethod deserialize(JsonParser jp, DeserializationContext ctxt)
          throws IOException, JsonProcessingException {
        JsonNode tree = jp.readValueAsTree();
        Object deserialized = null;
        boolean typeCoercion = ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS);
        int match = 0;
        JsonToken token = tree.traverse(jp.getCodec()).nextToken();
        // deserialize ApplePayDonations
        try {
          boolean attemptParsing = true;
          if (attemptParsing) {
            // Checks if the unique type of the oneOf json matches any of the object TypeEnum values
            boolean typeMatch = false;
            if (tree.findValue("type") != null) {
              typeMatch =
                  Arrays.stream(ApplePayDonations.TypeEnum.values())
                      .anyMatch((t) -> t.getValue().equals(tree.findValue("type").asText()));
            }
  
            if (typeMatch) {
              deserialized = tree.traverse(jp.getCodec()).readValueAs(ApplePayDonations.class);
              // TODO: there is no validation against JSON schema constraints
              // (min, max, enum, pattern...), this does not perform a strict JSON
              // validation, which means the 'match' count may be higher than it should be.
              match++;
              log.log(Level.FINER, "Input data matches schema 'ApplePayDonations'");
            }
          }
        } catch (Exception e) {
          // deserialization failed, continue
          log.log(Level.FINER, "Input data does not match schema 'ApplePayDonations'", e);
        }
  
        // deserialize CardDonations
        try {
          boolean attemptParsing = true;
          if (attemptParsing) {
            // Checks if the unique type of the oneOf json matches any of the object TypeEnum values
            boolean typeMatch = false;
            if (tree.findValue("type") != null) {
              typeMatch =
                  Arrays.stream(CardDonations.TypeEnum.values())
                      .anyMatch((t) -> t.getValue().equals(tree.findValue("type").asText()));
            }
  
            if (typeMatch) {
              deserialized = tree.traverse(jp.getCodec()).readValueAs(CardDonations.class);
              // TODO: there is no validation against JSON schema constraints
              // (min, max, enum, pattern...), this does not perform a strict JSON
              // validation, which means the 'match' count may be higher than it should be.
              match++;
              log.log(Level.FINER, "Input data matches schema 'CardDonations'");
            }
          }
        } catch (Exception e) {
          // deserialization failed, continue
          log.log(Level.FINER, "Input data does not match schema 'CardDonations'", e);
        }
  
        // deserialize GooglePayDonations
        try {
          boolean attemptParsing = true;
          if (attemptParsing) {
            // Checks if the unique type of the oneOf json matches any of the object TypeEnum values
            boolean typeMatch = false;
            if (tree.findValue("type") != null) {
              typeMatch =
                  Arrays.stream(GooglePayDonations.TypeEnum.values())
                      .anyMatch((t) -> t.getValue().equals(tree.findValue("type").asText()));
            }
  
            if (typeMatch) {
              deserialized = tree.traverse(jp.getCodec()).readValueAs(GooglePayDonations.class);
              // TODO: there is no validation against JSON schema constraints
              // (min, max, enum, pattern...), this does not perform a strict JSON
              // validation, which means the 'match' count may be higher than it should be.
              match++;
              log.log(Level.FINER, "Input data matches schema 'GooglePayDonations'");
            }
          }
        } catch (Exception e) {
          // deserialization failed, continue
          log.log(Level.FINER, "Input data does not match schema 'GooglePayDonations'", e);
        }
  
        // deserialize IdealDonations
        try {
          boolean attemptParsing = true;
          if (attemptParsing) {
            // Checks if the unique type of the oneOf json matches any of the object TypeEnum values
            boolean typeMatch = false;
            if (tree.findValue("type") != null) {
              typeMatch =
                  Arrays.stream(IdealDonations.TypeEnum.values())
                      .anyMatch((t) -> t.getValue().equals(tree.findValue("type").asText()));
            }
  
            if (typeMatch) {
              deserialized = tree.traverse(jp.getCodec()).readValueAs(IdealDonations.class);
              // TODO: there is no validation against JSON schema constraints
              // (min, max, enum, pattern...), this does not perform a strict JSON
              // validation, which means the 'match' count may be higher than it should be.
              match++;
              log.log(Level.FINER, "Input data matches schema 'IdealDonations'");
            }
          }
        } catch (Exception e) {
          // deserialization failed, continue
          log.log(Level.FINER, "Input data does not match schema 'IdealDonations'", e);
        }
  
        // deserialize PayWithGoogleDonations
        try {
          boolean attemptParsing = true;
          if (attemptParsing) {
            // Checks if the unique type of the oneOf json matches any of the object TypeEnum values
            boolean typeMatch = false;
            if (tree.findValue("type") != null) {
              typeMatch =
                  Arrays.stream(PayWithGoogleDonations.TypeEnum.values())
                      .anyMatch((t) -> t.getValue().equals(tree.findValue("type").asText()));
            }
  
            if (typeMatch) {
              deserialized = tree.traverse(jp.getCodec()).readValueAs(PayWithGoogleDonations.class);
              // TODO: there is no validation against JSON schema constraints
              // (min, max, enum, pattern...), this does not perform a strict JSON
              // validation, which means the 'match' count may be higher than it should be.
              match++;
              log.log(Level.FINER, "Input data matches schema 'PayWithGoogleDonations'");
            }
          }
        } catch (Exception e) {
          // deserialization failed, continue
          log.log(Level.FINER, "Input data does not match schema 'PayWithGoogleDonations'", e);
        }
  
        if (match == 1) {
          DonationPaymentMethod ret = new DonationPaymentMethod();
          ret.setActualInstance(deserialized);
          return ret;
        }
        throw new IOException(
            String.format(
                "Failed deserialization for DonationPaymentMethod: %d classes match result, expected 1",
                match));
      }
  
      /** Handle deserialization of the 'null' value. */
      @Override
      public DonationPaymentMethod getNullValue(DeserializationContext ctxt)
          throws JsonMappingException {
        throw new JsonMappingException(ctxt.getParser(), "DonationPaymentMethod cannot be null");
      }
    }
  
    // store a list of schema names defined in oneOf
    public static final Map<String, GenericType<?>> schemas = new HashMap<>();
  
    public DonationPaymentMethod() {
      super("oneOf", Boolean.FALSE);
    }
  
    public DonationPaymentMethod(ApplePayDonations o) {
      super("oneOf", Boolean.FALSE);
      setActualInstance(o);
    }
  
    public DonationPaymentMethod(CardDonations o) {
      super("oneOf", Boolean.FALSE);
      setActualInstance(o);
    }
  
    public DonationPaymentMethod(GooglePayDonations o) {
      super("oneOf", Boolean.FALSE);
      setActualInstance(o);
    }
  
    public DonationPaymentMethod(IdealDonations o) {
      super("oneOf", Boolean.FALSE);
      setActualInstance(o);
    }
  
    public DonationPaymentMethod(PayWithGoogleDonations o) {
      super("oneOf", Boolean.FALSE);
      setActualInstance(o);
    }
  
    static {
      schemas.put("ApplePayDonations", new GenericType<ApplePayDonations>() {});
      schemas.put("CardDonations", new GenericType<CardDonations>() {});
      schemas.put("GooglePayDonations", new GenericType<GooglePayDonations>() {});
      schemas.put("IdealDonations", new GenericType<IdealDonations>() {});
      schemas.put("PayWithGoogleDonations", new GenericType<PayWithGoogleDonations>() {});
      JSON.registerDescendants(DonationPaymentMethod.class, Collections.unmodifiableMap(schemas));
    }
  
    @Override
    public Map<String, GenericType<?>> getSchemas() {
      return DonationPaymentMethod.schemas;
    }
  
    /**
     * Set the instance that matches the oneOf child schema, check the instance parameter is valid
     * against the oneOf child schemas: ApplePayDonations, CardDonations, GooglePayDonations,
     * IdealDonations, PayWithGoogleDonations
     *
     * <p>It could be an instance of the 'oneOf' schemas. The oneOf child schemas may themselves be a
     * composed schema (allOf, anyOf, oneOf).
     */
    @Override
    public void setActualInstance(Object instance) {
      if (JSON.isInstanceOf(ApplePayDonations.class, instance, new HashSet<>())) {
        super.setActualInstance(instance);
        return;
      }
  
      if (JSON.isInstanceOf(CardDonations.class, instance, new HashSet<>())) {
        super.setActualInstance(instance);
        return;
      }
  
      if (JSON.isInstanceOf(GooglePayDonations.class, instance, new HashSet<>())) {
        super.setActualInstance(instance);
        return;
      }
  
      if (JSON.isInstanceOf(IdealDonations.class, instance, new HashSet<>())) {
        super.setActualInstance(instance);
        return;
      }
  
      if (JSON.isInstanceOf(PayWithGoogleDonations.class, instance, new HashSet<>())) {
        super.setActualInstance(instance);
        return;
      }
  
      throw new RuntimeException(
          "Invalid instance type. Must be ApplePayDonations, CardDonations, GooglePayDonations, IdealDonations, PayWithGoogleDonations");
    }
  
    /**
     * Get the actual instance, which can be the following: ApplePayDonations, CardDonations,
     * GooglePayDonations, IdealDonations, PayWithGoogleDonations
     *
     * @return The actual instance (ApplePayDonations, CardDonations, GooglePayDonations,
     *     IdealDonations, PayWithGoogleDonations)
     */
    @Override
    public Object getActualInstance() {
      return super.getActualInstance();
    }
  
    /**
     * Get the actual instance of `ApplePayDonations`. If the actual instance is not
     * `ApplePayDonations`, the ClassCastException will be thrown.
     *
     * @return The actual instance of `ApplePayDonations`
     * @throws ClassCastException if the instance is not `ApplePayDonations`
     */
    public ApplePayDonations getApplePayDonations() throws ClassCastException {
      return (ApplePayDonations) super.getActualInstance();
    }
  
    /**
     * Get the actual instance of `CardDonations`. If the actual instance is not `CardDonations`, the
     * ClassCastException will be thrown.
     *
     * @return The actual instance of `CardDonations`
     * @throws ClassCastException if the instance is not `CardDonations`
     */
    public CardDonations getCardDonations() throws ClassCastException {
      return (CardDonations) super.getActualInstance();
    }
  
    /**
     * Get the actual instance of `GooglePayDonations`. If the actual instance is not
     * `GooglePayDonations`, the ClassCastException will be thrown.
     *
     * @return The actual instance of `GooglePayDonations`
     * @throws ClassCastException if the instance is not `GooglePayDonations`
     */
    public GooglePayDonations getGooglePayDonations() throws ClassCastException {
      return (GooglePayDonations) super.getActualInstance();
    }
  
    /**
     * Get the actual instance of `IdealDonations`. If the actual instance is not `IdealDonations`,
     * the ClassCastException will be thrown.
     *
     * @return The actual instance of `IdealDonations`
     * @throws ClassCastException if the instance is not `IdealDonations`
     */
    public IdealDonations getIdealDonations() throws ClassCastException {
      return (IdealDonations) super.getActualInstance();
    }
  
    /**
     * Get the actual instance of `PayWithGoogleDonations`. If the actual instance is not
     * `PayWithGoogleDonations`, the ClassCastException will be thrown.
     *
     * @return The actual instance of `PayWithGoogleDonations`
     * @throws ClassCastException if the instance is not `PayWithGoogleDonations`
     */
    public PayWithGoogleDonations getPayWithGoogleDonations() throws ClassCastException {
      return (PayWithGoogleDonations) super.getActualInstance();
    }
  
    /**
     * Create an instance of DonationPaymentMethod given an JSON string
     *
     * @param jsonString JSON string
     * @return An instance of DonationPaymentMethod
     * @throws IOException if the JSON string is invalid with respect to DonationPaymentMethod
     */
    public static DonationPaymentMethod fromJson(String jsonString) throws IOException {
      return JSON.getMapper().readValue(jsonString, DonationPaymentMethod.class);
    }
  
    /**
     * Convert an instance of DonationPaymentMethod to an JSON string
     *
     * @return JSON string
     */
    public String toJson() throws JsonProcessingException {
      return JSON.getMapper().writeValueAsString(this);
    }
  }
  