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.
/*
* Copyright 2001-present Stephen Colebourne
*
* 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 org.joda.beans.ser;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.SortedMultiset;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;
import com.google.common.collect.TreeMultiset;
/**
* Guava factory used to create wrappers around collection-like objects.
*/
public class GuavaSerIteratorFactory extends SerIteratorFactory {
/**
* Creates an iterator wrapper for a meta-property value.
*
* @param value the possible collection-like object, not null
* @param prop the meta-property defining the value, not null
* @param beanClass the class of the bean, not the meta-property, for better generics, not null
* @return the iterator, null if not a collection-like type
*/
@Override
public SerIterator create(final Object value, final MetaProperty> prop, Class> beanClass) {
Class> declaredType = prop.propertyType();
if (value instanceof BiMap) {
Class> keyType = JodaBeanUtils.mapKeyType(prop, beanClass);
Class> valueType = JodaBeanUtils.mapValueType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return biMap((BiMap, ?>) value, declaredType, keyType, valueType, valueTypeTypes);
}
if (value instanceof Multiset) {
Class> valueType = JodaBeanUtils.collectionType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.collectionTypeTypes(prop, beanClass);
return multiset((Multiset>) value, declaredType, valueType, valueTypeTypes);
}
if (value instanceof Multimap) {
Class> keyType = JodaBeanUtils.mapKeyType(prop, beanClass);
Class> valueType = JodaBeanUtils.mapValueType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return multimap((Multimap, ?>) value, declaredType, keyType, valueType, valueTypeTypes);
}
if (value instanceof Table) {
Class> rowType = JodaBeanUtils.extractTypeClass(prop, beanClass, 3, 0);
Class> colType = JodaBeanUtils.extractTypeClass(prop, beanClass, 3, 1);
Class> valueType = JodaBeanUtils.extractTypeClass(prop, beanClass, 3, 2);
return table((Table, ?, ?>) value, declaredType, rowType, colType, valueType, EMPTY_VALUE_TYPES);
}
return super.create(value, prop, beanClass);
}
/**
* Creates an iterator wrapper for a value retrieved from a parent iterator.
*
* Allows the parent iterator to define the child iterator using generic type information.
* This handles cases such as a {@code List} as the value in a {@code Map}.
*
* @param value the possible collection-like object, not null
* @param parent the parent iterator, not null
* @return the iterator, null if not a collection-like type
*/
@Override
public SerIterator createChild(final Object value, final SerIterator parent) {
Class> declaredType = parent.valueType();
List> childGenericTypes = parent.valueTypeTypes();
if (value instanceof BiMap) {
if (childGenericTypes.size() == 2) {
return biMap((BiMap, ?>) value, declaredType, childGenericTypes.get(0), childGenericTypes.get(1), EMPTY_VALUE_TYPES);
}
return biMap((BiMap, ?>) value, Object.class, Object.class, Object.class, EMPTY_VALUE_TYPES);
}
if (value instanceof Multimap) {
if (childGenericTypes.size() == 2) {
return multimap((Multimap, ?>) value, declaredType, childGenericTypes.get(0), childGenericTypes.get(1), EMPTY_VALUE_TYPES);
}
return multimap((Multimap, ?>) value, Object.class, Object.class, Object.class, EMPTY_VALUE_TYPES);
}
if (value instanceof Multiset) {
if (childGenericTypes.size() == 1) {
return multiset((Multiset>) value, declaredType, childGenericTypes.get(0), EMPTY_VALUE_TYPES);
}
return multiset((Multiset>) value, Object.class, Object.class, EMPTY_VALUE_TYPES);
}
if (value instanceof Table) {
if (childGenericTypes.size() == 3) {
return table((Table, ?, ?>) value, declaredType,
childGenericTypes.get(0), childGenericTypes.get(1),
childGenericTypes.get(2), EMPTY_VALUE_TYPES);
}
return table((Table, ?, ?>) value, Object.class, Object.class, Object.class, Object.class, EMPTY_VALUE_TYPES);
}
return super.createChild(value, parent);
}
//-----------------------------------------------------------------------
/**
* Creates an iterator wrapper for a meta-property value.
*
* @param metaTypeDescription the description of the collection type, not null
* @param settings the settings object, not null
* @param knownTypes the known types map, null if not using known type shortening
* @return the iterator, null if not a collection-like type
*/
@Override
public SerIterable createIterable(final String metaTypeDescription, final JodaBeanSer settings, final Map> knownTypes) {
if (metaTypeDescription.equals("BiMap")) {
return biMap(Object.class, Object.class, EMPTY_VALUE_TYPES);
}
if (metaTypeDescription.equals("SetMultimap")) {
return setMultimap(Object.class, Object.class, EMPTY_VALUE_TYPES);
}
if (metaTypeDescription.equals("ListMultimap")) {
return listMultimap(Object.class, Object.class, EMPTY_VALUE_TYPES);
}
if (metaTypeDescription.equals("Multimap")) {
return listMultimap(Object.class, Object.class, EMPTY_VALUE_TYPES);
}
if (metaTypeDescription.equals("Multiset")) {
return multiset(Object.class, EMPTY_VALUE_TYPES);
}
if (metaTypeDescription.equals("Table")) {
return table(Object.class, Object.class, Object.class, EMPTY_VALUE_TYPES);
}
return super.createIterable(metaTypeDescription, settings, knownTypes);
}
/**
* Creates an iterator wrapper for a meta-property value.
*
* @param prop the meta-property defining the value, not null
* @param beanClass the class of the bean, not the meta-property, for better generics, not null
* @return the iterator, null if not a collection-like type
*/
@Override
public SerIterable createIterable(final MetaProperty> prop, Class> beanClass) {
if (BiMap.class.isAssignableFrom(prop.propertyType())) {
Class> keyType = defaultToObjectClass(JodaBeanUtils.mapKeyType(prop, beanClass));
Class> valueType = defaultToObjectClass(JodaBeanUtils.mapValueType(prop, beanClass));
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return biMap(keyType, valueType, valueTypeTypes);
}
if (SortedMultiset.class.isAssignableFrom(prop.propertyType())) {
Class> valueType = defaultToObjectClass(JodaBeanUtils.collectionType(prop, beanClass));
List> valueTypeTypes = JodaBeanUtils.collectionTypeTypes(prop, beanClass);
return sortedMultiset(valueType, valueTypeTypes);
}
if (Multiset.class.isAssignableFrom(prop.propertyType())) {
Class> valueType = defaultToObjectClass(JodaBeanUtils.collectionType(prop, beanClass));
List> valueTypeTypes = JodaBeanUtils.collectionTypeTypes(prop, beanClass);
return multiset(valueType, valueTypeTypes);
}
if (SetMultimap.class.isAssignableFrom(prop.propertyType())) {
Class> keyType = defaultToObjectClass(JodaBeanUtils.mapKeyType(prop, beanClass));
Class> valueType = defaultToObjectClass(JodaBeanUtils.mapValueType(prop, beanClass));
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return setMultimap(keyType, valueType, valueTypeTypes);
}
if (ListMultimap.class.isAssignableFrom(prop.propertyType())) {
Class> keyType = defaultToObjectClass(JodaBeanUtils.mapKeyType(prop, beanClass));
Class> valueType = defaultToObjectClass(JodaBeanUtils.mapValueType(prop, beanClass));
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return listMultimap(keyType, valueType, valueTypeTypes);
}
if (Multimap.class.isAssignableFrom(prop.propertyType())) {
Class> keyType = defaultToObjectClass(JodaBeanUtils.mapKeyType(prop, beanClass));
Class> valueType = defaultToObjectClass(JodaBeanUtils.mapValueType(prop, beanClass));
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return listMultimap(keyType, valueType, valueTypeTypes);
}
if (Table.class.isAssignableFrom(prop.propertyType())) {
Class> rowType = defaultToObjectClass(JodaBeanUtils.extractTypeClass(prop, beanClass, 3, 0));
Class> colType = defaultToObjectClass(JodaBeanUtils.extractTypeClass(prop, beanClass, 3, 1));
Class> valueType = defaultToObjectClass(JodaBeanUtils.extractTypeClass(prop, beanClass, 3, 2));
return table(rowType, colType, valueType, EMPTY_VALUE_TYPES);
}
if (ImmutableList.class.isAssignableFrom(prop.propertyType())) {
Class> valueType = JodaBeanUtils.collectionType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.collectionTypeTypes(prop, beanClass);
return immutableList(valueType, valueTypeTypes);
}
if (ImmutableSortedSet.class.isAssignableFrom(prop.propertyType())) {
Class> valueType = JodaBeanUtils.collectionType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.collectionTypeTypes(prop, beanClass);
return immutableSortedSet(valueType, valueTypeTypes);
}
if (ImmutableSet.class.isAssignableFrom(prop.propertyType())) {
Class> valueType = JodaBeanUtils.collectionType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.collectionTypeTypes(prop, beanClass);
return immutableSet(valueType, valueTypeTypes);
}
if (ImmutableSortedMap.class.isAssignableFrom(prop.propertyType())) {
Class> keyType = JodaBeanUtils.mapKeyType(prop, beanClass);
Class> valueType = JodaBeanUtils.mapValueType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return immutableSortedMap(keyType, valueType, valueTypeTypes);
}
if (ImmutableMap.class.isAssignableFrom(prop.propertyType())) {
Class> keyType = JodaBeanUtils.mapKeyType(prop, beanClass);
Class> valueType = JodaBeanUtils.mapValueType(prop, beanClass);
List> valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return immutableMap(keyType, valueType, valueTypeTypes);
}
return super.createIterable(prop, beanClass);
}
//-----------------------------------------------------------------------
/**
* Gets an iterable wrapper for {@code BiMap}.
*
* @param keyType the value type, not null
* @param valueType the value type, not null
* @param valueTypeTypes the generic parameters of the value type
* @return the iterable, not null
*/
public static final SerIterable biMap(final Class> keyType, final Class> valueType, final List> valueTypeTypes) {
final BiMap