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

com.arm.mbed.cloud.sdk.subscribe.adapters.AsynchronousResponseNotificationAdapter Maven / Gradle / Ivy

package com.arm.mbed.cloud.sdk.subscribe.adapters;

import java.util.List;

import com.arm.mbed.cloud.sdk.annotations.Internal;
import com.arm.mbed.cloud.sdk.annotations.Preamble;
import com.arm.mbed.cloud.sdk.common.GenericAdapter;
import com.arm.mbed.cloud.sdk.common.GenericAdapter.Mapper;
import com.arm.mbed.cloud.sdk.common.TranslationUtils;
import com.arm.mbed.cloud.sdk.connect.model.Resource;
import com.arm.mbed.cloud.sdk.internal.mds.model.AsyncIDResponse;
import com.arm.mbed.cloud.sdk.internal.mds.model.NotificationMessage;
import com.arm.mbed.cloud.sdk.subscribe.model.AsynchronousResponseNotification;
import com.arm.mbed.cloud.sdk.subscribe.model.ResourceValueNotification;

@Preamble(description = "Adapter for asynchronous response notifications")
@Internal
public final class AsynchronousResponseNotificationAdapter {

    private AsynchronousResponseNotificationAdapter() {
        super();
    }

    /**
     * Maps an asynchronous response to a resource value notification.
     *
     * @param correspondingResource
     *            corresponding resource.
     * @param asyncResponse
     *            asynchronous response
     * @return a resource value notification.
     */
    public static ResourceValueNotification
           mapToResourceValueNotification(Resource correspondingResource,
                                          AsynchronousResponseNotification asyncResponse) {
        if (asyncResponse == null) {
            return null;
        }
        final ResourceValueNotification notification = new ResourceValueNotification(correspondingResource);
        notification.setObject(asyncResponse);
        return notification;
    }

    /**
     * Maps an asynchronous response notification.
     *
     * @param asyncResponse
     *            async response.
     * @return an asynchronous response notification.
     */
    public static AsynchronousResponseNotification map(AsyncIDResponse asyncResponse) {
        if (asyncResponse == null) {
            return null;
        }
        final AsynchronousResponseNotification notification = new AsynchronousResponseNotification(asyncResponse.getId(),
                                                                                                   TranslationUtils.toInt(asyncResponse.getStatus(),
                                                                                                                          200));
        notification.setErrorMessage(asyncResponse.getError());
        notification.decodePayload(asyncResponse.getPayload(), asyncResponse.getCt());
        return notification;
    }

    /**
     * Gets a mapper.
     *
     * @return a mapper
     */
    public static Mapper getMapper() {
        return new Mapper() {

            @Override
            public AsynchronousResponseNotification map(AsyncIDResponse toBeMapped) {
                return AsynchronousResponseNotificationAdapter.map(toBeMapped);
            }
        };
    }

    /**
     * Maps a list of async responses.
     *
     * @param list
     *            list of async responses.
     * @return list of asynchronous response notification.
     */
    public static List mapList(List list) {
        return GenericAdapter.mapList(list, AsynchronousResponseNotificationAdapter.getMapper());
    }

    /**
     * Gets list mapper.
     *
     *
     * @return a list mapper.
     */
    public static Mapper, List> getListMapper() {

        return new Mapper, List>() {

            @Override
            public List map(List toBeMapped) {
                return AsynchronousResponseNotificationAdapter.mapList(toBeMapped);
            }

        };
    }

    /**
     * Maps asynchronous response notifications.
     *
     * @param notificationMessage
     *            notification message.
     * @return list of asynchronous response notification.
     */
    public static List
           mapNotificationMessage(NotificationMessage notificationMessage) {
        if (notificationMessage == null) {
            return null;
        }
        return AsynchronousResponseNotificationAdapter.mapList(notificationMessage.getAsyncResponses());
    }

    /**
     * Gets notification message mapper.
     *
     * @return a list mapper.
     */
    public static Mapper> getNotificationMessageMapper() {
        return new Mapper>() {

            @Override
            public List map(NotificationMessage toBeMapped) {
                return AsynchronousResponseNotificationAdapter.mapNotificationMessage(toBeMapped);
            }

        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy