xyz.putzi.slackmc.common.messaging.builder.DefaultSlackFieldBuilder.java Source code

Java tutorial

Introduction

Here is the source code for xyz.putzi.slackmc.common.messaging.builder.DefaultSlackFieldBuilder.java

Source

/*
 * SlackMc Bukkit/Bungeecord Api - Bukkit/Bungeecord plugin for Slack
 *      Copyright (C) 2016  putzi_x  (http://putzi.xyz)
 *
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

package xyz.putzi.slackmc.common.messaging.builder;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import xyz.putzi.slackmc.common.messaging.model.SlackField;

import java.util.Objects;

public class DefaultSlackFieldBuilder implements SlackField {

    private String title;
    private String value;
    private Boolean fieldShort;

    private JsonArray jsonArray = new JsonArray();

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public void setShort(Boolean fieldShort) {
        this.fieldShort = fieldShort;
    }

    @Override
    public JsonArray getJsonArray(Boolean clearForNextField) {
        Objects.requireNonNull(jsonArray, "JsonArray");

        if (clearForNextField) {
            JsonArray arrayToReturn = new JsonArray();
            arrayToReturn.addAll(jsonArray);
            jsonArray = new JsonArray();
            return arrayToReturn;
        } else {
            return this.jsonArray;
        }
    }

    @Override
    public void addToJsonArray() {
        JsonObject object = new JsonObject();

        if (title != null)
            object.addProperty("title", title);
        if (value != null)
            object.addProperty("value", value);
        if (fieldShort != null)
            object.addProperty("short", fieldShort.toString());

        jsonArray.add(object);
    }
}