1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.util.comparator;
16  
17  import com.liferay.portal.kernel.util.OrderByComparator;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
21  
22  /**
23   * <a href="FileVersionVersionComparator.java.html"><b><i>View Source</i></b>
24   * </a>
25   *
26   * @author Bruno Farache
27   */
28  public class FileVersionVersionComparator extends OrderByComparator {
29  
30      public static String ORDER_BY_ASC = "version ASC";
31  
32      public static String ORDER_BY_DESC = "version DESC";
33  
34      public static String[] ORDER_BY_FIELDS = {"version"};
35  
36      public FileVersionVersionComparator() {
37          this(false);
38      }
39  
40      public FileVersionVersionComparator(boolean asc) {
41          _asc = asc;
42      }
43  
44      public int compare(Object obj1, Object obj2) {
45          DLFileVersion fileVersion1 = (DLFileVersion)obj1;
46          DLFileVersion fileVersion2 = (DLFileVersion)obj2;
47  
48          int value = 0;
49  
50          int[] splitVersion1 = StringUtil.split(
51              fileVersion1.getVersion(), StringPool.PERIOD, 0);
52          int[] splitVersion2 = StringUtil.split(
53              fileVersion2.getVersion(), StringPool.PERIOD, 0);
54  
55          if ((splitVersion1.length != 2) && (splitVersion2.length != 2)) {
56              value = 0;
57          }
58          else if ((splitVersion1.length != 2)) {
59              value = -1;
60          }
61          else if ((splitVersion2.length != 2)) {
62              value = 1;
63          }
64          else if (splitVersion1[0] > splitVersion2[0]) {
65              value = 1;
66          }
67          else if (splitVersion1[0] < splitVersion2[0]) {
68              value = -1;
69          }
70          else if (splitVersion1[1] > splitVersion2[1]) {
71              value = 1;
72          }
73          else if (splitVersion1[1] < splitVersion2[1]) {
74              value = -1;
75          }
76  
77          if (_asc) {
78              return value;
79          }
80          else {
81              return -value;
82          }
83      }
84  
85      public String getOrderBy() {
86          if (_asc) {
87              return ORDER_BY_ASC;
88          }
89          else {
90              return ORDER_BY_DESC;
91          }
92      }
93  
94      public String[] getOrderByFields() {
95          return ORDER_BY_FIELDS;
96      }
97  
98      public boolean isAscending() {
99          return _asc;
100     }
101 
102     private boolean _asc;
103 
104 }