MediaItemComparator.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.model.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.Comparator;
  30. import java.util.List;
  31. import it.tidalwave.role.ui.Displayable;
  32. import it.tidalwave.util.As;
  33. import it.tidalwave.util.AsException;
  34. import it.tidalwave.util.Finder.SortDirection;
  35. import it.tidalwave.util.Finder.InMemorySortCriterion;
  36. import it.tidalwave.bluemarine2.model.MediaItem;
  37. import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
  38. import static it.tidalwave.role.ui.Displayable._Displayable_;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. public class MediaItemComparator implements InMemorySortCriterion<As>
  45.   {
  46.     private static final long serialVersionUID = 3413093735254009245L;

  47.     private static final Comparator<As> COMPARATOR = (o1, o2) ->
  48.       {
  49.         try
  50.           {
  51.             // FIXME: use comparators chaining for lambdas
  52.             final MediaItem mi1 = o1.as(MediaItem.class);
  53.             final MediaItem mi2 = o2.as(MediaItem.class);
  54.             final MediaItem.Metadata m1 = mi1.getMetadata();
  55.             final MediaItem.Metadata m2 = mi2.getMetadata();
  56.             final int d1 = m1.get(DISK_NUMBER).orElse(1);
  57.             final int d2 = m2.get(DISK_NUMBER).orElse(1);
  58.             final int t1 = m1.get(TRACK_NUMBER).orElse(0);
  59.             final int t2 = m2.get(TRACK_NUMBER).orElse(0);

  60.             if (d1 != d2)
  61.               {
  62.                 return d1 - d2;
  63.               }

  64.             if (t1 != t2)
  65.               {
  66.                 return t1 - t2;
  67.               }
  68.           }
  69.         catch (AsException e) // FIXME: useless?
  70.           {
  71.           }

  72.         return displayNameOf(o1).compareTo(displayNameOf(o2));
  73.       };

  74.     private static final InMemorySortCriterion<As> DELEGATE = InMemorySortCriterion.of(COMPARATOR);

  75.     @Nonnull
  76.     private static String displayNameOf (@Nonnull final As object)
  77.       {
  78.         return object.maybeAs(_Displayable_).map(Displayable::getDisplayName).orElse("???");
  79.       }

  80.     @Override
  81.     public void sort (@Nonnull List<? extends As> results, @Nonnull SortDirection sortDirection)
  82.       {
  83.         DELEGATE.sort(results, sortDirection);
  84.       }

  85.     @Override @Nonnull
  86.     public String toString()
  87.       {
  88.         return "MediaItemComparator";
  89.       }
  90.   }