All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.shyiko.mappify.api.AbstractMapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 Stanley Shyiko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.shyiko.mappify.api;

import java.lang.reflect.Array;
import java.util.*;

/**
 * Convenient partial implementation of {@link Mapper}, which leaves only two methods to be overridden -
 * {@link #map(Object, Object, String, MappingContext)} and {@link #allowsToMap(Class, Class, String)}.
 *
 * @author Stanley Shyiko
 */
public abstract class AbstractMapper implements Mapper {

    protected volatile boolean enforceMappingContext = true;

    /**
     * @return true if mapping context must be created at the beginning of the map invocation
     * (if not explicitly provided), false otherwise.
     */
    public boolean isEnforceMappingContext() {
        return enforceMappingContext;
    }

    /**
     * @param enforceMappingContext true indicates that mapping context must always be present during the map process,
     * regardless of whether it was passed or is going to be used.
     */
    public void setEnforceMappingContext(boolean enforceMappingContext) {
        this.enforceMappingContext = enforceMappingContext;
    }

    @Override
    public  T map(Object source, T target) {
        return map(source, target, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public  T map(Object source, T target, String mappingName) {
        return map(source, target, mappingName, getDefaultContext());
    }

    @Override
    public  T map(Object source, T target, MappingContext mappingContext) {
        return map(source, target, getDefaultMappingName(), mappingContext);
    }

    @Override
    public  T map(Object source, Class targetClass) {
        return map(source, targetClass, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public  T map(Object source, Class targetClass, String mappingName) {
        return map(source, targetClass, mappingName, getDefaultContext());
    }

    @Override
    public  T map(Object source, Class targetClass, MappingContext mappingContext) {
        return map(source, targetClass, getDefaultMappingName(), mappingContext);
    }

    @Override
    public  T map(Object source, Class targetClass, String mappingName, MappingContext mappingContext) {
        if (source == null) {
            return null;
        }
        assertNotNull(targetClass, "Target class cannot be null");
        return map(source, newInstance(targetClass), mappingName, mappingContext);
    }

    protected  T newInstance(Class targetClass) {
        T result;
        try {
            result = targetClass.newInstance();
        } catch (Exception e) {
            throw new MappingException("Unable to create instance of " + targetClass, e);
        }
        return result;
    }

    @Override
    public , T> C map(Collection sourceCollection, Class targetClass, C targetCollection) {
        return map(sourceCollection, targetClass, targetCollection, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public , T> C map(
            Collection sourceCollection, Class targetClass, C targetCollection, String mappingName) {
        return map(sourceCollection, targetClass, targetCollection, mappingName, getDefaultContext());
    }

    @Override
    public , T> C map(
            Collection sourceCollection, Class targetClass, C targetCollection, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, targetCollection, getDefaultMappingName(), mappingContext);
    }

    @Override
    public , T> C map(
            Collection sourceCollection, Class targetClass, C targetCollection, String mappingName,
            MappingContext mappingContext) {
        assertNotNull(sourceCollection, "Source collection must never be null");
        for (Object source : sourceCollection) {
            targetCollection.add(map(source, targetClass, mappingName, mappingContext));
        }
        return targetCollection;
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetCollection) {
        return map(sourceArray, targetClass, targetCollection, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetCollection,
                                                 String mappingName) {
        return map(sourceArray, targetClass, targetCollection, mappingName, getDefaultContext());
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetCollection,
                                                 MappingContext mappingContext) {
        return map(sourceArray, targetClass, targetCollection, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetCollection,
                                                 String mappingName, MappingContext mappingContext) {
        assertNotNull(sourceArray, "Source array must never be null");
        for (Object source : sourceArray) {
            targetCollection.add(map(source, targetClass, mappingName, mappingContext));
        }
        return targetCollection;
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetMap) {
        return map(sourceArray, targetClass, targetMap, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetMap, String mappingName) {
        return map(sourceArray, targetClass, targetMap, mappingName, getDefaultContext());
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetMap,
                                             MappingContext mappingContext) {
        return map(sourceArray, targetClass, targetMap, getDefaultMappingName(), mappingContext);
    }

    @Override
    public , T> C map(S[] sourceArray, Class targetClass, C targetMap, String mappingName,
                                             MappingContext mappingContext) {
        assertNotNull(sourceArray, "Source array must never be null");
        for (S source : sourceArray) {
            targetMap.put(source, map(source, targetClass, mappingName, mappingContext));
        }
        return targetMap;
    }

    @Override
    public  T[] map(S[] sourceArray, Class targetClass) {
        return map(sourceArray, targetClass, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public  T[] map(S[] sourceArray, Class targetClass, String mappingName) {
        return map(sourceArray, targetClass, mappingName, getDefaultContext());
    }

    @Override
    public  T[] map(S[] sourceArray, Class targetClass, MappingContext mappingContext) {
        return map(sourceArray, targetClass, getDefaultMappingName(), mappingContext);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  T[] map(S[] sourceArray, Class targetClass, String mappingName, MappingContext mappingContext) {
        assertNotNull(sourceArray, "Source array must never be null");
        T[] result = (T[]) Array.newInstance(targetClass, sourceArray.length);
        int i = 0;
        for (Object source : sourceArray) {
            result[i++] = map(source, targetClass, mappingName, mappingContext);
        }
        return result;
    }

    protected void assertNotNull(Object object, String exceptionMessage) {
        if (object == null) {
            throw new MappingException(exceptionMessage);
        }
    }

    @Override
    public , T> C map(Collection sourceCollection, Class targetClass, C targetMap) {
        return map(sourceCollection, targetClass, targetMap, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public , T> C map(
            Collection sourceCollection, Class targetClass, C targetMap, String mappingName) {
        return map(sourceCollection, targetClass, targetMap, mappingName, getDefaultContext());
    }

    @Override
    public , T> C map(
            Collection sourceCollection, Class targetClass, C targetMap, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, targetMap, getDefaultMappingName(), mappingContext);
    }

    @Override
    public , T> C map(
            Collection sourceCollection, Class targetClass, C targetMap, String mappingName,
            MappingContext mappingContext) {
        assertNotNull(sourceCollection, "Source collection must never be null");
        for (S source : sourceCollection) {
            targetMap.put(source, map(source, targetClass, mappingName, mappingContext));
        }
        return targetMap;
    }

    @Override
    public  T[] map(Collection sourceCollection, Class targetClass) {
        return map(sourceCollection, targetClass, getDefaultMappingName(), getDefaultContext());
    }

    @Override
    public  T[] map(Collection sourceCollection, Class targetClass, String mappingName) {
        return map(sourceCollection, targetClass, mappingName, getDefaultContext());
    }

    @Override
    public  T[] map(Collection sourceCollection, Class targetClass, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, getDefaultMappingName(), mappingContext);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  T[] map(
            Collection sourceCollection, Class targetClass, String mappingName, MappingContext mappingContext) {
        assertNotNull(sourceCollection, "Source collection must never be null");
        T[] result = (T[]) Array.newInstance(targetClass, sourceCollection.size());
        int i = 0;
        for (Object source : sourceCollection) {
            result[i++] = map(source, targetClass, mappingName, mappingContext);
        }
        return result;
    }

    @Override
    public boolean allowsToMap(Class sourceClass, Class targetClass) {
        return allowsToMap(sourceClass, targetClass, getDefaultMappingName());
    }

    @Override
    public  ArrayList mapToArrayList(Collection sourceCollection, Class targetClass) {
        return map(sourceCollection, targetClass, new ArrayList(determineSICForArrayList(sourceCollection)));
    }

    @Override
    public  ArrayList mapToArrayList(Collection sourceCollection, Class targetClass, String mappingName) {
        return map(sourceCollection, targetClass, new ArrayList(determineSICForArrayList(sourceCollection)),
                mappingName);
    }

    @Override
    public  ArrayList mapToArrayList(Collection sourceCollection, Class targetClass,
                                           MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new ArrayList(determineSICForArrayList(sourceCollection)),
                mappingContext);
    }

    @Override
    public  ArrayList mapToArrayList(Collection sourceCollection, Class targetClass,
                                           String mappingName, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new ArrayList(determineSICForArrayList(sourceCollection)),
                mappingName, mappingContext);
    }

    @Override
    public  ArrayList mapToArrayList(S[] sourceArray, Class targetClass) {
        return map(sourceArray, targetClass, new ArrayList(determineSICForArrayList(sourceArray)));
    }

    @Override
    public  ArrayList mapToArrayList(S[] sourceArray, Class targetClass, String mappingName) {
        return map(sourceArray, targetClass, new ArrayList(determineSICForArrayList(sourceArray)), mappingName);
    }

    @Override
    public  ArrayList mapToArrayList(S[] sourceArray, Class targetClass, MappingContext mappingContext) {
        return map(sourceArray, targetClass, new ArrayList(determineSICForArrayList(sourceArray)), mappingContext);
    }

    @Override
    public  ArrayList mapToArrayList(S[] sourceArray, Class targetClass, String mappingName,
                                              MappingContext mappingContext) {
        return map(sourceArray, targetClass, new ArrayList(determineSICForArrayList(sourceArray)),
                mappingName, mappingContext);
    }

    protected int determineSICForArrayList(Collection collection) {
        assertNotNull(collection, "Source collection must never be null");
        return collection.size();
    }

    protected int determineSICForArrayList(Object[] array) {
        assertNotNull(array, "Source array must never be null");
        return array.length;
    }

    @Override
    public  HashSet mapToHashSet(Collection sourceCollection, Class targetClass) {
        return map(sourceCollection, targetClass,
                new HashSet(determineSICForMap(sourceCollection)));
    }

    @Override
    public  HashSet mapToHashSet(Collection sourceCollection, Class targetClass, String mappingName) {
        return map(sourceCollection, targetClass,
                new HashSet(determineSICForMap(sourceCollection)), mappingName);
    }

    @Override
    public  HashSet mapToHashSet(Collection sourceCollection, Class targetClass,
                                       MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new HashSet(determineSICForMap(sourceCollection)),
                mappingContext);
    }

    @Override
    public  HashSet mapToHashSet(Collection sourceCollection, Class targetClass, String mappingName,
                                       MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new HashSet(determineSICForMap(sourceCollection)), mappingName,
                mappingContext);
    }

    @Override
    public  HashSet mapToHashSet(S[] sourceArray, Class targetClass) {
        return map(sourceArray, targetClass, new HashSet(determineSICForMap(sourceArray)));
    }

    @Override
    public  HashSet mapToHashSet(S[] sourceArray, Class targetClass, String mappingName) {
        return map(sourceArray, targetClass, new HashSet(determineSICForMap(sourceArray)), mappingName);
    }

    @Override
    public  HashSet mapToHashSet(S[] sourceArray, Class targetClass, MappingContext mappingContext) {
        return map(sourceArray, targetClass, new HashSet(determineSICForMap(sourceArray)), mappingContext);
    }

    @Override
    public  HashSet mapToHashSet(S[] sourceArray, Class targetClass, String mappingName,
                                          MappingContext mappingContext) {
        return map(sourceArray, targetClass, new HashSet(determineSICForMap(sourceArray)), mappingName,
                mappingContext);
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(Collection sourceCollection, Class targetClass) {
        return map(sourceCollection, targetClass, new LinkedHashSet(determineSICForMap(sourceCollection)));
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(Collection sourceCollection, Class targetClass,
                                                   String mappingName) {
        return map(sourceCollection, targetClass, new LinkedHashSet(determineSICForMap(sourceCollection)),
                mappingName);
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(Collection sourceCollection, Class targetClass, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new LinkedHashSet(determineSICForMap(sourceCollection)),
                mappingContext);
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(Collection sourceCollection, Class targetClass,
                                                   String mappingName, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new LinkedHashSet(determineSICForMap(sourceCollection)),
                mappingName, mappingContext);
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(S[] sourceArray, Class targetClass) {
        return map(sourceArray, targetClass, new LinkedHashSet(determineSICForMap(sourceArray)));
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(S[] sourceArray, Class targetClass, String mappingName) {
        return map(sourceArray, targetClass, new LinkedHashSet(determineSICForMap(sourceArray)), mappingName);
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(S[] sourceArray, Class targetClass,
                                                      MappingContext mappingContext) {
        return map(sourceArray, targetClass, new LinkedHashSet(determineSICForMap(sourceArray)), mappingContext);
    }

    @Override
    public  LinkedHashSet mapToLinkedHashSet(S[] sourceArray, Class targetClass,
                                                      String mappingName, MappingContext mappingContext) {
        return map(sourceArray, targetClass, new LinkedHashSet(determineSICForMap(sourceArray)), mappingName,
                mappingContext);
    }

    @Override
    public  HashMap mapToHashMap(Collection sourceCollection, Class targetClass) {
        return map(sourceCollection, targetClass, new HashMap(determineSICForMap(sourceCollection)));
    }

    @Override
    public  HashMap mapToHashMap(Collection sourceCollection, Class targetClass,
                                             String mappingName) {
        return map(sourceCollection, targetClass, new HashMap(determineSICForMap(sourceCollection)),
                mappingName);
    }

    @Override
    public  HashMap mapToHashMap(Collection sourceCollection, Class targetClass,
                                             MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new HashMap(determineSICForMap(sourceCollection)),
                mappingContext);
    }

    @Override
    public  HashMap mapToHashMap(Collection sourceCollection, Class targetClass,
                                             String mappingName, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new HashMap(determineSICForMap(sourceCollection)),
                mappingName, mappingContext);
    }

    @Override
    public  HashMap mapToHashMap(S[] sourceArray, Class targetClass) {
        return map(sourceArray, targetClass, new HashMap(determineSICForMap(sourceArray)));
    }

    @Override
    public  HashMap mapToHashMap(S[] sourceArray, Class targetClass, String mappingName) {
        return map(sourceArray, targetClass, new HashMap(determineSICForMap(sourceArray)), mappingName);
    }

    @Override
    public  HashMap mapToHashMap(S[] sourceArray, Class targetClass, MappingContext mappingContext) {
        return map(sourceArray, targetClass, new HashMap(determineSICForMap(sourceArray)), mappingContext);
    }

    @Override
    public  HashMap mapToHashMap(S[] sourceArray, Class targetClass, String mappingName,
                                             MappingContext mappingContext) {
        return map(sourceArray, targetClass, new HashMap(determineSICForMap(sourceArray)),
                mappingName, mappingContext);
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(Collection sourceCollection, Class targetClass) {
        return map(sourceCollection, targetClass, new LinkedHashMap(determineSICForMap(sourceCollection)));
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(Collection sourceCollection, Class targetClass,
                                                         String mappingName) {
        return map(sourceCollection, targetClass, new LinkedHashMap(determineSICForMap(sourceCollection)),
                mappingName);
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(Collection sourceCollection, Class targetClass,
                                                         MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new LinkedHashMap(determineSICForMap(sourceCollection)),
                mappingContext);
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(Collection sourceCollection, Class targetClass,
                                                         String mappingName, MappingContext mappingContext) {
        return map(sourceCollection, targetClass, new LinkedHashMap(determineSICForMap(sourceCollection)),
                mappingName, mappingContext);
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(S[] sourceArray, Class targetClass) {
        return map(sourceArray, targetClass, new LinkedHashMap(determineSICForMap(sourceArray)));
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(S[] sourceArray, Class targetClass, String mappingName) {
        return map(sourceArray, targetClass, new LinkedHashMap(determineSICForMap(sourceArray)), mappingName);
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(S[] sourceArray, Class targetClass,
                                                         MappingContext mappingContext) {
        return map(sourceArray, targetClass, new LinkedHashMap(determineSICForMap(sourceArray)), mappingContext);
    }

    @Override
    public  LinkedHashMap mapToLinkedHashMap(S[] sourceArray, Class targetClass,
                                                         String mappingName, MappingContext mappingContext) {
        return map(sourceArray, targetClass, new LinkedHashMap(determineSICForMap(sourceArray)),
                mappingName, mappingContext);
    }

    protected int determineSICForMap(Collection collection) {
        assertNotNull(collection, "Source collection must never be null");
        return determineSufficientInitialCapacityForMap(collection.size());
    }

    protected int determineSICForMap(Object[] array) {
        assertNotNull(array, "Source array must never be null");
        return determineSufficientInitialCapacityForMap(array.length);
    }

    protected int determineSufficientInitialCapacityForMap(int size) {
        return Math.max((int) (size / .75f) + 1, 16);
    }

    protected String getDefaultMappingName() {
        return "";
    }

    protected MappingContext getDefaultContext() {
        return enforceMappingContext ? new MappingContext() : null;
    }
}