001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.console.freemarker; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.util.*; 022 023import org.apache.juneau.marshall.collections.*; 024 025/** 026 * The built-in {@code thresholds} decorator: a horizontal mark line at a y value, on a carrier series 027 * {@code juneau-thresholds-<n>}. 028 * 029 * <h5 class='section'>Example:</h5> 030 * <p class='bjava'> 031 * Thresholds.<jsm>at</jsm>(500).label(<js>"SLO"</js>).tone(<js>"danger"</js>); 032 * </p> 033 * 034 * @since 10.0.0 035 */ 036public final class Thresholds implements Decorator { 037 038 private static final Set<String> TONES = Set.of("danger", "ok", "warning"); 039 040 private final Map<String,Object> value = new LinkedHashMap<>(); 041 042 private Thresholds() {} 043 044 /** @param y The y value. @return A new decorator. */ 045 public static Thresholds at(Number y) { 046 if (y == null) 047 throw iaex("Thresholds y must not be null."); 048 var x = new Thresholds(); 049 x.value.put("y", y); 050 return x; 051 } 052 053 /** @param v The line label. @return This object. */ 054 public Thresholds label(String v) { value.put("label", v); return this; } 055 056 /** @param v {@code danger}, {@code ok} or {@code warning}. @return This object. */ 057 public Thresholds tone(String v) { 058 if (! TONES.contains(v)) 059 throw iaex("Thresholds tone '%s' must be 'danger', 'ok' or 'warning'.", v); 060 value.put("tone", v); 061 return this; 062 } 063 064 @Override 065 public JsonMap toJson() { return JsonMap.of("use", "thresholds", "values", List.of(new LinkedHashMap<>(value))); } 066}