Class Pruner


final class Pruner extends MetadataVisitor<Boolean>
Implementation of AbstractMetadata.isEmpty() and AbstractMetadata.prune() methods. The MetadataVisitor.visited map inherited by this class is the thread-local map of metadata objects already tested. Keys are metadata instances, and values are the results of the metadata.isEmpty() operation. If the final operation requested by the user is isEmpty(), then this map will contain one of few false values since the walk in the tree will stop at the first false value found. If the final operation requested by the user is prune(), then this map will contain a mix of false and true values since the operation will unconditionally walk through the entire tree.
Since:
0.3
Version:
1.0
  • Field Details

    • VISITORS

      private static final ThreadLocal<Pruner> VISITORS
      Provider of visitor instances.
    • prune

      private boolean prune
      true for removing empty properties.
    • isEmpty

      private boolean isEmpty
      Whether the metadata is empty.
  • Constructor Details

    • Pruner

      private Pruner()
      Creates a new object which will test or prune metadata properties.
  • Method Details

    • creator

      final ThreadLocal<Pruner> creator()
      Returns the thread-local variable that created this Pruner instance.
      Overrides:
      creator in class MetadataVisitor<Boolean>
    • isEmpty

      static boolean isEmpty(AbstractMetadata metadata, boolean prune)
      Returns true if all properties in the given metadata are null or empty. This method is the entry point for the AbstractMetadata.isEmpty() and AbstractMetadata.prune() public methods.
      Parameters:
      metadata - the metadata object.
      prune - true for deleting empty entries.
      Returns:
      true if all metadata properties are null or empty.
    • preVisit

      Marks a metadata instance as empty before we start visiting its non-null properties. If the metadata does not contain any property, then the isEmpty field will stay true.
      Overrides:
      preVisit in class MetadataVisitor<Boolean>
      Parameters:
      accessor - information about the standard interface and implementation of the metadata being visited.
      Returns:
      MetadataVisitor.Filter.NON_EMPTY since this visitor is not restricted to writable properties. We need to visit all readable properties even for pruning operation since we need to determine if the metadata is empty.
    • visit

      Object visit(Class<?> type, Object value)
      Invoked for each element in the metadata to test or prune. This method is invoked only for new elements not yet processed by Pruner. The element may be a value object or a collection. For convenience we will proceed as if we had only collections, wrapping value object in a singleton collection.
      Specified by:
      visit in class MetadataVisitor<Boolean>
      Parameters:
      type - the type of elements. Note that this is not necessarily the type of given element argument if the latter is a collection.
      value - value of the metadata element being visited.
      Returns:
      the new property value to set, or MetadataVisitor.SKIP_SIBLINGS.
    • result

      Boolean result()
      Returns the result of visiting all elements in the metadata.
      Overrides:
      result in class MetadataVisitor<Boolean>