001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.annotations;
014
015import org.apache.tapestry5.ioc.annotations.UseWith;
016
017import java.lang.annotation.*;
018
019import static org.apache.tapestry5.ioc.annotations.AnnotationUseContext.*;
020
021/**
022 * Indicates that a method should only be evaluated once per request and the result cached.
023 * Further calls to the method during the same request will return the cached result.
024 * However, if the method's component occurs more than once within an enclosing component,
025 * the cached results will be distinct for each occurrence.
026 *
027 * This annotation is commonly used on getters for component properties:
028 * <pre>
029 * &#064;Cached
030 * Date getNow() {
031 *     new Date();
032 * }
033 * </pre>
034 * You may not apply &#064;Cached to void methods or methods with parameters.
035 *
036 * Note that this annotation is inheritance-safe; if a subclass calls a superclass method that
037 * has &#064;Cached then the value the subclass method gets is the cached value.
038 *
039 * The watch parameter can be passed a binding expression which will be evaluated each time the method is called. The
040 * method will then only be executed the first time it is called and after that only when the value of the binding
041 * changes. This can be used, for instance, to have the method only evaluated once per iteration of a loop by setting
042 * watch to the value or index of the loop.
043 */
044@Target(ElementType.METHOD)
045@Retention(RetentionPolicy.RUNTIME)
046@Documented
047@UseWith({COMPONENT,MIXIN,PAGE})
048public @interface Cached
049{
050    /**
051     * The optional binding to watch (default binding prefix is "prop").
052     */
053    String watch() default "";
054}