Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* The MIT License (MIT)
*
* Copyright (c) 2014
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.mjeanroy.spring.mappers.objects;
import com.github.mjeanroy.spring.mappers.Mapper;
import com.github.mjeanroy.spring.mappers.factory.ObjectFactory;
import java.util.ArrayList;
import java.util.Collection;
/**
* In memory mapper implementation.
*
* This implementation returns iterable of destination objects that will be
* stored in a collection. It means that destination object are mapped when
* the iterable is returned.
*
* Iterable can be explicitly cast to a collection.
*
* This class is an abstract class because it needs to be sub-classed to be able to use
* constructor without generic types.
*
* @param Type of source objects.
* @param Type of destination objects.
*/
public abstract class AbstractInMemoryObjectMapper extends AbstractObjectMapper implements ObjectMapper {
/**
* Create new in memory mapper.
* Generic types will be detected at object creation.
*
* @param mapper Mapper used to map source to destination.
*/
protected AbstractInMemoryObjectMapper(Mapper mapper) {
super(mapper);
}
/**
* Create new in memory mapper.
*
* @param mapper Mapper used to map source to destination.
* @param klassT Source type.
* @param klassU Destination type.
*/
protected AbstractInMemoryObjectMapper(Mapper mapper, Class klassT, Class klassU) {
super(mapper, klassT, klassU);
}
/**
* Create new in memory mapper.
*
* @param mapper Mapper used to map source to destination.
* @param klassT Source type.
* @param klassU Destination type.
* @param factory Factory used to instantiate destination object.
*/
protected AbstractInMemoryObjectMapper(Mapper mapper, Class klassT, Class klassU, ObjectFactory factory) {
super(mapper, klassT, klassU, factory);
}
@Override
public Collection from(Iterable sources) {
final Collection results = initIterable(sources);
for (T source : sources) {
final U destination = convert(source);
results.add(destination);
}
return results;
}
/**
* Init iterable collection object.
* By default, this method create a new array list.
* Initial capacity of array list is defined by {@link #initialCapacity(Iterable)} method.
*
* Collection returned must be empty (since it will be filled later).
* Source object can be used to compute target collection size.
*
* @param sources Source objects.
* @return Empty destination collection.
*/
protected Collection initIterable(Iterable sources) {
final int size = initialCapacity(sources);
return new ArrayList(size);
}
/**
* Try to compute initial capacity of destination collections from
* source collection.
*
* Default is:
* - If iterable parameter is an instance of collection, {@link java.util.Collection#size()} is used.
* - Otherwise 10 is returned.
*
* @param iterables Source objects.
* @return Initial capacity of destination.
*/
protected int initialCapacity(Iterable iterables) {
final int size;
if (iterables instanceof Collection) {
return ((Collection) iterables).size();
}
else {
size = 10;
}
return size;
}
}