java.lang.Object
com.blackhillsoftware.smf.summary.Top<T>
Type Parameters:
T - The type of values to be processed.
All Implemented Interfaces:
Iterable<T>

public class Top<T> extends Object implements Iterable<T>
Collect the top values for a specific type, as determined by a supplied Comparator function. This class is designed to handle sequences of millions of entries or more. It only keeps a reference to the largest values seen so far, rather than the entire list.

You can create an instance of the class to add values one at a time, or use it to collect values from a Stream.

Process values one at a time:

 
 try (SmfRecordReader reader = SmfRecordReader.fromName("//DD:INPUT")
             .include(30, 5)) 
 {
     Top<Smf30Record> topCpu =
         new Top<>(10,
             Comparator.comparing(r30 -> r30.processorAccountingSection().smf30cpt())
         );

     for (SmfRecord record : reader)
     {
         Smf30Record r30 = Smf30Record.from(record);
         if (r30.processorAccountingSection() != null)
         {
             topCpu.add(r30);	
         }	
     }

     for (Smf30Record r30 : topCpu)
     {						
         System.out.format("%-24s %-8s %-8s %6.2f%n", 
             r30.smfDateTime(),
             r30.identificationSection().smf30jbn(),
             r30.identificationSection().smf30jnm(),
             r30.processorAccountingSection().smf30cptSeconds());	
     }
 }
 
 

Collect values from a Stream:

 
 try (SmfRecordReader reader = SmfRecordReader.fromName("//DD:INPUT")
             .include(30, 5)) 
 {
     List<Smf30Record> topCpu = reader.stream()
         .map(record -> Smf30Record.from(record))
         .filter(r30 -> r30.processorAccountingSection() != null)
         .collect(Top.values(10, 
             Comparator.comparing(r30 -> r30.processorAccountingSection().smf30cpt())
     ));

     for (Smf30Record r30 : topCpu)
     {						
         System.out.format("%-24s %-8s %-8s %6.2f%n", 
             r30.smfDateTime(),
             r30.identificationSection().smf30jbn(),
             r30.identificationSection().smf30jnm(),
             r30.processorAccountingSection().smf30cptSeconds());	
     }
 }
 
 
  • Constructor Details

    • Top

      public Top(int n, Comparator<? super T> comparator)
      Create an instance to find the top n values in a sequence of values (the largest according to the supplied comparator)
      Parameters:
      n - the number of values to keep, ie top n
      comparator - the comparison function to use to compare values.
  • Method Details

    • values

      public static <T> Collector<T,?,List<T>> values(int n, Comparator<? super T> comparator)
      Collect the top values from a Stream (the largest according to the supplied comparator). This collector is designed to work with streams containing millions of entries.
      Type Parameters:
      T - the type of value
      Parameters:
      n - the number of values to collect
      comparator - the comparison function to use to compare values.
      Returns:
      the top value collector
    • add

      public void add(T value)
      Add a new value. It will be retained or discarded depending on whether it is larger than other values according to the comparator.
      Parameters:
      value - the value to add
    • addAll

      public boolean addAll(Collection<? extends T> collection)
      Add all entries from a collection.
      Parameters:
      collection - the source collection
      Returns:
      true if values were processed
    • toList

      public List<T> toList()
      Get the values as a List. The returned list will be in the order largest to smallest, according to the supplied comparator.
      Returns:
    • iterator

      public Iterator<T> iterator()
      Get an iterator for the values. The iterator will return values in the order largest to smallest, according to the supplied comparator.
      Specified by:
      iterator in interface Iterable<T>