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    package org.apache.commons.betwixt.strategy;
018    
019    /**
020     * <p>Pluggable strategy determines whether introspection or bind time
021     * typing should be used when finding mappings.
022     * The type of a property is determined at introspection time but
023     * the actual type of the instance can differ at bind time (when the 
024     * xml is actually being processed). This strategy is used to set
025     * (at a per-element level) whether the bind or introspection
026     * time type should be used to calculate the mapping to be used.
027     * </p>
028     * <p>
029     * <strong>Note:</strong> this strategy is intentionally course.
030     * Typically, the best approach is to use a custom strategy to set
031     * coursely grained rules (for example: all implemetations of 'IAnimal'
032     * use bind time type mapping) and then dot betwixt files to provide
033     * refinements.
034     * </p>
035     * @since 0.7
036     * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, 
037     * <a href='http://www.apache.org'>Apache Software Foundation</a>
038     */
039    public abstract class MappingDerivationStrategy {
040    
041        /**
042         * Implementation that always uses bind time type mapping
043         */
044        public static final MappingDerivationStrategy USE_BIND_TIME_TYPE 
045                    = new MappingDerivationStrategy() {
046            public boolean useBindTimeTypeForMapping(Class propertyType, Class singluarPropertyType) {
047                return true;
048            }
049        };
050    
051        /**
052         * Implementation that always uses introspection time type mapping
053         */
054        public static final MappingDerivationStrategy USE_INTROSPECTION_TIME_TYPE 
055                    = new MappingDerivationStrategy() {
056            public boolean useBindTimeTypeForMapping(Class propertyType, Class singluarPropertyType) {
057                return false;
058            }
059        };
060        
061        /**
062         * The default Betwixt strategy.
063         */
064        public static final MappingDerivationStrategy DEFAULT = USE_BIND_TIME_TYPE;
065        
066        /**
067         * Should bind time type be used for all elements of the given property type?
068         * @param propertyType <code>Class</code> typing the property, not null
069         * @param singluarPropertyType <code>Class</code> composing the collective
070         * or null if the property is not collective
071         * @return true if bind time type should be used for the mapping
072         */
073        public abstract boolean useBindTimeTypeForMapping(Class propertyType, Class singluarPropertyType);
074    }