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 */
017
018package org.apache.commons.proxy2.interceptor;
019
020import org.apache.commons.proxy2.Interceptor;
021import org.apache.commons.proxy2.Invoker;
022import org.apache.commons.proxy2.ObjectProvider;
023import org.apache.commons.proxy2.provider.ObjectProviderUtils;
024
025public final class InterceptorUtils
026{
027    //******************************************************************************************************************
028    // Static Methods
029    //******************************************************************************************************************
030
031    /**
032     * Creates an {@link Interceptor} which always returns a constant value (for all methods).
033     * 
034     * @param value
035     *            the constant
036     * @return an {@link Interceptor} which always returns a constant value (for all methods)
037     */
038    public static Interceptor constant(Object value)
039    {
040        return new ObjectProviderInterceptor(ObjectProviderUtils.constant(value));
041    }
042
043    /**
044     * Creates an {@link Interceptor} which returns the resulting object from an object provider (for all methods).
045     * 
046     * @param provider
047     *            the object provider
048     * @return an {@link Interceptor} which returns the resulting object from an object provider (for all methods)
049     */
050    public static Interceptor provider(ObjectProvider<?> provider)
051    {
052        return new ObjectProviderInterceptor(provider);
053    }
054
055    /**
056     * Creates an {@link Interceptor} which throws a specific exception (for all methods).
057     * 
058     * @param e
059     *            the exception
060     * @return an {@link Interceptor} which throws a specific exception (for all methods)
061     */
062    public static Interceptor throwing(Exception e)
063    {
064        return new ThrowingInterceptor(ObjectProviderUtils.constant(e));
065    }
066
067    /**
068     * Creates an {@link Interceptor} which throws the exception provided by an object provider (for all methods).
069     * 
070     * @param provider
071     *            the object provider
072     * @return an {@link Interceptor} which throws the exception provided by an object provider (for all methods)
073     */
074    public static Interceptor throwing(ObjectProvider<? extends Exception> provider)
075    {
076        return new ThrowingInterceptor(provider);
077    }
078
079    /**
080     * Creates an {@link Interceptor} that delegates to the specified {@link Invoker}.
081     * 
082     * @param invoker
083     *            delegate
084     * @return invoker {@link Interceptor}
085     */
086    public static Interceptor invoking(Invoker invoker)
087    {
088        return new InvokerInterceptor(invoker);
089    }
090
091    //******************************************************************************************************************
092    // Constructors
093    //******************************************************************************************************************
094
095    private InterceptorUtils()
096    {
097    }
098}