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

sg.dex.starfish.impl.remote.RemoteBundle Maven / Gradle / Ivy

package sg.dex.starfish.impl.remote;

import sg.dex.starfish.Asset;
import sg.dex.starfish.Bundle;
import sg.dex.starfish.impl.memory.MemoryBundle;
import sg.dex.starfish.util.JSON;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static sg.dex.starfish.constant.Constant.ASSET_ID;
import static sg.dex.starfish.constant.Constant.CONTENTS;


/**
 * Class representing an Bundle Asset managed via a remote agent.
 */
public class RemoteBundle extends ARemoteAsset implements Bundle {
    protected RemoteBundle(String meta, RemoteAgent remoteAgent) {
        super(meta, remoteAgent);
    }


    /**
     * Creates a RemoteAsset with the given metadata on the specified remote agent
     *
     * @param agent RemoteAgent on which to create the RemoteAsset
     * @param meta  Asset metadata which must be a valid JSON string
     * @return RemoteAsset
     */
    public static RemoteBundle create(RemoteAgent agent, String meta) {
        return new RemoteBundle(meta, agent);
    }

    /**
     * This method is to get the AssetMap
     *
     * @return assetMap that belong to bundle
     */
    private Map getAssetMap() {
        Map subAssetMap = new HashMap<>();

        @SuppressWarnings("unchecked")
        Map> subAssetIdMap =
                (Map>) JSON.toMap(metadataString).get(CONTENTS);

        for (String name : subAssetIdMap.keySet()) {
            Asset asset = agent.getAsset(subAssetIdMap.get(name).get(ASSET_ID));
            subAssetMap.put(name, asset);
        }
        return subAssetMap;
    }

    @Override
    public Bundle add(String name, Asset asset) {
        Map copyMap = getAssetMap();
        copyMap.put(name, asset);
        Bundle newBundle = MemoryBundle.create(copyMap, this.getMetadata());
        return agent.registerAsset(newBundle);
    }

    @Override
    public Bundle addAll(Map assetMapp) {
        Map copyMap = getAssetMap();
        copyMap.putAll(assetMapp);
        Bundle newBundle = MemoryBundle.create(copyMap, this.getMetadata());
        return agent.registerAsset(newBundle);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  R get(String name) {
        return (R) getAssetMap().get(name);
    }

    @Override
    public Map getAll() {
        return getAssetMap().entrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                        Map.Entry::getValue));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy