001// Copyright 2008, 2010 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5;
016
017import java.util.Optional;
018
019/**
020 * A collection of parameters that may eventually be passed to an event handler method. Includes the
021 * ability to coerce
022 * or encode parameters as needed.
023 * 
024 * @see org.apache.tapestry5.commons.services.TypeCoercer
025 * @see org.apache.tapestry5.ValueEncoder
026 */
027public interface EventContext
028{
029    /**
030     * Returns the number of parameter values that can be extracted.
031     */
032    int getCount();
033
034    /**
035     * Returns {@code true} if this context contains no elements.
036     * @since 5.8.2
037     */
038    boolean isEmpty();
039
040    /**
041     * Extracts a parameter value and coerces or decodes it to the desired type.
042     * 
043     * @param desiredType
044     *            the type of value required
045     * @param index
046     *            identifies which parameter value to extract
047     * @return the value extracted and converted or coerced
048     * @throws RuntimeException
049     *             if the value can't be converted or the index is out of range
050     */
051    <T> T get(Class<T> desiredType, int index);
052
053    /**
054     * Tries to extract a parameter value and coerces or decodes it to the desired type.
055     * 
056     * @param desiredType
057     *            the type of value required
058     * @param index
059     *            identifies which parameter value to extract
060     * @return the value extracted and converted or coerced, wrapped in an Optional, or {@link Optional#empty()}
061     *         if index is out of bounds, or the value can't be converted.
062     * @since 5.8.2
063     */
064    <T> Optional<T> tryGet(Class<T> desiredType, int index);
065
066    /**
067     * Extracts the value of each context value as a string.
068     * 
069     * @return context values
070     * @since 5.2.0
071     */
072    String[] toStrings();
073}