Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  /*
   * Carrot2 project.
   *
   * Copyright (C) 2002-2012, Dawid Weiss, Stanisław Osiński.
   * All rights reserved.
   *
   * Refer to the full license file "carrot2.LICENSE"
   * in the root folder of the repository checkout or at:
  * http://www.carrot2.org/carrot2.LICENSE
  */
 
 package org.carrot2.util.attribute;
 
 import java.util.*;
 
 
Provides a full description of a Bindable type, including AttributeDescriptors for all attributes defined by the type. Also provides some human-readable metadata for the Bindable type itself. BindableDescriptors are immutable.

BindableDescriptors can be obtained from BindableDescriptorBuilder.buildDescriptor(java.lang.Object).

 
 public class BindableDescriptor
 {
    
 
     public enum GroupingMethod
     {
        
Grouping by the java.lang.Class in which the attribute is declared. Attributes defined directly in the top-level BindableDescriptor will be put in BindableDescriptor.attributeDescriptors.

Group key type: java.lang.Class.

 
         STRUCTURE("Declaring class"),

        
Grouping by the AttributeLevel. Attributes without AttributeLevel will be put in BindableDescriptor.attributeDescriptors.

Group key type: AttributeLevel

 
         LEVEL("Attribute level"),

        
Grouping by the "semantic" group assigned to the attribute. Attributes with undefined semantic group will be put in BindableDescriptor.attributeDescriptors.

Group key type: java.lang.String.

 
         GROUP("Attribute semantics"),

        
No grouping, all attributes will be put in BindableDescriptor.attributeDescriptors.

 
         NONE("None");
 
         private final String label;
         
         private GroupingMethod(String label)
         {
             this. = label;
         }
         
         public String toString()
         {
             return ;
         }
     }

    
The method by which this BindableDescriptor is grouped.
 
     public final GroupingMethod groupedBy;

    
The type this BindableDescriptor refers to.
    public final Class<?> type;

    
Prefix of the Bindable this descriptor refers to, as returned by BindableUtils.getPrefix(java.lang.Class).
    public final String prefix;

    
Descriptors without any group assignment. Keys in the map correspond to attribute keys as defined in Attribute.key().

    public final Map<StringAttributeDescriptorattributeDescriptors;

    
Grouped descriptors. By default descriptors come ungrouped BindableDescriptor.GroupingMethod.NONE, to get them grouped, use group(org.carrot2.util.attribute.BindableDescriptor.GroupingMethod). The iterator returns values of this map in the natural order of keys. For the exact type of the key of this map, see BindableDescriptor.GroupingMethod.
    public final Map<ObjectMap<StringAttributeDescriptor>> attributeGroups;

    
Internal descriptors for attributes defined directly in the type this BindableDescriptor refers to.
Internal descriptors for other Bindable types referenced by this descriptor. Keys in this map correspond to fields that hold the references.
Human-readable metadata about this Bindable type.
    public final BindableMetadata metadata;

    
An internal constructor.
    BindableDescriptor(Class<?> bindableTypeBindableMetadata metadata,
        Map<FieldBindableDescriptorbindableDescriptors,
        Map<StringAttributeDescriptorattributeDescriptors)
    {
        this(bindableTypemetadatabindableDescriptorsattributeDescriptors,
            .);
    }

    
An internal constructor.
    BindableDescriptor(Class<?> bindableTypeBindableMetadata metadata,
        Map<FieldBindableDescriptorbindableDescriptors,
        Map<StringAttributeDescriptorattributeDescriptorsGroupingMethod groupBy)
    {
        this. = bindableType;
        this. = BindableUtils.getPrefix(bindableType);
        this. = metadata;
        this. = bindableDescriptors;
        this. = attributeDescriptors;
        this. = groupBy;
        final LinkedHashMap<StringAttributeDescriptornewAttributeDescriptors = Maps
            .newLinkedHashMap();
        final LinkedHashMap<ObjectMap<StringAttributeDescriptor>> newAttributeGroups = Maps
            .newLinkedHashMap();
        buildAttributeGroups(newAttributeDescriptorsnewAttributeGroupsthis);
        this. = Collections.unmodifiableMap(newAttributeDescriptors);
        this. = Collections.unmodifiableMap(newAttributeGroups);
    }

    
Preserves attribute descriptors for which the provided predicate returns true. Notice that BindableDescriptors are immutable, so the filtered descriptor set is returned rather than filtering being applied to the receiver.

Parameters:
predicate predicate to the applied
Returns:
a new BindableDescriptor with the descriptors filtered.
    {
        final Map<StringAttributeDescriptorfilteredAttributeDescriptors = Maps
            .newLinkedHashMap();
        outer: for (final Map.Entry<StringAttributeDescriptorentry : 
            .entrySet())
        {
            final AttributeDescriptor descriptor = entry.getValue();
            if (!predicate.apply(descriptor))
            {
                continue outer;
            }
            filteredAttributeDescriptors.put(entry.getKey(), descriptor);
        }
        // Now recursively filter bindable descriptors
        final Map<FieldBindableDescriptorfilteredBindableDescriptors = Maps
            .newLinkedHashMap();
        for (final Map.Entry<FieldBindableDescriptorentry : 
            .entrySet())
        {
            filteredBindableDescriptors.put(entry.getKey(), entry.getValue().only(
                predicate));
        }
        return new BindableDescriptor(this.this.,
            filteredBindableDescriptorsfilteredAttributeDescriptorsthis.);
    }

    
Preserves descriptors that have all of the specified annotations. Notice that BindableDescriptors are immutable, so the filtered descriptor set is returned rather than filtering being applied to the receiver.

Parameters:
annotationClasses binding time and direction annotation classes to be matched.
Returns:
a new BindableDescriptor with the descriptors filtered.
    public BindableDescriptor only(final Class<? extends Annotation>... annotationClasses)
    {
        if (annotationClasses.length == 0)
        {
            return this;
        }
        return only(new AnnotationsPredicate(trueannotationClasses));
    }

    
Preserves descriptors that have none of the specified annotations. Notice that BindableDescriptors are immutable, so the filtered descriptor set is returned rather than filtering being applied to the receiver.

Parameters:
annotationClasses binding time and direction annotation classes to be matched.
Returns:
a new BindableDescriptor with the descriptors filtered.
    public BindableDescriptor not(final Class<? extends Annotation>... annotationClasses)
    {
        if (annotationClasses.length == 0)
        {
            return this;
        }
        return only(Predicates.<AttributeDescriptornot(new AnnotationsPredicate(false
            annotationClasses)));
    }

    
Returns a flattened structure of attribute descriptors. After flattening, attributeDescriptors contains descriptors of all attributes and attributeGroups is empty. Notice that BindableDescriptors are immutable, so the flattened descriptor set is returned rather than flattening being applied to the receiver.

Returns:
flattened descriptor
    public BindableDescriptor flatten()
    {
        return group(.);
    }

    
Returns a grouped structure of attribute descriptors. Notice that BindableDescriptors are immutable, so the grouped descriptor set is returned rather than grouping being applied to the receiver.

Parameters:
groupingMethod the grouping method to be used
Returns:
grouped descriptors
    public BindableDescriptor group(GroupingMethod groupingMethod)
    {
        if (this..equals(groupingMethod))
        {
            return this;
        }
        else
        {
            return new BindableDescriptor(this.this.,
                this.this.,
                groupingMethod);
        }
    }

    
Returns a map of default values of attributes associated with this bindable. A shortcut for:
 for (AttributeDescriptor d : only(Input.class).flatten().attributeDescriptors.values())
 {
     if (d.defaultValue != null || d.requiredAttribute)
        values.put(d.key, d.defaultValue);
 }
 
    @SuppressWarnings("unchecked")
    {
        final HashMap<StringObjectvalues = Maps.newHashMap();
        for (AttributeDescriptor d : only(Input.class).flatten()..values())
        {
            if (d.defaultValue != null || d.requiredAttribute)
            {
                values.put(d.keyd.defaultValue);
            }
        }
        return values;
    }

    
Internal interface for extracting grouping key values.
    static interface IGrouper<T>
    {
        T getGroupingObject(BindableDescriptor bindableDescriptor,
            AttributeDescriptor attributeDescriptor);
    }
        "unchecked""rawtypes"
    })
    static void buildAttributeGroups(
        Map<StringAttributeDescriptornewAttributeDescriptors,
        Map<ObjectMap<StringAttributeDescriptor>> newAttributeGroups,
        BindableDescriptor bindableDescriptorGroupingMethod groupingMethod)
    {
        final Map<ObjectMap<StringAttributeDescriptor>> unsortedGroups = Maps
            .newHashMap();
        final List sortedGroupKeys;
        if (..equals(groupingMethod))
        {
            addGroups(bindableDescriptornewAttributeDescriptorsunsortedGroups,
                );
            sortedGroupKeys = Lists.newArrayList(unsortedGroups.keySet());
            Collections.sort(sortedGroupKeys.);
        }
        else if (..equals(groupingMethod))
        {
            addGroups(bindableDescriptornewAttributeDescriptorsunsortedGroups,
                );
            sortedGroupKeys = Lists.newArrayList(unsortedGroups.keySet());
            Collections.sort(sortedGroupKeys);
        }
        else if (..equals(groupingMethod))
        {
            addGroups(bindableDescriptornewAttributeDescriptorsunsortedGroups,
                );
            sortedGroupKeys = Lists.newArrayList(unsortedGroups.keySet());
            Collections.sort(sortedGroupKeys);
        }
        else if (..equals(groupingMethod))
        {
            addGroups(bindableDescriptornewAttributeDescriptorsunsortedGroups,
                );
            sortedGroupKeys = .;
        }
        else
        {
            throw new IllegalArgumentException("Unknown grouping method: "
                + groupingMethod);
        }
        // Add ordered
        for (Object object : sortedGroupKeys)
        {
            newAttributeGroups.put(objectunsortedGroups.get(object));
        }
    }
    static IGrouper<Class<?>> GROUPER_BY_STRUCTURE = new IGrouper<Class<?>>()
    {
        public Class<?> getGroupingObject(BindableDescriptor bindableDescriptor,
            AttributeDescriptor attributeDescriptor)
        {
            return attributeDescriptor.attributeField.getDeclaringClass();
        }
    };
    {
        public AttributeLevel getGroupingObject(BindableDescriptor bindableDescriptor,
            AttributeDescriptor attributeDescriptor)
        {
            if (attributeDescriptor.metadata != null)
            {
                return attributeDescriptor.metadata.getLevel();
            }
            else
            {
                return null;
            }
        }
    };
    static IGrouper<StringGROUPER_BY_GROUP = new IGrouper<String>()
    {
        public String getGroupingObject(BindableDescriptor bindableDescriptor,
            AttributeDescriptor attributeDescriptor)
        {
            if (attributeDescriptor.metadata != null)
            {
                return attributeDescriptor.metadata.getGroup();
            }
            else
            {
                return null;
            }
        }
    };
    static IGrouper<StringGROUPER_BY_NONE = new IGrouper<String>()
    {
        public String getGroupingObject(BindableDescriptor bindableDescriptor,
            AttributeDescriptor attributeDescriptor)
        {
            return null;
        }
    };
    private static <T> void addGroups(BindableDescriptor sourceBindableDescriptor,
        Map<StringAttributeDescriptornewAttributeDescriptors,
        Map<ObjectMap<StringAttributeDescriptor>> newAttributeGroups,
        IGrouper<T> grouper)
    {
        // Run through direct attribute descriptors first
        for (AttributeDescriptor attributeDescriptor1 : sourceBindableDescriptor.attributeDescriptorsInternal
            .values())
        {
            addToMaps(newAttributeDescriptorsnewAttributeGroupsattributeDescriptor1,
                grouper.getGroupingObject(sourceBindableDescriptorattributeDescriptor1));
        }
        // Recursively run through nested attribute descriptors
        for (Entry<FieldBindableDescriptorentry : sourceBindableDescriptor.bindableDescriptorsInternal
            .entrySet())
        {
            addGroups(entry.getValue(), newAttributeDescriptorsnewAttributeGroups,
                grouper);
        }
    }
    private static <T> void addToMaps(
        Map<StringAttributeDescriptornewAttributeDescriptors,
        Map<ObjectMap<StringAttributeDescriptor>> groups,
        AttributeDescriptor attributeDescriptorfinal T groupingObject)
    {
        if (groupingObject == null)
        {
            newAttributeDescriptors.put(attributeDescriptor.keyattributeDescriptor);
        }
        else
        {
            Map<StringAttributeDescriptormap = groups.get(groupingObject);
            if (map == null)
            {
                map = Maps.newLinkedHashMap();
                groups.put(groupingObjectmap);
            }
            map.put(attributeDescriptor.keyattributeDescriptor);
        }
    }
    private static class ClassComparator implements Comparator<Class<?>>
    {
        public static final ClassComparator INSTANCE = new ClassComparator();
        public int compare(Class<?> o1Class<?> o2)
        {
            return o1.getSimpleName().compareTo(o2.getSimpleName());
        }
    }
New to GrepCode? Check out our FAQ X