本文整理汇总了Java中org.springframework.core.CollectionFactory类的典型用法代码示例。如果您正苦于以下问题:Java CollectionFactory类的具体用法?Java CollectionFactory怎么用?Java CollectionFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CollectionFactory类属于org.springframework.core包,在下文中一共展示了CollectionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convert
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
(elementDesc != null ? elementDesc.getType() : null), 1);
if (elementDesc == null || elementDesc.isCollection()) {
target.add(source);
}
else {
Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
target.add(singleElement);
}
return target;
}
示例2: populateInverseRelationship
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
/**
* Populates the property on the other side of the relationship.
*
* @param relationship the relationship on the wrapped data object for which to populate the inverse relationship.
* @param propertyValue the value of the property.
*/
protected void populateInverseRelationship(MetadataChild relationship, Object propertyValue) {
if (propertyValue != null) {
MetadataChild inverseRelationship = relationship.getInverseRelationship();
if (inverseRelationship != null) {
DataObjectWrapper<?> wrappedRelationship = dataObjectService.wrap(propertyValue);
if (inverseRelationship instanceof DataObjectCollection) {
DataObjectCollection collectionRelationship = (DataObjectCollection)inverseRelationship;
String colRelName = inverseRelationship.getName();
Collection<Object> collection =
(Collection<Object>)wrappedRelationship.getPropertyValue(colRelName);
if (collection == null) {
// if the collection is null, let's instantiate an empty one
collection =
CollectionFactory.createCollection(wrappedRelationship.getPropertyType(colRelName), 1);
wrappedRelationship.setPropertyValue(colRelName, collection);
}
collection.add(getWrappedInstance());
}
}
}
}
示例3: readCollectionOfComplexTypes
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
/**
* @param path
* @param collectionType
* @param valueType
* @param source
* @return
*/
private Collection<?> readCollectionOfComplexTypes(String path, Class<?> collectionType, Class<?> valueType,
RedisData source) {
Collection<Object> target = CollectionFactory.createCollection(collectionType, valueType, 10);
Set<byte[]> values = getKeysStartingWith(toBytes(path + ".["), source);
for (byte[] value : values) {
RedisData newPartialSource = new RedisData(extractDataStartingWith(value, source));
byte[] typeInfo = source.getDataForKey(ByteUtils.concat(value, toBytes("._class")));
if (typeInfo != null && typeInfo.length > 0) {
newPartialSource.addDataEntry(toBytes("_class"), typeInfo);
}
Object o = readInternal(fromBytes(value, String.class), valueType, newPartialSource);
target.add(o);
}
return target;
}
示例4: readMapOfSimpleTypes
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
/**
* @param path
* @param mapType
* @param keyType
* @param valueType
* @param source
* @return
*/
private Map<?, ?> readMapOfSimpleTypes(String path, Class<?> mapType, Class<?> keyType, Class<?> valueType,
RedisData source) {
Map<Object, Object> target = CollectionFactory.createMap(mapType, 10);
byte[] prefix = toBytes(path + ".[");
byte[] postfix = toBytes("]");
Map<byte[], byte[]> values = extractDataStartingWith(toBytes(path + ".["), source);
for (Entry<byte[], byte[]> entry : values.entrySet()) {
byte[] binKey = ByteUtils.extract(entry.getKey(), prefix, postfix);
Object key = fromBytes(binKey, keyType);
target.put(key, fromBytes(entry.getValue(), valueType));
}
return target;
}
示例5: readCollection
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
private Object readCollection(Collection<?> source, TypeInformation<?> type, Object parent) {
Assert.notNull(type);
Class<?> collectionType = type.getType();
if (CollectionUtils.isEmpty(source)) {
return source;
}
collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
Collection<Object> items;
if (type.getType().isArray()) {
items = new ArrayList<Object>();
} else {
items = CollectionFactory.createCollection(collectionType, source.size());
}
TypeInformation<?> componentType = type.getComponentType();
Iterator<?> it = source.iterator();
while (it.hasNext()) {
items.add(readValue(it.next(), componentType, parent));
}
return type.getType().isArray() ? convertItemsToArrayOfType(type, items) : items;
}
示例6: readCollection
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private Object readCollection(Collection<?> source, TypeInformation<?> type, Object parent) {
Assert.notNull(type);
Class<?> collectionType = type.getType();
if (CollectionUtils.isEmpty(source)) {
return source;
}
collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
Collection<Object> items = type.getType().isArray() ? new ArrayList<Object>() : CollectionFactory
.createCollection(collectionType, source.size());
TypeInformation<?> componentType = type.getComponentType();
Iterator<?> it = source.iterator();
while (it.hasNext()) {
items.add(readValue(it.next(), componentType, parent));
}
return type.getType().isArray() ? convertItemsToArrayOfType(type, items) : items;
}
示例7: readCollectionOrArray
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
private Object readCollectionOrArray(Path path, Class<?> collectionType, Class<?> valueType, Tuple tuple) {
List<Path> keys = new ArrayList<>(tuple.extractAllKeysFor(path));
boolean isArray = collectionType.isArray();
Class<?> collectionTypeToUse = isArray ? ArrayList.class : collectionType;
Collection<Object> target = CollectionFactory.createCollection(collectionTypeToUse, valueType, keys.size());
for (Path key : keys) {
// if (key.endsWith(TYPE_HINT_ALIAS)) {
// continue;
// }
//
Tuple elementData = tuple.extract(key);
// byte[] typeInfo = elementData.get(key + "." + TYPE_HINT_ALIAS);
// if (typeInfo != null && typeInfo.length > 0) {
// elementData.put(TYPE_HINT_ALIAS, typeInfo);
// }
Class<?> typeToUse = getTypeHint(key, tuple, valueType);
if (isTarantoolNativeType(valueType)) {
target.add(fromTarantoolNativeType(elementData.get(key), typeToUse));
} else {
target.add(readInternal(key, valueType, new TarantoolData(elementData)));
}
}
return isArray ? toArray(target, collectionType, valueType) : target;
}
示例8: convertDataArrayToTargetCollection
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType, Class<?> elementType)
throws NoSuchMethodException {
Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType());
Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array));
for (int i = 0; i < array.length; i++) {
resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
}
return resultColl;
}
示例9: newValue
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
private Object newValue(Class<?> type, String name) {
try {
if (type.isArray()) {
Class<?> componentType = type.getComponentType();
// TODO - only handles 2-dimensional arrays
if (componentType.isArray()) {
Object array = Array.newInstance(componentType, 1);
Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
return array;
}
else {
return Array.newInstance(componentType, 0);
}
}
else if (Collection.class.isAssignableFrom(type)) {
return CollectionFactory.createCollection(type, 16);
}
else if (Map.class.isAssignableFrom(type)) {
return CollectionFactory.createMap(type, 16);
}
else {
return type.newInstance();
}
}
catch (Exception ex) {
// TODO Root cause exception context is lost here... should we throw another exception type that preserves context instead?
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex);
}
}
示例10: convert
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
Collection<?> sourceCollection = (Collection<?>) source;
// Shortcut if possible...
boolean copyRequired = !targetType.getType().isInstance(source);
if (!copyRequired && sourceCollection.isEmpty()) {
return source;
}
TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
if (elementDesc == null && !copyRequired) {
return source;
}
// At this point, we need a collection copy in any case, even if just for finding out about element copies...
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
(elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());
if (elementDesc == null) {
target.addAll(sourceCollection);
}
else {
for (Object sourceElement : sourceCollection) {
Object targetElement = this.conversionService.convert(sourceElement,
sourceType.elementTypeDescriptor(sourceElement), elementDesc);
target.add(targetElement);
if (sourceElement != targetElement) {
copyRequired = true;
}
}
}
return (copyRequired ? target : source);
}
示例11: newValue
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
try {
if (type.isArray()) {
Class<?> componentType = type.getComponentType();
// TODO - only handles 2-dimensional arrays
if (componentType.isArray()) {
Object array = Array.newInstance(componentType, 1);
Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
return array;
}
else {
return Array.newInstance(componentType, 0);
}
}
else if (Collection.class.isAssignableFrom(type)) {
TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
}
else if (Map.class.isAssignableFrom(type)) {
TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
}
else {
return BeanUtils.instantiate(type);
}
}
catch (Exception ex) {
// TODO: Root cause exception context is lost here; just exception message preserved.
// Should we throw another exception type that preserves context instead?
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex);
}
}
示例12: readCollectionOrArray
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
/**
* Reads the given {@link List} into a collection of the given {@link TypeInformation}
* .
*
* @param targetType must not be {@literal null}.
* @param sourceValue must not be {@literal null}.
* @return the converted {@link Collection} or array, will never be {@literal null}.
*/
@Nullable
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object readCollectionOrArray(TypeInformation<?> targetType, List sourceValue) {
Assert.notNull(targetType, "Target type must not be null!");
Class<?> collectionType = targetType.getType();
TypeInformation<?> componentType = targetType.getComponentType() != null ? targetType
.getComponentType() : ClassTypeInformation.OBJECT;
Class<?> rawComponentType = componentType.getType();
collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType
: List.class;
Collection<Object> items = targetType.getType().isArray() ? new ArrayList<>(
sourceValue.size()) : CollectionFactory.createCollection(collectionType,
rawComponentType, sourceValue.size());
if (sourceValue.isEmpty()) {
return getPotentiallyConvertedSimpleRead(items, collectionType);
}
for (Object obj : sourceValue) {
if (obj instanceof Map) {
items.add(read(componentType, (Map) obj));
}
else if (obj instanceof List) {
items.add(readCollectionOrArray(ClassTypeInformation.OBJECT, (List) obj));
}
else {
items.add(getPotentiallyConvertedSimpleRead(obj, rawComponentType));
}
}
return getPotentiallyConvertedSimpleRead(items, targetType.getType());
}
示例13: readCollectionOfSimpleTypes
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
/**
* @param path
* @param collectionType
* @param valueType
* @param source
* @return
*/
private Collection<?> readCollectionOfSimpleTypes(String path, Class<?> collectionType, Class<?> valueType,
RedisData source) {
Collection<Object> target = CollectionFactory.createCollection(collectionType, valueType, 10);
Map<byte[], byte[]> values = extractDataStartingWith(toBytes(path + ".["), source);
for (byte[] value : values.values()) {
target.add(fromBytes(value, valueType));
}
return target;
}
示例14: readMapOfComplexTypes
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
/**
* @param path
* @param mapType
* @param keyType
* @param valueType
* @param source
* @return
*/
private Map<?, ?> readMapOfComplexTypes(String path, Class<?> mapType, Class<?> keyType, Class<?> valueType,
RedisData source) {
Map<Object, Object> target = CollectionFactory.createMap(mapType, 10);
byte[] prefix = toBytes(path + ".[");
byte[] postfix = toBytes("]");
Set<byte[]> keys = getKeysStartingWith(prefix, source);
for (byte[] fullKey : keys) {
byte[] binKey = ByteUtils.extract(fullKey, prefix, postfix);
Object key = fromBytes(binKey, keyType);
RedisData newPartialSource = new RedisData(extractDataStartingWith(fullKey, source));
byte[] typeInfo = source.getDataForKey(ByteUtils.concat(fullKey, toBytes("._class")));
if (typeInfo != null && typeInfo.length > 0) {
newPartialSource.addDataEntry(toBytes("_class"), typeInfo);
}
Object o = readInternal(fromBytes(fullKey, String.class), valueType, newPartialSource);
target.put(key, o);
}
return target;
}
示例15: convertDataArrayToTargetCollection
import org.springframework.core.CollectionFactory; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private Collection convertDataArrayToTargetCollection(Object[] array, Class collectionType, Class elementType)
throws NoSuchMethodException {
Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType());
Collection resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array));
for (int i = 0; i < array.length; i++) {
resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
}
return resultColl;
}