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

com.exactpro.sf.common.impl.messages.json.configuration.Converters Maven / Gradle / Ivy

There is a newer version: 3.4.260
Show newest version
/*******************************************************************************
 * Copyright 2009-2019 Exactpro (Exactpro Systems Limited)
 *
 * 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 com.exactpro.sf.common.impl.messages.json.configuration;

import com.exactpro.sf.common.util.EPSCommonException;
import com.exactpro.sf.common.util.SingleKeyHashMap;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * Java class for different converters for different classes in JSON/YAML formats.
 */
public final class Converters {

    /**
     * 

Java class for convert {@link Map}<{@link String}, {@link JsonAttribute}> to {@link List} of {@link JsonAttribute} * *

Help to make following JSON/YAML structure to objects collection, and set names to objects *

     *     {
     *         ...
     *         "attributes":{
     *            "name1":{...},
     *            "name2":{...},
     *            ...
     *         }
     *         ...
     *     }
     * 
*/ public static class AttributesDeserializeConverter implements Converter, List> { @Override public List convert(Map value) { List list = new ArrayList<>(value.size()); for (Map.Entry pair : value.entrySet()) { if (StringUtils.isEmpty(pair.getKey())) { throw new EPSCommonException("Empty name for attribute/value"); } pair.getValue().setName(pair.getKey()); list.add(pair.getValue()); } return list; } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructMapType(SingleKeyHashMap.class, String.class, JsonAttribute.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructCollectionLikeType(LinkedList.class, JsonAttribute.class); } } /** *

Java class for convert {@link Map}<{@link String}, {@link JsonField}> to {@link List} of {@link JsonField} * *

Help to make following JSON/YAML structure to objects collection, and set names to objects *

     *     {
     *         ...
     *         "fields":{
     *            "name1":{...},
     *            "name2":{...},
     *            ...
     *         }
     *         ...
     *     }
     * 
*/ public static class FieldsDeserializeConverter implements Converter, List> { @Override public List convert(Map value) { List linkedList = new ArrayList<>(value.size()); for (Map.Entry pair : value.entrySet()) { if (pair.getKey().length() < 1) throw new EPSCommonException("Empty name for field"); pair.getValue().setName(pair.getKey()); linkedList.add(pair.getValue()); } return linkedList; } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructMapType(SingleKeyHashMap.class, String.class, JsonField.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructCollectionLikeType(LinkedList.class, JsonField.class); } } /** *

Java class for convert {@link Map}<{@link String}, {@link JsonMessage}> to {@link List} of {@link JsonMessage} * *

Help to make following JSON/YAML structure to objects collection, and set names to objects *

     *     {
     *         ...
     *         "messages":{
     *            "name1":{...},
     *            "name2":{...},
     *            ...
     *         }
     *         ...
     *     }
     * 
*/ public static class MessagesDeserializeConverter implements Converter, List> { @Override public List convert(Map value) { List linkedList = new ArrayList<>(value.size()); for (Map.Entry pair : value.entrySet()) { if (pair.getKey().length() < 1) throw new EPSCommonException("Empty name for message"); pair.getValue().setName(pair.getKey()); linkedList.add(pair.getValue()); } return linkedList; } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructMapType(SingleKeyHashMap.class, String.class, JsonMessage.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructCollectionLikeType(LinkedList.class, JsonMessage.class); } } /** *

Java class for convert {@link List} of {@link JsonAttribute} to {@link Map}<{@link String}, {@link JsonAttribute}>. * *

Help to make collection of the object to following JSON/YAML structure. *

     *     {
     *         ...
     *         "attributes":{
     *            "name1":{...},
     *            "name2":{...},
     *            ...
     *         }
     *         ...
     *     }
     * 
*

* or * *

     *     {
     *         ...
     *         "values":{
     *             "name1":{...},
     *             "name2":{...},
     *             ...
     *         }
     *         ...
     *     }
     * 
*/ public static class AttributesSerializeConverter implements Converter, Map> { @Override public Map convert(List value) { Map map = new LinkedHashMap<>(); for (JsonAttribute attribute : value) { map.put(attribute.getName(), attribute); } return map; } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructCollectionLikeType(List.class, JsonAttribute.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructMapType(HashMap.class, String.class, JsonAttribute.class); } } /** *

Java class for convert {@link List} of {@link JsonField} to {@link Map}<{@link String}, {@link JsonField}>. * *

Help to make collection of the object to following JSON/YAML structure. *

     *     {
     *         ...
     *         "fields":{
     *            "name1":{...},
     *            "name2":{...},
     *            ...
     *         }
     *         ...
     *     }
     * 
*/ public static class FieldsSerializeConverter implements Converter, Map> { @Override public Map convert(List value) { Map map = new LinkedHashMap<>(); for (JsonField field : value) { map.put(field.getName(), field); } return map; } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructCollectionLikeType(List.class, JsonField.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructMapType(HashMap.class, String.class, JsonField.class); } } /** *

Java class for convert {@link List} of {@link JsonMessage} to {@link Map}<{@link String}, {@link JsonMessage}>. * *

Help to make collection of the object to following JSON/YAML structure. *

     *     {
     *         ...
     *         "messages":{
     *            "name1":{...},
     *            "name2":{...},
     *            ...
     *         }
     *         ...
     *     }
     * 
*/ public static class MessagesSerializeConverter implements Converter, Map> { @Override public Map convert(List value) { Map map = new LinkedHashMap<>(); for (JsonMessage message : value) { map.put(message.getName(), message); } return map; } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructCollectionLikeType(List.class, JsonMessage.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructMapType(HashMap.class, String.class, JsonMessage.class); } } /** *

Java class for convert {@link String} to {@link com.exactpro.sf.common.impl.messages.xml.configuration.JavaType}. */ public static class JavaTypeDeserializeConverter implements Converter { @Override public com.exactpro.sf.common.impl.messages.xml.configuration.JavaType convert(String value) { return com.exactpro.sf.common.impl.messages.xml.configuration.JavaType.fromValue(value); } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructType(String.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructType(com.exactpro.sf.common.impl.messages.xml.configuration.JavaType.class); } } /** *

Java class for convert {@link com.exactpro.sf.common.impl.messages.xml.configuration.JavaType} to {@link String}. */ public static class JavaTypeSerializeConverter implements Converter { @Override public String convert(com.exactpro.sf.common.impl.messages.xml.configuration.JavaType value) { return value.value(); } @Override public JavaType getInputType(TypeFactory typeFactory) { return typeFactory.constructType(com.exactpro.sf.common.impl.messages.xml.configuration.JavaType.class); } @Override public JavaType getOutputType(TypeFactory typeFactory) { return typeFactory.constructType(String.class); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy