de.craftolution.craftoplugin4.modules.bot.result.Result.java Source code

Java tutorial

Introduction

Here is the source code for de.craftolution.craftoplugin4.modules.bot.result.Result.java

Source

/*
 * This file is part of CraftoPlugin, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2018 CraftolutionDE <https://craftolution.de>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Website: http://craftolution.de/
 * Contact: support@craftolution.de
 */
package de.craftolution.craftoplugin4.modules.bot.result;

import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import de.craftolution.craftoplugin4.modules.bot.packets.ResultPacket;
import de.craftolution.craftoplugin4.modules.bot.util.ResultEntry;

import com.google.common.collect.Lists;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * TODO: File description for Result.java
 *
 * @author Fear837, Pingebam
 * @since 16.04.2017
 * @see <a href="https://github.com/Craftolution">Craftolution on Github</a>
 */
public class Result {

    public static Result parse(ResultPacket resultPacket) {
        JsonObject content = resultPacket.getContent();

        // Stats
        JsonObject statsJson = content.getAsJsonObject("stats");
        long processTime = statsJson.get("process-time").getAsLong();
        int processedNodeCount = statsJson.get("processed-node-count").getAsInt();
        int dismissedNodeCount = statsJson.get("dismissed-node-count").getAsInt();

        // Response
        JsonObject responseJson = content.getAsJsonObject("response");
        String responseText = responseJson.get("text").getAsString().trim(); // TODO: Fix trailing whitespace

        // Entries
        JsonObject entries = content.getAsJsonObject("entries");
        List<ResultEntry> entryList = Lists.newArrayList();
        Set<Entry<String, JsonElement>> set = entries.entrySet();
        for (Entry<String, JsonElement> entry : set) {
            JsonObject obj = entry.getValue().getAsJsonObject();
            ResultEntry resultEntry = new ResultEntry();

            resultEntry.nodeId = entry.getKey();
            resultEntry.question = obj.get("node").getAsJsonObject().get("question").getAsString();
            resultEntry.probability = obj.get("probability").getAsFloat();

            entryList.add(resultEntry);
        }

        // Process chain
        //JsonObject processChain = new JsonObject();

        Result result = new Result(responseText, processTime, processedNodeCount, dismissedNodeCount, entryList);
        return result;
    }

    // -------------------------------------------------------------------

    private final String response;
    private final long processTime;
    private final int processedNodeCount;
    private final int dismissedNodeCount;
    private final List<ResultEntry> entryList;

    Result(String response, long processTime, int processedNodeCount, int dismissedNodeCount,
            List<ResultEntry> entryList) {
        this.response = response.trim(); // TODO: Fix trailing whitespace problem
        this.processTime = processTime;
        this.processedNodeCount = processedNodeCount;
        this.dismissedNodeCount = dismissedNodeCount;
        this.entryList = entryList;
    }

    /* TODO: Documentation */
    public String getResponse() {
        return this.response;
    }

    /** TODO: Documentation for getProcessTime() in Result.java */
    public long getProcessTime() {
        return processTime;
    }

    /** TODO: Documentation for getProcessedNodeCount() in Result.java */
    public int getProcessedNodeCount() {
        return processedNodeCount;
    }

    /** TODO: Documentation for getDismissedNodeCount() in Result.java */
    public int getDismissedNodeCount() {
        return dismissedNodeCount;
    }

    /** TODO: Documentation for getEntries() in Result.java */
    public List<ResultEntry> getEntries() {
        return entryList;
    }

}