Skip to content

Method: MediaItemComparator()

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:
29: import javax.annotation.Nonnull;
30: import java.util.Comparator;
31: import java.util.List;
32: import it.tidalwave.util.As;
33: import it.tidalwave.util.AsException;
34: import it.tidalwave.util.Finder.InMemorySortCriterion;
35: import it.tidalwave.util.Finder.SortDirection;
36: import it.tidalwave.role.ui.Displayable;
37: import it.tidalwave.bluemarine2.model.MediaItem;
38: import static it.tidalwave.role.ui.Displayable._Displayable_;
39: import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
40:
41: /***********************************************************************************************************************
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: public class MediaItemComparator implements InMemorySortCriterion<As>
47: {
48: private static final long serialVersionUID = 3413093735254009245L;
49:
50: private static final Comparator<As> COMPARATOR = (o1, o2) ->
51: {
52: try
53: {
54: // FIXME: use comparators chaining for lambdas
55: final MediaItem mi1 = o1.as(MediaItem.class);
56: final MediaItem mi2 = o2.as(MediaItem.class);
57: final MediaItem.Metadata m1 = mi1.getMetadata();
58: final MediaItem.Metadata m2 = mi2.getMetadata();
59: final int d1 = m1.get(DISK_NUMBER).orElse(1);
60: final int d2 = m2.get(DISK_NUMBER).orElse(1);
61: final int t1 = m1.get(TRACK_NUMBER).orElse(0);
62: final int t2 = m2.get(TRACK_NUMBER).orElse(0);
63:
64: if (d1 != d2)
65: {
66: return d1 - d2;
67: }
68:
69: if (t1 != t2)
70: {
71: return t1 - t2;
72: }
73: }
74: catch (AsException e) // FIXME: useless?
75: {
76: }
77:
78: return displayNameOf(o1).compareTo(displayNameOf(o2));
79: };
80:
81: private static final InMemorySortCriterion<As> DELEGATE = InMemorySortCriterion.of(COMPARATOR);
82:
83: @Nonnull
84: private static String displayNameOf (@Nonnull final As object)
85: {
86: return object.maybeAs(_Displayable_).map(Displayable::getDisplayName).orElse("???");
87: }
88:
89: @Override
90: public void sort (@Nonnull List<? extends As> results, @Nonnull SortDirection sortDirection)
91: {
92: DELEGATE.sort(results, sortDirection);
93: }
94:
95: @Override @Nonnull
96: public String toString()
97: {
98: return "MediaItemComparator";
99: }
100: }