Example usage for com.google.gwt.dev.util.collect Lists sort

List of usage examples for com.google.gwt.dev.util.collect Lists sort

Introduction

In this page you can find the example usage for com.google.gwt.dev.util.collect Lists sort.

Prototype

public static <T> List<T> sort(List<T> list, Comparator<? super T> sort) 

Source Link

Usage

From source file:com.github.nmorel.gwtjackson.rebind.property.PropertyProcessor.java

License:Apache License

public static PropertiesContainer findAllProperties(RebindConfiguration configuration, TreeLogger logger,
        JacksonTypeOracle typeOracle, BeanInfo beanInfo, boolean mapperInSamePackageAsType)
        throws UnableToCompleteException {

    // we first parse the bean to retrieve all the properties
    ImmutableMap<String, PropertyAccessors> fieldsMap = PropertyParser.findPropertyAccessors(configuration,
            logger, beanInfo);/*from w  ww.  j a  v  a2s  . c o m*/

    // value, any getter and any setter properties
    PropertyInfo valuePropertyInfo = null;
    PropertyInfo anyGetterPropertyInfo = null;
    PropertyInfo anySetterPropertyInfo = null;

    // Processing all the properties accessible via field, getter or setter
    Map<String, PropertyInfo> propertiesMap = new LinkedHashMap<String, PropertyInfo>();
    for (PropertyAccessors propertyAccessors : fieldsMap.values()) {

        Optional<PropertyInfo> propertyInfoOptional = processProperty(configuration, logger, typeOracle,
                propertyAccessors, beanInfo, mapperInSamePackageAsType);

        if (propertyInfoOptional.isPresent()) {

            PropertyInfo propertyInfo = propertyInfoOptional.get();

            boolean put = true;

            if (propertyInfo.isValue()) {
                if (null != valuePropertyInfo) {
                    logger.log(TreeLogger.Type.WARN, "More than one method annotated with @JsonValue on "
                            + beanInfo.getType().getName() + ". Using the first one.");
                } else {
                    valuePropertyInfo = propertyInfo;
                }
                put = false;
            }

            if (propertyInfo.isAnyGetter()) {
                if (null != anyGetterPropertyInfo) {
                    logger.log(TreeLogger.Type.WARN, "More than one method annotated with @JsonAnyGetter on "
                            + beanInfo.getType().getName() + ". Using the first one.");
                } else {
                    anyGetterPropertyInfo = propertyInfo;
                }
                put = false;
            }

            if (propertyInfo.isAnySetter()) {
                if (null != anySetterPropertyInfo) {
                    logger.log(TreeLogger.Type.WARN, "More than one method annotated with @JsonAnySetter on "
                            + beanInfo.getType().getName() + ". Using the first one.");
                } else {
                    anySetterPropertyInfo = propertyInfo;
                }
                put = false;
            }

            if (put) {
                propertiesMap.put(propertyInfo.getPropertyName(), propertyInfo);
            }

        } else {
            logger.log(TreeLogger.Type.DEBUG, "Field " + propertyAccessors.getPropertyName() + " of type "
                    + beanInfo.getType() + " is not visible");
        }
    }

    ImmutableMap.Builder<String, PropertyInfo> result = ImmutableMap.builder();

    // we first add the properties defined in order
    for (String orderedProperty : beanInfo.getPropertyOrderList()) {
        // we remove the entry to have the map with only properties with natural or alphabetic order
        PropertyInfo property = propertiesMap.remove(orderedProperty);
        if (null != property) {
            result.put(property.getPropertyName(), property);
        }
    }

    // if the user asked for an alphabetic order, we sort the rest of the properties
    if (beanInfo.isPropertyOrderAlphabetic()) {

        List<Entry<String, PropertyInfo>> entries = new ArrayList<Entry<String, PropertyInfo>>(
                propertiesMap.entrySet());

        // sorting entries alphabetically on their key
        Lists.sort(entries, Ordering.natural().onResultOf(new Function<Entry<String, PropertyInfo>, String>() {
            @Override
            public String apply(@Nullable Entry<String, PropertyInfo> entry) {
                return null == entry ? null : entry.getKey();
            }
        }));

        for (Map.Entry<String, PropertyInfo> entry : entries) {
            result.put(entry.getKey(), entry.getValue());
        }

    } else {

        // no specified order, we add the rest of the properties in the order we found them
        for (Map.Entry<String, PropertyInfo> entry : propertiesMap.entrySet()) {
            result.put(entry.getKey(), entry.getValue());
        }
    }

    return new PropertiesContainer(result.build(), Optional.fromNullable(valuePropertyInfo),
            Optional.fromNullable(anyGetterPropertyInfo), Optional.fromNullable(anySetterPropertyInfo));
}