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.terminal.region;
018
019import java.util.*;
020
021/**
022 * The app's single answer to "who may watch" and "who may type".
023 *
024 * <p>
025 * As a lambda it answers the first question only: typing stays denied unless {@link #inputSink(TerminalCaller, String)}
026 * is overridden.
027 */
028@FunctionalInterface
029public interface TerminalAccess {
030
031   /**
032    * Whether the caller may watch the terminal.
033    *
034    * <p>
035    * A typist must also be able to watch.
036    *
037    * @param caller Who is asking.
038    * @param terminalId The terminal id.
039    * @return <jk>true</jk> if the caller may watch it.
040    */
041   boolean canWatch(TerminalCaller caller, String terminalId);
042
043   /**
044    * The sink the caller may type into.
045    *
046    * <p>
047    * Input also requires {@link #canWatch(TerminalCaller, String)}: a sink returned for a caller who may not watch is
048    * still denied.
049    *
050    * @param caller Who is asking.
051    * @param terminalId The terminal id.
052    * @return The sink, or empty when typing is denied.  Empty by default.
053    */
054   default Optional<TerminalSink> inputSink(TerminalCaller caller, String terminalId) {
055      return Optional.empty();
056   }
057}