Example usage for com.google.common.reflect TypeToken hashCode

List of usage examples for com.google.common.reflect TypeToken hashCode

Introduction

In this page you can find the example usage for com.google.common.reflect TypeToken hashCode.

Prototype

@Override
    public int hashCode() 

Source Link

Usage

From source file:net.navatwo.jfxproperties.PropertyObject.java

/**
 * Initialize a {@link PropertyObject}./*from   w  w  w. j a  v a2  s.co m*/
 * <br/>
 * <p>
 * This should only be called from {@link Builder}.
 *
 * @param fields  Fields found in the hierarchy
 * @param methods Methods found in the hierarchy
 */
@SuppressWarnings("unchecked")
PropertyObject(TypeToken<T> base, Collection<? extends PropertyObject<? super T>> supers,
        Set<String> ignoredProperties, Map<String, TypeToken<?>> types, Map<String, Field> fields,
        Map<String, EnumMap<MethodType, Invokable<T, ?>>> methods) {
    this.base = checkNotNull(base, "base == null");
    this.hashCode = base.hashCode();
    this.supers = checkNotNull(supers, "supers == null");

    // Collect all of the properties from the immediate super collectors, these will have been initialized with
    // their super ignored properties as well.
    ImmutableSortedSet.Builder<String> ignoredPropertiesBuilder = ImmutableSortedSet.naturalOrder();
    ignoredPropertiesBuilder.addAll(ignoredProperties);
    supers.stream().flatMap(s -> s.getIgnoredProperties().stream()).forEach(ignoredPropertiesBuilder::add);
    this.ignoredProperties = ignoredPropertiesBuilder.build();

    // now we need to go through and create a mapping of property names to the accessing methods/fields
    // do a union on the keys this gives us all the property names that have been found
    ImmutableMap.Builder<String, PropertyInfo<?>> propertyMapBuilder = ImmutableMap.builder();
    Sets.union(methods.keySet(), fields.keySet()).stream()
            .filter(propName -> !this.ignoredProperties.contains(propName)).forEach(propName -> {
                // Now build the appropriate PropertyInfo<> implementation dependant on the type of the Field.
                // We can use the primitive versions when it will be faster for them to execute later.

                TypeToken<?> propType = types.get(propName).unwrap();

                EnumMap<MethodType, Invokable<T, ?>> mmap = methods.getOrDefault(propName,
                        new EnumMap<>(MethodType.class));

                Field field = fields.get(propName);
                Invokable<T, ? extends ReadOnlyProperty<?>> accessor = (Invokable<T, ? extends ReadOnlyProperty<?>>) mmap
                        .get(MethodType.ACCESSOR);
                Invokable<T, ?> getter = mmap.get(MethodType.GETTER);
                Invokable<T, Void> setter = (Invokable<T, Void>) mmap.get(MethodType.SETTER);

                if (getter != null || setter != null || accessor != null) {

                    PropertyInfoExtractor piExtract = new PropertyInfoExtractor(propName, base, propType, field,
                            getter, setter, accessor);
                    TypeAcceptor.accept(propType, piExtract);

                    propertyMapBuilder.put(propName, piExtract.getPropertyInfo());
                }
            });

    this.localProperties = propertyMapBuilder.build();
    this.thisNames = ImmutableSortedSet.copyOf(localProperties.keySet());

    supers.stream().flatMap(s -> s.getProperties().entrySet().stream())
            .filter(e -> !this.thisNames.contains(e.getKey()))
            .filter(e -> !this.ignoredProperties.contains(e.getKey()))
            .forEach(e -> propertyMapBuilder.put(e.getKey(), e.getValue()));

    // Now create the immutable structures required and store them.
    allProperties = propertyMapBuilder.build();
    allNames = ImmutableSortedSet.copyOf(allProperties.keySet());
}