1 22 23 package org.meshcms.core; 24 25 import java.io.*; 26 import java.util.*; 27 import org.meshcms.util.*; 28 29 36 public final class PageInfoComparator implements Comparator, Serializable { 37 private SiteMap siteMap; 38 private SiteInfo siteInfo; 39 40 public PageInfoComparator(SiteMap siteMap, SiteInfo siteInfo) { 41 this.siteMap = siteMap; 42 this.siteInfo = siteInfo; 43 } 44 45 48 public int compare(Object o1, Object o2) { 49 try { 50 return compare((PageInfo) o1, (PageInfo) o2); 51 } catch (ClassCastException ex) {} 52 53 return 0; 54 } 55 56 60 public int compare(PageInfo pageInfo1, PageInfo pageInfo2) { 61 Path path1 = pageInfo1.getPath(); 62 Path path2 = pageInfo2.getPath(); 63 64 if (path1.equals(path2)) { return 0; 66 } 67 68 if (path1.isRoot()) { return -1; 70 } 71 72 if (path2.isRoot()) { return 1; 74 } 75 76 Path commonPath = path1.getCommonPath(path2); 77 78 if (commonPath.equals(path1)) { return -1; 80 } 81 82 if (commonPath.equals(path2)) { return 1; 84 } 85 86 Path subPath1 = path1.getPartial(commonPath.getElementCount() + 1); 91 Path subPath2 = path2.getPartial(commonPath.getElementCount() + 1); 92 return compareSameLevel(subPath1, subPath2); 93 } 94 95 private int compareSameLevel(Path subPath1, Path subPath2) { 96 int score1 = siteInfo.getPageScore(subPath1); 98 int score2 = siteInfo.getPageScore(subPath2); 99 100 if (score1 > score2) { 101 return -1; 102 } 103 104 if (score1 < score2) { 105 return 1; 106 } 107 108 PageInfo pageInfo1 = siteMap.getPageInfo(subPath1); 110 PageInfo pageInfo2 = siteMap.getPageInfo(subPath2); 111 112 if (pageInfo1.getTotalHits() > pageInfo2.getTotalHits()) { 113 return -1; 114 } 115 116 if (pageInfo1.getTotalHits() < pageInfo2.getTotalHits()) { 117 return 1; 118 } 119 120 return 0; 121 } 122 } 123 | Popular Tags |