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.widgets; 018 019import java.util.function.*; 020 021import org.apache.juneau.commons.svl.*; 022 023/** 024 * An immutable named server-side scalar value declaration held by a {@link ServerValues} bean. 025 * 026 * <p> 027 * Each value pairs a stable {@code name} (the key an author interpolates as <js>"$FV{name}"</js>) with a 028 * session-aware {@code provider}. The provider is invoked at serve time against a per-render 029 * {@link VarResolverSession} and must return a scalar ({@link String} / {@link Number} / {@link Boolean}) or 030 * <jk>null</jk>; non-scalar results are rejected at resolve time rather than {@code toString()}-ed. 031 * 032 * <p> 033 * {@code cacheable} defaults to <jk>false</jk> — the provider re-runs on every reference within a render. 034 * Set it <jk>true</jk> only for values that are stable across all references in a single response. 035 * 036 * @since 10.0.0 037 */ 038public final class ServerValuesValue { 039 040 /** The interpolation name (the {@code x} in <js>"$FV{x}"</js>). Must not be null or blank. */ 041 public final String name; 042 043 /** The session-aware scalar provider. Must not be null. */ 044 public final Function<VarResolverSession,?> provider; 045 046 /** Whether the resolved value may be memoized for the lifetime of a single render. Defaults to false. */ 047 public final boolean cacheable; 048 049 /** 050 * Constructor. 051 * 052 * @param name The interpolation name. 053 * @param provider The session-aware scalar provider. 054 * @param cacheable Whether the resolved value may be memoized within a single render. 055 */ 056 private ServerValuesValue(String name, Function<VarResolverSession,?> provider, boolean cacheable) { 057 this.name = name; 058 this.provider = provider; 059 this.cacheable = cacheable; 060 } 061 062 /** 063 * Creates a non-cacheable value. 064 * 065 * @param name The interpolation name. 066 * @param provider The session-aware scalar provider. 067 * @return A new {@link ServerValuesValue} with {@code cacheable=false}. 068 */ 069 public static ServerValuesValue of(String name, Function<VarResolverSession,?> provider) { 070 return new ServerValuesValue(name, provider, false); 071 } 072 073 /** 074 * Creates a value with an explicit cacheable flag. 075 * 076 * @param name The interpolation name. 077 * @param provider The session-aware scalar provider. 078 * @param cacheable Whether the resolved value may be memoized within a single render. 079 * @return A new {@link ServerValuesValue}. 080 */ 081 public static ServerValuesValue of(String name, Function<VarResolverSession,?> provider, boolean cacheable) { 082 return new ServerValuesValue(name, provider, cacheable); 083 } 084}