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

com.bandwidth.internal.OptionalNullable Maven / Gradle / Ivy

Go to download

The official client SDK for Bandwidth's Voice, Messaging, MFA, and WebRTC APIs

There is a newer version: 12.0.0
Show newest version
/*
 * BandwidthLib
 *
 * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
 */

package com.bandwidth.internal;

import com.bandwidth.DateTimeHelper;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * Class to encapsulate fields which are Optional as well as Nullable. It also
 * provides helper methods to create OptionalNullable generic type, and to
 * extract value from it.
 * @param  Type of the encapsulated field.
 */
public class OptionalNullable {

    /**
     * Private store for encapsulated object's value.
     */
    private T value;

    private OptionalNullable(T value) {
        this.value = value;
    }

    /**
     * Converts this OptionalNullable into string format.
     * @return String representation of this class
     */
    @Override
    public String toString() {
        return "" + value;
    }

    /**
     * Creates an OptionalNullable instance with the provided value.
     * @param    Type of the provided object.
     * @param value Value of the provided object.
     * @return {@link OptionalNullable} instance encapsulating given value.
     */
    public static  OptionalNullable of(T value) {
        return new OptionalNullable(value);
    }

    /**
     * Extracts the encapsulated value from the given OptionalNullable.
     * @param               Type of the expected value.
     * @param optionalNullable OptionalNullable instance to get value.
     * @return Value of the extracted field.
     */
    public static  T getFrom(OptionalNullable optionalNullable) {
        return (optionalNullable == null) ? null : optionalNullable.value;
    }

    /**
     * JsonSerializer for the {@link OptionalNullable} instance. It is used to
     * Serialize an {@link OptionalNullable} as its encapsulated object.
     */
    public static class Serializer extends JsonSerializer> {
        @Override
        public void serialize(OptionalNullable object, JsonGenerator jgen,
                SerializerProvider provider) throws IOException {
            jgen.writeObject(object.value);
        }
    }

    /**
     * A class to handle serialization of Unix Timestamps to DateTime objects.
     */
    public static class UnixTimestampSerializer extends JsonSerializer> {
        @SuppressWarnings("unchecked")
        @Override
        public void serialize(OptionalNullable object, JsonGenerator jgen,
                SerializerProvider provider) throws IOException {
            if (object.value instanceof List) {
                jgen.writeObject(
                        DateTimeHelper.toUnixTimestampLong((List) object.value));
            } else if (object.value instanceof Map) {
                jgen.writeObject(DateTimeHelper
                        .toUnixTimestampLong((Map) object.value));
            } else {
                jgen.writeObject(DateTimeHelper.toUnixTimestampLong((LocalDateTime) object.value));
            }
        }
    }

    /**
     * A class to handle serialization of Rfc1123 format strings to DateTime
     * objects.
     */
    public static class Rfc1123DateTimeSerializer extends JsonSerializer> {
        @SuppressWarnings("unchecked")
        @Override
        public void serialize(OptionalNullable object, JsonGenerator jgen,
                SerializerProvider provider) throws IOException {
            if (object.value instanceof List) {
                jgen.writeObject(
                        DateTimeHelper.toRfc1123DateTime((List) object.value));
            } else if (object.value instanceof Map) {
                jgen.writeObject(DateTimeHelper
                        .toRfc1123DateTime((Map) object.value));
            } else {
                jgen.writeString(DateTimeHelper.toRfc1123DateTime((LocalDateTime) object.value));
            }
        }
    }

    /**
     * A class to handle serialization of Rfc8601(Rfc3339) format strings to
     * DateTime objects.
     */
    public static class Rfc8601DateTimeSerializer extends JsonSerializer> {
        @SuppressWarnings("unchecked")
        @Override
        public void serialize(OptionalNullable object, JsonGenerator jgen,
                SerializerProvider provider) throws IOException {
            if (object.value instanceof List) {
                jgen.writeObject(
                        DateTimeHelper.toRfc8601DateTime((List) object.value));
            } else if (object.value instanceof Map) {
                jgen.writeObject(DateTimeHelper
                        .toRfc8601DateTime((Map) object.value));
            } else {
                jgen.writeString(DateTimeHelper.toRfc8601DateTime((LocalDateTime) object.value));
            }
        }
    }

    /**
     * A class to handle serialization of LocalDate objects to date strings.
     */
    public static class SimpleDateSerializer extends JsonSerializer> {
        @SuppressWarnings("unchecked")
        @Override
        public void serialize(OptionalNullable object, JsonGenerator jgen,
                SerializerProvider provider) throws IOException {
            if (object.value instanceof List) {
                jgen.writeObject(DateTimeHelper.toSimpleDate((List) object.value));
            } else if (object.value instanceof Map) {
                jgen.writeObject(
                        DateTimeHelper.toSimpleDate((Map) object.value));
            } else {
                jgen.writeString(DateTimeHelper.toSimpleDate((LocalDate) object.value));
            }
        }
    }
}