View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.restapi.v1.utils;
20  
21  import java.util.Comparator;
22  import java.util.Locale;
23  
24  import org.mycore.datamodel.common.MCRObjectIDDate;
25  import org.mycore.restapi.v1.utils.MCRRestAPISortObject.SortOrder;
26  
27  /**
28   * Comparator to sort a collection of result objects by id or date
29   * 
30   * ToDo: Check if this can be replaced with SOLR functionality
31   * 
32   * @author Robert Stephan
33   * 
34   * @version $Revision: $ $Date: $
35   */
36  public class MCRRestAPISortObjectComparator implements Comparator<MCRObjectIDDate> {
37      private MCRRestAPISortObject sortObj = null;
38  
39      public MCRRestAPISortObjectComparator(MCRRestAPISortObject sortObj) {
40          this.sortObj = sortObj;
41      }
42  
43      @Override
44      public int compare(MCRObjectIDDate o1, MCRObjectIDDate o2) {
45          if ("id".equals(sortObj.getField().toLowerCase(Locale.ROOT))) {
46              if (sortObj.getOrder() == SortOrder.ASC) {
47                  return o1.getId().compareTo(o2.getId());
48              }
49              if (sortObj.getOrder() == SortOrder.DESC) {
50                  return o2.getId().compareTo(o1.getId());
51              }
52          }
53          if ("lastmodified".equals(sortObj.getField().toLowerCase(Locale.ROOT))) {
54              if (sortObj.getOrder() == SortOrder.ASC) {
55                  return o1.getLastModified().compareTo(o2.getLastModified());
56              }
57              if (sortObj.getOrder() == SortOrder.DESC) {
58                  return o2.getLastModified().compareTo(o1.getLastModified());
59              }
60          }
61          return 0;
62      }
63  }