BrightSide Workbench Full Report + Source Code
Elephant/elephant/src/main/java/org/turro/elephant/documents/FileComparator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero 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  * This program 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 Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.documents;
19 
20 import java.util.Comparator;
21 
26 public class FileComparator implements Comparator {
27 
29  public FileComparator() {
30  }
31 
44  @Override
45  public int compare(Object o1, Object o2) {
46  FileDocument fd1 = (FileDocument) o1,
47  fd2 = (FileDocument) o2;
48 
49  String field;
50  boolean desc;
51  int result = 0;
52  for(int i = 0; i < 10 && result == 0; i++) {
53  field = fd1.getDocumentsRoot().readRootConfig("file.ordering." + i + ".name");
54  desc = "desc".equals(fd1.getDocumentsRoot().readRootConfig("file.ordering." + i + ".order"));
55  if(result == 0 && "directory".equals(field)) {
56  if(!fd1.isDirectory() && fd2.isDirectory()) {
57  result = 1;
58  }
59  else if(fd1.isDirectory() && !fd2.isDirectory()) {
60  result = -1;
61  }
62  else {
63  result = 0;
64  }
65  if(desc) result *= -1;
66  }
67  else if(result == 0 && "fileName".equals(field)) {
68  result = fd1.getName().compareTo(fd2.getName());
69  if(desc) result *= -1;
70  }
71  else if(result == 0 && "fileDate".equals(field)) {
72  result = new Long(fd1.lastModified()).compareTo(fd2.lastModified());
73  if(desc) result *= -1;
74  }
75  else if(!fd1.isDirectory() && !fd2.isDirectory() && result == 0 && "fileSize".equals(field)) {
76  result = new Long(fd1.length()).compareTo(fd2.length());
77  if(desc) result *= -1;
78  }
79  }
80  return result;
81  }
82 
83 }