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

org.graylog2.contentpacks.model.entities.WidgetConfig Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * 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
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.contentpacks.model.entities;

import org.graylog2.contentpacks.exceptions.ContentPackException;
import org.graylog2.contentpacks.model.entities.references.ReferenceMap;
import org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils;
import org.graylog2.contentpacks.model.entities.references.ValueReference;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class WidgetConfig {
    private Map config;

    public WidgetConfig(ReferenceMap config, Map parameters) {
        this.config = ReferenceMapUtils.toValueMap(config, parameters);
    }

    public Optional getOptionalString(String key) {
        Object value = config.get(key);
        if (value instanceof String) {
            return Optional.of((String) value);
        }
        return Optional.empty();
    }

    public String getString(String key) throws ContentPackException {
        Object value = config.get(key);
        if (value instanceof String) {
            return (String) value;
        }
        throw new ContentPackException("Could not find key " + key + " in config.");
    }

    public boolean getBoolean(String key) throws ContentPackException {
        Object value = config.get(key);
        if (value != null) {
            return (boolean) value;
        }
        throw new ContentPackException("Could not find key " + key + " in config.");
    }

    public Optional getOptionalBoolean(String key) {
        final Boolean value = (Boolean)config.get(key);
        return Optional.ofNullable(value);
    }

    public int getInteger(String key) {
        Object value = config.get(key);
        if (value != null) {
            return (int) value;
        }
        throw new ContentPackException("Could not find key " + key + " in config.");
    }

    public Optional getOptionalInteger(String key) {
        Object value = config.get(key);
        if (value instanceof Integer) {
            return Optional.of((Integer) value);
        }
        return Optional.empty();
    }

    public List getList(String key) {
        Object value = config.get(key);
        if (value instanceof List) {
            return (List) value;
        }
        return Collections.emptyList();
    }
}