1 // 2 // GenericItem.java 3 // CocoDonkey 4 // $Id: GenericItem.java,v 1.8 2002/07/22 00:30:22 fortun Exp $ 5 // 6 // Created by Fred Bonzoun on Sun May 12 2002. 7 // Copyright (c) 2002 Bonzoun. All rights reserved. 8 // 9 // This library is free software; you can redistribute it and/or modify 10 // it under the terms of the GNU Lesser General Public License as published 11 // by the Free Software Foundation; either version 2.1 of the License, or 12 // (at your option) any later version. 13 // 14 // This library is distributed in the hope that it will be useful, 15 // but WITHOUT ANY WARRANTY; without even the implied warranty of 16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 // GNU Lesser General Public License for more details. 18 // 19 // You should have received a copy of the GNU Lesser General Public License 20 // along with this program; if not, write to the Free Software 21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 // 23 24 package net.bonzoun.cocodonkey; 25 26 import com.apple.cocoa.foundation; 27 import java.util; 28 29 public abstract class GenericItem { 30 31 protected int id; 32 protected String name; 33 protected int size; 34 protected String hash; 35 protected int nbClients; 36 37 protected boolean complete; 38 39 private static final PrintfFormat pfInt = new PrintfFormat("%02d"); 40 private static final PrintfFormat pfFloat = new PrintfFormat("%3.1f"); 41 42 private static final int SEC_PER_DAY = 86400; 43 private static final int SEC_PER_HOUR = 3600; 44 private static final int SEC_PER_MIN = 60; 45 46 /*package*/ GenericItem(int id, String name) { 47 this.id = id; 48 this.name = name; 49 complete = false; 50 } 51 52 public void setNbClients(int nb) { 53 this.nbClients=nb; 54 } 55 56 public final int id() { 57 return id; 58 } 59 60 public final String name() { 61 return name; 62 } 63 64 public final int size() { 65 return size; 66 } 67 68 public final String prettySize() { 69 return displaySize(size); 70 } 71 72 public final int nbClients() { 73 return nbClients; 74 } 75 76 public final String hash() { 77 return hash; 78 } 79 80 public static boolean sameList(List list1, List list2) { 81 if (list1.size()!=list2.size()) 82 return false; 83 for(int i=0; i<list1.size(); i++) { 84 GenericItem item1 = (GenericItem)list1.get(i); 85 GenericItem item2 = (GenericItem)list2.get(i); 86 if (!item1.equals(item2)) 87 return false; 88 } 89 return true; 90 } 91 92 public boolean equals(Object o) { 93 if (!(o instanceof GenericItem)) 94 return false; 95 GenericItem item = (GenericItem)o; 96 if (id()!=item.id()) 97 return false; 98 if ((name()==null && item.name!=null) || (name()!=null && !name().equals(item.name()))) 99 return false; 100 if (size()!=item.size()) 101 return false; 102 if ((hash()==null && item.hash!=null) || (hash()!=null && !hash().equals(item.hash()))) 103 return false; 104 return true; 105 } 106 107 public static abstract class MyComparator implements java.util.Comparator { 108 public boolean equals(Object o) { 109 return equals(o); 110 } 111 } 112 113 public static class CompareName extends MyComparator { 114 public final int compare(Object o1, Object o2) { 115 GenericItem item1 = (GenericItem)o1; 116 GenericItem item2 = (GenericItem)o2; 117 if (item1.name()!=null) 118 return item1.name().compareToIgnoreCase(item2.name()); 119 else 120 return 1; 121 } 122 } 123 124 public static class CompareSize extends MyComparator { 125 public final int compare(Object o1, Object o2) { 126 GenericItem item1 = (GenericItem)o1; 127 GenericItem item2 = (GenericItem)o2; 128 if (item1.size()<item2.size()) 129 return -1; 130 else if (item1.size()==item2.size()) 131 return 0; 132 else 133 return 1; 134 } 135 } 136 137 public static class CompareNbClients extends MyComparator { 138 public final int compare(Object o1, Object o2) { 139 GenericItem item1 = (GenericItem)o1; 140 GenericItem item2 = (GenericItem)o2; 141 if (item1.nbClients()<item2.nbClients()) 142 return -1; 143 else if (item1.nbClients()==item2.nbClients()) 144 return 0; 145 else 146 return 1; 147 } 148 } 149 150 public static String displaySize(int n) { 151 if (n<2048) 152 return "" + n + " B"; 153 float f = (float)n; 154 f /= 1024; 155 if (f<2048) 156 return "" + (Math.round(f*10.)/10.) + " kB"; 157 f /= 1024; 158 if (f<2048) 159 return "" + (Math.round(f*10.)/10.) + " MB"; 160 f /= 1024; 161 return "" + (Math.round(f*10.)/10.) + " GB"; 162 } 163 164 /** Tries to display an approximation of 10%, always between 5% and 20% **/ 165 public static String displayTime(int n) { 166 if (n < 0) { 167 return "Unknown"; 168 } else if (n < 50) { 169 // Below 50s: 1s, 2s, ... up from 2% 170 return "~ " + n + " s"; 171 } else if (n <= 5*SEC_PER_MIN) { 172 // From 50s to 5m: 1m 10s, 1m 20s, ... down from 20% to 3.3% 173 int min = n / SEC_PER_MIN; 174 int sec = ((n%SEC_PER_MIN) / 10) * 10; 175 if (sec>0) 176 return "" + min + " m " + sec + " s"; 177 else 178 return "" + min + " m"; 179 } else if (n <= 20*SEC_PER_MIN) { 180 // From 5 to 20 minutes: 1m, 2m... down from 20% to 5% 181 int min = (n+SEC_PER_MIN/2) / SEC_PER_MIN; 182 return "~ " + min + " m"; 183 } else if (n <= SEC_PER_HOUR) { 184 // From 20 minutes to 1 hour : 5m; 10m, ... down from 25% to 8.4% 185 int min = ((n+SEC_PER_MIN/2) / SEC_PER_MIN / 5) * 5; 186 return "~ " + min + " m"; 187 } else if (n < 5*SEC_PER_HOUR) { 188 // From 1 to 5 hour: 1h 10m, 1h 20m, ... down from 17% to 3.3% 189 int hours = n / SEC_PER_HOUR; 190 int min = (((n%SEC_PER_HOUR)+SEC_PER_MIN/2) / SEC_PER_MIN / 10) * 10; 191 if (min>0) 192 return "" + hours + " h " + min + " m"; 193 else 194 return "" + hours + " h"; 195 } else if (n <= SEC_PER_DAY) { 196 // From 5 to 24 hours: 1h, 2h, ... down from 20% to 4% 197 int hours = (n+SEC_PER_HOUR/2) / SEC_PER_HOUR; 198 return "" + hours + " h"; 199 } else if (n < 5*SEC_PER_DAY) { 200 // From 1 to 5 days: 2d 4h, 2d 8h, ... down from 17% up to 3.3% 201 int days = n / SEC_PER_DAY; 202 int hours = (((n%SEC_PER_DAY)+SEC_PER_HOUR/2) / SEC_PER_HOUR / 4) * 4; 203 if (hours>0) 204 return "" + days + " d " + hours + " h"; 205 else 206 return "" + days + " d"; 207 } else { 208 // Above 5 days: 5 d, 6 d, ... down from 20% to 0% 209 int days = (n+SEC_PER_DAY/2) / SEC_PER_DAY; 210 return "" + days + " d"; 211 } 212 } 213 } 214 215 // $Log: GenericItem.java,v $ 216 // Revision 1.8 2002/07/22 00:30:22 fortun 217 // NSArrays are now Lists 218 // 219 // Revision 1.7 2002/07/08 22:01:09 fortun 220 // Bug in displayTime if second term is 0 (displayed but should not) 221 // 222 // Revision 1.6 2002/07/07 12:43:20 fortun 223 // Sorting is now case insensitive 224 // 225 // Revision 1.5 2002/06/09 22:45:29 fortun 226 // Finer algorithm for displayTime() 227 // 228 // Revision 1.4 2002/06/09 15:44:03 fortun 229 // Simplified display for the timeLeft() 230 // 231 // Revision 1.3 2002/06/08 12:20:59 fortun 232 // Estimation of the time remaining 233 // 234 // Revision 1.2 2002/05/27 22:08:30 fortun 235 // Added some comparators 236 // Added an 'equal' method 237 // Added the 'sameList' method to compare two lists of GenericItems 238 // 239 // Revision 1.1.1.1 2002/05/21 21:26:13 fortun 240 // 241 //