Package ma.glasnost.orika.impl.mapping.strategy

Examples of ma.glasnost.orika.impl.mapping.strategy.MappingStrategy


     */
    public <S, D> MappingStrategy resolveMappingStrategy(final S sourceObject, final java.lang.reflect.Type initialSourceType, final java.lang.reflect.Type initialDestinationType, boolean mapInPlace, final MappingContext context) {
       
       
        MappingStrategyKey key = new MappingStrategyKey(sourceObject.getClass(), initialSourceType, initialDestinationType, mapInPlace);
        MappingStrategy strategy = strategyCache.get(key);
       
        if (strategy == null) {
           
            @SuppressWarnings("unchecked")
            Type<S> sourceType = (Type<S>) (initialSourceType != null ? TypeFactory.valueOf(initialSourceType) : TypeFactory.typeOf(sourceObject));
            Type<D> destinationType = TypeFactory.valueOf(initialDestinationType);
           
           
            MappingStrategyRecorder strategyRecorder = new MappingStrategyRecorder(key, unenhanceStrategy);
       
            final Type<S> resolvedSourceType = normalizeSourceType(sourceObject, sourceType, destinationType);
            final S resolvedSourceObject;
           
            if (mapInPlace) {
                resolvedSourceObject = sourceObject;
            } else {
                resolvedSourceObject = unenhanceStrategy.unenhanceObject(sourceObject, sourceType);
            }
       
            strategyRecorder.setResolvedSourceType(resolvedSourceType);
            strategyRecorder.setResolvedDestinationType(destinationType);
            if (sourceObject != resolvedSourceObject) {
                strategyRecorder.setUnenhance(true);
            }
           
            if (!mapInPlace && canCopyByReference(destinationType, resolvedSourceType)) {
                /*
                 * We can copy by reference when source and destination types
                 * are the same and immutable.
                 */
                strategyRecorder.setCopyByReference(true);
            } else if (!mapInPlace && canConvert(resolvedSourceType, destinationType)) {
                strategyRecorder.setResolvedConverter(mapperFactory.getConverterFactory().getConverter(resolvedSourceType,
                            destinationType));
               
            } else {
                Type<? extends D> resolvedDestinationType;
                if (mapInPlace) {
                    resolvedDestinationType = destinationType;
                } else {
                    strategyRecorder.setInstantiate(true);
                    resolvedDestinationType = mapperFactory.lookupConcreteDestinationType(resolvedSourceType, destinationType,
                            context);
                    if (resolvedDestinationType == null) {
                        if (!ClassUtil.isConcrete(destinationType)) {
                            MappingException e = new MappingException("No concrete class mapping defined for source class "
                                    + resolvedSourceType.getName());
                            e.setDestinationType(destinationType);
                            e.setSourceType(resolvedSourceType);
                            throw e;
                        } else {
                            resolvedDestinationType = destinationType;
                        }
                    }
                }
                strategyRecorder.setResolvedDestinationType(resolvedDestinationType);
                strategyRecorder.setResolvedMapper(resolveMapper(resolvedSourceType, resolvedDestinationType));
                if (!mapInPlace) {
                    strategyRecorder.setResolvedObjectFactory(mapperFactory.lookupObjectFactory(resolvedDestinationType));
                }
            }
            strategy = strategyRecorder.playback();
            if (log.isDebugEnabled()) {
                log.debug(strategyRecorder.describeDetails());
            }
            strategyCache.put(key, strategy);
        }
       
        /*
         * Set the resolved types on the current mapping context; this can be used
         * by downstream Mappers to determine the originally resolved types
         */
        context.setResolvedSourceType(strategy.getSoureType());
        context.setResolvedDestinationType(strategy.getDestinationType());
       
        return strategy;
    }
View Full Code Here


                return null;
            }
           
            D existingResult = (D) context.getMappedObject(sourceObject, destinationType);
            if (existingResult == null) {
                MappingStrategy strategy = resolveMappingStrategy(sourceObject, sourceType, destinationType, false, context);
                existingResult = (D) strategy.map(sourceObject, null, context);
            }
            return existingResult;
           
        } catch (MappingException e) {
            /* don't wrap our own exceptions */
 
View Full Code Here

            if (sourceObject == null) {
                throw new MappingException("[sourceObject] can not be null.");
            }
           
            if (context.getMappedObject(sourceObject, destinationType) == null) {
                MappingStrategy strategy = resolveMappingStrategy(sourceObject, sourceType, destinationType, true, context);
                strategy.map(sourceObject, destinationObject, context);
            }

        } catch (MappingException e) {
            /* don't wrap our own exceptions */
            throw e;
View Full Code Here

            if (sourceObject == null) {
                throw new MappingException("[sourceObject] can not be null.");
            }
           
            if (context.getMappedObject(sourceObject, destinationObject.getClass()) == null) {
                MappingStrategy strategy = resolveMappingStrategy(sourceObject, null, destinationObject.getClass(), true, context);
                strategy.map(sourceObject, destinationObject, context);
            }

        } catch (MappingException e) {
            /* don't wrap our own exceptions */
            throw e;
View Full Code Here

       
        if (source == null) {
            return null;
        }
       
        MappingStrategy strategy = null;
        Class<?> sourceClass = null;
        for (final S item : source) {
            if (item == null) {
                continue;
            } else if (strategy == null || (!item.getClass().equals(sourceClass))) {
                /*
                 * Resolve the strategy and reuse; assuming this is a homogeneous collection,
                 * this will save us (n-1) lookups; if not, we would have done those lookups anyway
                 */
                strategy = resolveMappingStrategy(item, sourceType, destinationType, false, context);
                sourceClass = item.getClass();
            }
            D mappedItem = (D) context.getMappedObject(item, destinationType);
            if (mappedItem == null) {
                mappedItem = (D) strategy.map(item, null, context);
            }
            destination.add(mappedItem);
        }
        return destination;
    }
View Full Code Here

                return null;
            }
           
            D result = (D) context.getMappedObject(sourceObject, destinationClass);
            if (result == null) {
                MappingStrategy strategy = resolveMappingStrategy(sourceObject, null, destinationClass, false, context);
                result = (D) strategy.map(sourceObject, null, context);
            }
            return result;
           
        } catch (MappingException e) {
            /* don't wrap our own exceptions */
 
View Full Code Here

        /*
         * Resolve the strategy used for the key and value; only re-resolve
         * a strategy if we encounter a different source class. This should allow
         * us to process a homogeneous key/value typed map as quickly as possible
         */
        MappingStrategy keyStrategy = null;
        MappingStrategy valueStrategy = null;
        Class<?> keyClass = null;
        Class<?> valueClass = null;

        for (Entry<Sk, Sv> entry : source.entrySet()) {
            Dk key;
            if (entry.getKey() == null) {
                key = null;
            } else {
                if (keyStrategy == null || !entry.getKey().getClass().equals(keyClass)) {
                    keyStrategy = resolveMappingStrategy(entry.getKey(), sourceType.<Sk> getNestedType(0), destinationType.<Dk> getNestedType(0), false, context);
                    keyClass = entry.getKey().getClass();
                }
                Dk mappedKey = (Dk) context.getMappedObject(entry.getKey(), destinationType.<Dk> getNestedType(0));
                if (mappedKey == null) {
                    mappedKey = (Dk) (Dk) keyStrategy.map(entry.getKey(), null, context);
                }
               
                key = mappedKey;
            }
           
            Dv value;
            if (entry.getValue() == null) {
                value = null;
            } else {
                if (valueStrategy == null || !entry.getValue().getClass().equals(valueClass)) {
                    valueStrategy = resolveMappingStrategy(entry.getValue(), sourceType.<Sv> getNestedType(1), destinationType.<Dv> getNestedType(1), false, context);
                    valueClass = entry.getValue().getClass();
                }
               
                Dv mappedValue = (Dv) context.getMappedObject(entry.getValue(), destinationType.<Dv> getNestedType(1));
                if (mappedValue == null) {
                    mappedValue = (Dv) (Dv) valueStrategy.map(entry.getValue(), null, context);
                }
               
                value = mappedValue;
            }
           
View Full Code Here

    @SuppressWarnings("unchecked")
    public <S, Dk, Dv> Map<Dk, Dv> mapAsMap(Iterable<S> source, Type<S> sourceType, Type<? extends Map<Dk, Dv>> destinationType,
            MappingContext context) {
       
        Map<Dk, Dv> destination = new HashMap<Dk, Dv>();
        MappingStrategy strategy = null;
        Class<?> entryClass = null;
       
        Type<?> entryType = TypeFactory.valueOf(Map.Entry.class, destinationType.getNestedType(0), destinationType.getNestedType(1));
       
        for (S element : source) {
            if (strategy == null || !element.getClass().equals(entryClass)) {
                strategy = resolveMappingStrategy(element, sourceType, entryType, false, context);
                entryClass = element.getClass();
            }
           
            Map.Entry<Dk, Dv> entry = context.getMappedObject(element, entryType);
            if (entry == null) {
                entry = (Map.Entry<Dk, Dv>) strategy.map(element, null, context);
            }
            destination.put(entry.getKey(), entry.getValue());
        }
       
        return destination;
View Full Code Here

    public <S, Dk, Dv> Map<Dk, Dv> mapAsMap(S[] source, Type<S> sourceType, Type<? extends Map<Dk, Dv>> destinationType,
            MappingContext context) {
       
        Map<Dk, Dv> destination = new HashMap<Dk, Dv>();
        Type<MapEntry<Dk, Dv>> entryType = MapEntry.concreteEntryType(destinationType);
        MappingStrategy strategy = null;
        Class<?> entryClass = null;
       
        for (S element : source) {
            if (strategy == null || !element.getClass().equals(entryClass)) {
                strategy = resolveMappingStrategy(element, sourceType, entryType, false, context);
                entryClass = element.getClass();
            }
           
            MapEntry<Dk, Dv> entry = context.getMappedObject(element, entryType);
            if (entry == null) {
                entry = (MapEntry<Dk, Dv>) strategy.map(element, null, context);
            }
            destination.put(entry.getKey(), entry.getValue());
        }
       
        return destination;
View Full Code Here

    protected void mapArray(float[] destination, List<Object> source, Class<?> clazz, MappingContext mappingContext) {
        if (source == null) {
            return;
        }
       
        MappingStrategy strategy = null;
        Class<?> entryClass = null;
        int i = 0;
        for (final Object s : source) {
            if (strategy == null || !s.getClass().equals(entryClass)) {
                strategy = mapperFacade.resolveMappingStrategy(s, null, clazz, false, mappingContext);
                entryClass = s.getClass();
            }
            destination[i++] = (Float) strategy.map(s, null, mappingContext);
        }
       
    }
View Full Code Here

TOP

Related Classes of ma.glasnost.orika.impl.mapping.strategy.MappingStrategy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.