PriorizedList.java

  1. package fr.sii.ogham.core.util;

  2. import static java.util.stream.Collectors.toList;

  3. import java.util.ArrayList;
  4. import java.util.List;

  5. /**
  6.  * Helper class that registers objects with associated priority. Each registered
  7.  * object is then returned as list ordered by priority. The higher priority
  8.  * value comes first in the list.
  9.  *
  10.  * @author AurĂ©lien Baudet
  11.  *
  12.  * @param <P>
  13.  *            the type of priorized objects
  14.  */
  15. public class PriorizedList<P> {
  16.     private final List<WithPriority<P>> priorities;

  17.     /**
  18.      * Initializes with an empty list
  19.      */
  20.     public PriorizedList() {
  21.         this(new ArrayList<>());
  22.     }

  23.     /**
  24.      * Initializes with some priorized objects
  25.      *
  26.      * @param priorities
  27.      *            the priorized objects
  28.      */
  29.     public PriorizedList(List<WithPriority<P>> priorities) {
  30.         super();
  31.         this.priorities = priorities;
  32.     }

  33.     /**
  34.      * Registers a new priorized object
  35.      *
  36.      * @param priorized
  37.      *            the wrapped object with its priority
  38.      * @return this instance for fluent chaining
  39.      */
  40.     public PriorizedList<P> register(WithPriority<P> priorized) {
  41.         priorities.add(priorized);
  42.         return this;
  43.     }

  44.     /**
  45.      * Registers an object with its priority
  46.      *
  47.      * @param priorized
  48.      *            the object to register
  49.      * @param priority
  50.      *            the associated priority
  51.      * @return this instance for fluent chaining
  52.      */
  53.     public PriorizedList<P> register(P priorized, int priority) {
  54.         priorities.add(new WithPriority<>(priorized, priority));
  55.         return this;
  56.     }

  57.     /**
  58.      * Merge all priorities of another {@link PriorizedList} into this one.
  59.      *
  60.      * @param other
  61.      *            the priority list
  62.      * @return this isntance for fluent chaining
  63.      */
  64.     public PriorizedList<P> register(PriorizedList<P> other) {
  65.         priorities.addAll(other.getPriorities());
  66.         return this;
  67.     }

  68.     /**
  69.      * Returns true if this list contains no elements.
  70.      *
  71.      * @return if this list contains no elements
  72.      */
  73.     public boolean isEmpty() {
  74.         return priorities.isEmpty();
  75.     }

  76.     /**
  77.      * Get the list of priorities ordered by priority
  78.      *
  79.      * @return ordered list of priorities
  80.      */
  81.     public List<WithPriority<P>> getPriorities() {
  82.         return sort();
  83.     }

  84.     /**
  85.      * Get the list of priorized objects ordered by highest priority.
  86.      *
  87.      * @return list of objects ordered by highet priority
  88.      */
  89.     public List<P> getOrdered() {
  90.         return sort().stream().map(WithPriority::getPriorized).collect(toList());
  91.     }

  92.     private List<WithPriority<P>> sort() {
  93.         priorities.sort(WithPriority.comparator());
  94.         return priorities;
  95.     }
  96. }