001package com.hfg.util.io; 002 003import java.util.Comparator; 004 005import com.hfg.datetime.DateSortDirection; 006 007//------------------------------------------------------------------------------ 008/** 009 * Comparator for comparing RemoteFile objects based on their timestamps. 010 * 011 * @author J. Alex Taylor, hairyfatguy.com 012 */ 013//------------------------------------------------------------------------------ 014// com.hfg XML/HTML Coding Library 015// 016// This library is free software; you can redistribute it and/or 017// modify it under the terms of the GNU Lesser General Public 018// License as published by the Free Software Foundation; either 019// version 2.1 of the License, or (at your option) any later version. 020// 021// This library is distributed in the hope that it will be useful, 022// but WITHOUT ANY WARRANTY; without even the implied warranty of 023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024// Lesser General Public License for more details. 025// 026// You should have received a copy of the GNU Lesser General Public 027// License along with this library; if not, write to the Free Software 028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 029// 030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 031// jataylor@hairyfatguy.com 032//------------------------------------------------------------------------------ 033 034 035public class RemoteFileTimestampComparator implements Comparator 036{ 037 038 private DateSortDirection mDirection; 039 040 //########################################################################### 041 // CONSTRUCTORS 042 //########################################################################### 043 044 //--------------------------------------------------------------------------- 045 public RemoteFileTimestampComparator(DateSortDirection inDirection) 046 { 047 mDirection = inDirection; 048 } 049 050 051 052 //########################################################################### 053 // PUBLIC METHODS 054 //########################################################################### 055 056 //--------------------------------------------------------------------------- 057 public int compare(Object o, Object o1) 058 { 059 RemoteFile one = (RemoteFile) o; 060 RemoteFile two = (RemoteFile) o1; 061 062 int returnValue = 0; 063 064 if (one.getTimestamp() != null) 065 { 066 if (two.getTimestamp() != null) 067 { 068 if (one.getTimestamp().getTimeInMillis() > two.getTimestamp().getTimeInMillis()) 069 { 070 returnValue = -1; 071 } 072 else if (one.getTimestamp().getTimeInMillis() < two.getTimestamp().getTimeInMillis()) 073 { 074 returnValue = 1; 075 } 076 } 077 } 078 079 if (mDirection == DateSortDirection.OLDER_TO_NEWER) returnValue = -returnValue; 080 081 return returnValue; 082 } 083 084}