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

com.jonnymatts.jzonbie.model.Cloner Maven / Gradle / Ivy

There is a newer version: 5.1.0
Show newest version
package com.jonnymatts.jzonbie.model;

import com.jonnymatts.jzonbie.model.content.*;

import java.util.HashMap;
import java.util.Map;

import static com.jonnymatts.jzonbie.model.content.ArrayBodyContent.arrayBody;
import static com.jonnymatts.jzonbie.model.content.LiteralBodyContent.literalBody;
import static com.jonnymatts.jzonbie.model.content.ObjectBodyContent.objectBody;
import static com.jonnymatts.jzonbie.model.content.StringBodyContent.stringBody;

class Cloner {

    static AppRequest cloneRequest(AppRequest appRequest) {
        final AppRequest copy = new AppRequest();
        copy.setPath(appRequest.getPath());
        copy.setMethod(appRequest.getMethod());
        copy.setQueryParams(copyMap(appRequest.getQueryParams()));
        copy.setHeaders(copyMap(appRequest.getHeaders()));
        copy.setBody(copyBodyContent(appRequest.getBody()));
        return copy;
    }

    static AppResponse cloneResponse(AppResponse appResponse) {
        final AppResponse copy = new AppResponse();
        copy.setStatusCode(appResponse.getStatusCode());
        copy.setHeaders(copyMap(appResponse.getHeaders()));
        copy.setBody(copyBodyContent(appResponse.getBody()));
        appResponse.getDelay().ifPresent(appResponse::setDelay);
        return copy;
    }

    private static  HashMap copyMap(Map map) {
        return map == null ? null : new HashMap<>(map);
    }

    private static BodyContent copyBodyContent(BodyContent bodyContent) {
        if(bodyContent == null) return null;
        if(bodyContent instanceof LiteralBodyContent) return literalBody(((LiteralBodyContent)bodyContent).getContent());
        if(bodyContent instanceof ArrayBodyContent) return arrayBody(((ArrayBodyContent)bodyContent).getContent());
        if(bodyContent instanceof StringBodyContent) return stringBody(((StringBodyContent)bodyContent).getContent());
        return objectBody(new HashMap<>(((ObjectBodyContent)bodyContent).getContent()));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy