KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > FileLengthComparator


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: FileLengthComparator.java,v 1.6 2007/02/21 06:14:26 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.io.File JavaDoc;
25 import java.util.Comparator JavaDoc;
26
27 /**
28  * Comparator to compare length of files so that the files will be sorted from
29  * the smallest one to the largest one.
30  *
31  * @version $Id: FileLengthComparator.java,v 1.6 2007/02/21 06:14:26 bastafidli Exp $
32  * @author Miro Halas
33  * @code.reviewer Miro Halas
34  * @code.reviewed 1.2 2004/12/18 06:18:25 bastafidli
35  */

36 public class FileLengthComparator implements Comparator JavaDoc
37 {
38    // Cached values ////////////////////////////////////////////////////////////
39

40    /**
41     * Shared comparator instance. Must be named this way to avoid Checkstyle
42     * warning.
43     */

44    private static Comparator JavaDoc s_instance = new FileLengthComparator();
45
46    // Public methods ///////////////////////////////////////////////////////////
47

48    /**
49     * Get shared comparator instance.
50     *
51     * @return FileLengthComparator - shared comparator instance
52     */

53    public static Comparator JavaDoc getInstance(
54    )
55    {
56       return s_instance;
57    }
58
59    /**
60     * Compare lengths two files represented by File Objects.
61     *
62     * @param o1 - File #1
63     * @param o2 - File #2
64     * @return int - -1 if o1 < o2,
65     * 0 if o1 == o2
66     * 1 if o1 > o2
67     */

68    public int compare(
69       Object JavaDoc o1,
70       Object JavaDoc o2
71    )
72    {
73       long lLength1 = ((File JavaDoc)o1).length();
74       long lLength2 = ((File JavaDoc)o2).length();
75
76       if (lLength1 < lLength2)
77       {
78          return -1;
79       }
80       else
81       {
82          if (lLength1 == lLength2)
83          {
84             return 0;
85          }
86          else
87          {
88             return 1;
89          }
90       }
91    }
92 }
93
Popular Tags