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.commons.flatfile.util;
018
019import java.util.Map;
020
021import net.sf.morph.lang.languages.SimpleLanguage;
022import net.sf.morph.reflect.Reflector;
023import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector;
024import net.sf.morph.transform.Transformer;
025import net.sf.morph.transform.transformers.SimpleDelegatingTransformer;
026
027import org.apache.commons.flatfile.morph.EntityCollectionReflector;
028import org.apache.commons.flatfile.morph.FieldOptionConstantConverter;
029import org.apache.commons.flatfile.morph.TextToByteConverter;
030
031/**
032 * Utilities for applying options.
033 * @version $Revision: 1301244 $ $Date: 2012-03-15 17:16:23 -0500 (Thu, 15 Mar 2012) $
034 */
035public abstract class ApplyOptions {
036
037    private static final SimpleLanguage MORPH_LANGUAGE;
038
039    static {
040        MORPH_LANGUAGE = new SimpleLanguage();
041        MORPH_LANGUAGE.setReflector(new SimpleDelegatingReflector(
042                new Reflector[] { new EntityCollectionReflector() }, true));
043        MORPH_LANGUAGE.setConverter(new SimpleDelegatingTransformer(
044                new Transformer[] { new TextToByteConverter(),
045                        new FieldOptionConstantConverter() }, true));
046    }
047
048    /**
049     * Apply the specified option map to <code>o</code>.
050     * @param <T> type of fluent arg/result.
051     * @param o Object target
052     * @param options option map
053     * @return <code>o</code>.
054     */
055    public static <T> T apply(T o, Map<String, ?> options) {
056        for (Map.Entry<String, ?> option : options.entrySet()) {
057            apply(o, option.getKey(), option.getValue());
058        }
059        return o;
060    }
061
062    /**
063     * Apply the specified option to <code>o</code>.
064     * @param <T> type of fluent arg/result.
065     * @param o Object target
066     * @param option String option name.
067     * @param value option value.
068     * @return <code>o</code>.
069     */
070    public static <T> T apply(T o, String option, Object value) {
071        MORPH_LANGUAGE.set(o, option, value);
072        return o;
073    }
074
075}