1 23 package com.sun.enterprise.diagnostics.util; 24 import java.io.File ; 25 import java.util.Comparator ; 26 import java.util.Date ; 27 import java.text.SimpleDateFormat ; 28 import java.text.ParseException ; 29 30 import com.sun.enterprise.diagnostics.Constants; 31 32 36 public class LogNameComparator implements Comparator { 37 38 private static final SimpleDateFormat dateFormat = 39 new SimpleDateFormat (Constants.DATE_PATTERN); 40 41 public LogNameComparator() { 42 } 43 44 public int compare(Object obj1 , Object obj2) { 45 String name1 = ((File )obj1).getName(); 46 String name2 = ((File )obj2).getName(); 47 48 if (name1 == null || name2 == null) 49 return 0; 50 51 int name1DateBeginIndex = name1.indexOf 53 (Constants.FILENAME_DATE_SEPARATOR) + 1; 54 int name2DateBeginIndex = name2.indexOf 55 (Constants.FILENAME_DATE_SEPARATOR) + 1; 56 57 if (name1DateBeginIndex <= 0) { 59 return 1; 60 } 61 62 if(name2DateBeginIndex <= 0) { 64 return -1; 65 } 66 67 try { 68 Date name1Date = dateFormat.parse 69 (name1.substring 70 (name1DateBeginIndex,name1DateBeginIndex + 71 Constants.ENTRY_DATE_LENGTH)); 72 73 Date name2Date = dateFormat.parse 74 (name2.substring 75 (name2DateBeginIndex,name1DateBeginIndex + 76 Constants.ENTRY_DATE_LENGTH)); 77 78 if (name1Date.after(name2Date)) 79 return 1; 80 else 81 return -1; 82 } catch (ParseException exc) { 83 return 0; 84 } 85 } 86 } 87 | Popular Tags |