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

es.moki.ratelimitj.redis.LimitRuleJsonSerialiser Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package es.moki.ratelimitj.redis;


import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
import es.moki.ratelimitj.core.api.LimitRule;

import java.util.OptionalInt;

class LimitRuleJsonSerialiser {

    String encode(Iterable rules) {
        JsonArray jsonArray = Json.array().asArray();
        rules.forEach(rule -> jsonArray.add(toJsonArray(rule)));
        return jsonArray.toString();
    }

    private JsonArray toJsonArray(LimitRule rule) {
        JsonArray array = Json.array().asArray().add(rule.getDurationSeconds()).add(rule.getLimit());

        OptionalInt precision = rule.getPrecision();
        if (precision.isPresent()) {
            array.add(precision.getAsInt());
        }
        return array;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy