BrightSide Workbench Full Report + Source Code
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.file;
19 
20 import java.io.File;
21 import java.util.Comparator;
22 import java.util.Properties;
23 
28 public class FileComparator implements Comparator {
29 
30  private Properties props;
31 
33  public FileComparator(Properties props) {
34  this.props = props;
35  }
36 
49  @Override
50  public int compare(Object o1, Object o2) {
51  File f1 = (File) o1,
52  f2 = (File) o2;
53 
54  String field;
55  boolean desc;
56  int result = 0;
57  for(int i = 0; i < 10 && result == 0; i++) {
58  field = props.getProperty("file.ordering." + i + ".name");
59  desc = "desc".equals(props.getProperty("file.ordering." + i + ".order"));
60  if(result == 0 && "directory".equals(field)) {
61  if(!f1.isDirectory() && f2.isDirectory()) {
62  result = 1;
63  }
64  else if(f1.isDirectory() && !f2.isDirectory()) {
65  result = -1;
66  }
67  else {
68  result = 0;
69  }
70  if(desc) result *= -1;
71  }
72  else if(result == 0 && "fileName".equals(field)) {
73  result = f1.getName().compareTo(f2.getName());
74  if(desc) result *= -1;
75  }
76  else if(result == 0 && "fileDate".equals(field)) {
77  result = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
78  if(desc) result *= -1;
79  }
80  else if(!f1.isDirectory() && !f2.isDirectory() && result == 0 && "fileSize".equals(field)) {
81  result = Long.valueOf(f1.length()).compareTo(f2.length());
82  if(desc) result *= -1;
83  }
84  }
85  return result;
86  }
87 
88 }
FileComparator(Properties props)
int compare(Object o1, Object o2)