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

io.apimatic.core.utilities.DateHelper Maven / Gradle / Ivy

Go to download

A library that contains core logic and utilities for consuming REST APIs using Java SDKs generated by APIMatic.

There is a newer version: 0.6.5
Show newest version
package io.apimatic.core.utilities;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * This is a utility class for Date operations.
 */
public class DateHelper {

    protected DateHelper() {}

    /**
     * Parse a simple date string to a LocalDate object.
     * @param date The date string.
     * @return The parsed LocalDate object.
     */
    public static LocalDate fromSimpleDate(String date) {
        return LocalDate.parse(date);
    }

    /**
     * Convert a LocalDate object to a string.
     * @param value The LocalDate object to convert.
     * @return The converted Strings.
     */
    public static String toSimpleDate(LocalDate value) {
        return value == null ? null : value.toString();
    }

    /**
     * Convert a List of LocalDate objects to strings.
     * @param values The List of LocalDate objects to convert.
     * @return The List of converted Strings.
     */
    public static List toSimpleDate(List values) {
        if (values == null) {
            return null;
        }
        List valuesAsString = new ArrayList<>();
        for (LocalDate value : values) {
            valuesAsString.add(toSimpleDate(value));
        }
        return valuesAsString;
    }

    /**
     * Convert a Map of LocalDate objects to strings.
     * @param values The Map of LocalDate objects to convert.
     * @return The Map of converted Strings.
     */
    public static Map toSimpleDate(Map values) {
        if (values == null) {
            return null;
        }
        Map valuesAsString = new HashMap<>();
        for (Map.Entry value : values.entrySet()) {
            valuesAsString.put(value.getKey(), toSimpleDate(value.getValue()));
        }
        return valuesAsString;
    }

    /**
     * Convert a List of Map of LocalDate objects to strings.
     * @param values The List of Map of LocalDate objects to convert.
     * @return The list of map of converted Strings.
     */
    public static List> toArrayOfMapOfSimpleDate(
            List> values) {
        if (values == null) {
            return null;
        }
        List> valuesAsString = new ArrayList<>();
        for (Map value : values) {
            valuesAsString.add(toSimpleDate(value));
        }
        return valuesAsString;
    }

    /**
     * A class to handle deserialization of date strings to LocalDate objects.
     */
    public static class SimpleDateDeserializer extends JsonDeserializer {
        @SuppressWarnings("unused")
        @Override
        public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            return fromSimpleDate(jp.getValueAsString());
        }
    }

    /**
     * A class to handle serialization of LocalDate objects to date strings.
     */
    public static class SimpleDateSerializer extends JsonSerializer {
        @SuppressWarnings("unused")
        @Override
        public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            jgen.writeString(toSimpleDate(value));
        }
    }

    /**
     * Simple Adapter utility class.
     */
    public static class SimpleAdapter extends XmlAdapter {
        @Override
        public String marshal(LocalDate date) {
            return date.toString();
        }

        @Override
        public LocalDate unmarshal(String date) {
            return LocalDate.parse(date);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy