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

de.otto.jsonhome.registry.controller.RegistryConverter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 Guido Steinacker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.otto.jsonhome.registry.controller;

import de.otto.jsonhome.registry.store.Link;
import de.otto.jsonhome.registry.store.Registry;

import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static de.otto.jsonhome.registry.controller.LinkConverter.jsonToLink;
import static de.otto.jsonhome.registry.controller.LinkConverter.linkToJson;
import static java.util.stream.Collectors.toList;

/**
 * A converter used to convert Registry and Link items into maps and vice versa.
 *
 * @author Guido Steinacker
 * @since 14.11.12
 */
public class RegistryConverter {

    private RegistryConverter() {}

    public static Registry jsonToRegistry(final Map map) {
        try {
            @SuppressWarnings("unchecked")
            final List> linksMap = (List>) map.get("service");
            return new Registry(
                    (String) map.get("name"),
                    (String) map.get("title"),
                    linksMap
                            .stream()
                            .map(LinkConverter::jsonToLink)
                            .collect(toList())
            );
        } catch (final NullPointerException e) {
            throw new IllegalArgumentException("Map does not contain valid links", e);
        }
    }

    public static Map registryToJson(final URI baseUri, final Registry registry) {
        final Map content = new LinkedHashMap<>();
        content.put("name", registry.getName());
        content.put("title", registry.getTitle());
        content.put("self", baseUri + "/registries/" + registry.getName());
        content.put("container", baseUri + "/registries");
        content.put("service", registry.getAll()
                .stream()
                .map(LinkConverter::linkToJson)
                .collect(toList()));
        return content;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy