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

io.qubite.tomoko.gson.PatchFactory Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.qubite.tomoko.gson;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import io.qubite.tomoko.json.CommandType;
import io.qubite.tomoko.json.OperationDto;
import io.qubite.tomoko.json.Patch;
import io.qubite.tomoko.type.Types;
import io.qubite.tomoko.type.ValueType;

import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by edhendil on 03.09.16.
 */
public class PatchFactory {

    private static final ValueType> TYPE = Types.list(GsonOperationDto.class);

    private final NodeFactory nodeFactory;

    PatchFactory(NodeFactory nodeFactory) {
        this.nodeFactory = nodeFactory;
    }

    public static PatchFactory instance() {
        return new PatchFactory(new NodeFactory(new Gson()));
    }

    public static PatchFactory instance(Gson gson) {
        return new PatchFactory(new NodeFactory(gson));
    }

    public Patch parse(String json) {
        return Patch.of(nodeFactory.toTree(json).getAs(TYPE).stream().map(this::toOperationDto).collect(Collectors.toList()));
    }

    public Patch parse(InputStream inputStream) {
        return Patch.of(nodeFactory.toTree(inputStream).getAs(TYPE).stream().map(this::toOperationDto).collect(Collectors.toList()));
    }

    public Patch parse(JsonElement node) {
        return Patch.of(nodeFactory.toTree(node).getAs(TYPE).stream().map(this::toOperationDto).collect(Collectors.toList()));
    }

    private OperationDto toOperationDto(GsonOperationDto operationDto) {
        return OperationDto.of(operationDto.getPath(), CommandType.of(operationDto.getOp()), nodeFactory.toTree(operationDto.getValue()));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy