001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.util.function.context;
031
032import org.openimaj.util.data.Context;
033import org.openimaj.util.function.Function;
034
035/**
036 * An adaptor that allows a {@link Function} to be applied to a single element
037 * of a {@link Context}, and store its output in a (potentially) different slot
038 * of the {@link Context}.
039 * 
040 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
041 * 
042 * @param <IN>
043 *            Type of input to the adapted function
044 * @param <OUT>
045 *            Type of output of the adapted function
046 */
047public class ContextFunctionAdaptor<IN, OUT> extends ContextAdaptor<Function<IN, OUT>, IN, OUT>
048                implements
049                Function<Context, Context>
050{
051
052        /**
053         * @param inner
054         * @param extract
055         * @param insert
056         */
057        public ContextFunctionAdaptor(Function<IN, OUT> inner, ContextExtractor<IN> extract,
058                        ContextInsertor<OUT> insert)
059        {
060                super(inner, extract, insert);
061        }
062
063        /**
064         * Construct {@link ContextFunctionAdaptor} that reads data from a given
065         * key, applies a function and sets the result in a different key.
066         * 
067         * @param extract
068         *            the key to pull data from
069         * @param insert
070         *            the key to write the result to
071         * @param inner
072         *            the function to apply
073         */
074        public ContextFunctionAdaptor(Function<IN, OUT> inner, String extract, String insert)
075        {
076                super(inner, extract, insert);
077        }
078
079        /**
080         * Construct {@link ContextFunctionAdaptor} that reads data from a given
081         * key, applies a function and sets the result with the same key.
082         * 
083         * @param both
084         *            the extract and insert key
085         * @param inner
086         *            the function to apply
087         */
088        public ContextFunctionAdaptor(String both, Function<IN, OUT> inner)
089        {
090                super(inner, both, both);
091        }
092
093        @Override
094        public Context apply(Context in) {
095                final OUT obj = inner.apply(extract.extract(in));
096
097                insert.insert(obj, in);
098
099                return in;
100        }
101
102        /**
103         * Create a new {@link ContextFunctionAdaptor} that reads data from a given
104         * key, applies a function and sets the result in a different key.
105         * 
106         * @param extract
107         *            the key to pull data from
108         * @param insert
109         *            the key to write the result to
110         * @param inner
111         *            the function to apply
112         * @return the new {@link ContextFunctionAdaptor}
113         */
114        public static <IN, OUT> Function<Context, Context> create(String extract, String insert, Function<IN, OUT> inner) {
115                return new ContextFunctionAdaptor<IN, OUT>(inner, extract, insert);
116        }
117
118}