Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*     */ package org.jboss.util;
  /*     */ 
  /*     */ import java.util.HashMap;
  /*     */ 
  /*     */ public class LRUCachePolicy
  /*     */   implements CachePolicy
  /*     */ {
  /*     */   protected HashMap m_map;
  /*     */   protected LRUList m_list;
 /*     */   protected int m_maxCapacity;
 /*     */   protected int m_minCapacity;
 /*     */ 
 /*     */   public LRUCachePolicy()
 /*     */   {
 /*     */   }
 /*     */ 
 /*     */   public LRUCachePolicy(int minint max)
 /*     */   {
 /*  74 */     if ((min < 2) || (min > max)) throw new IllegalArgumentException("Illegal cache capacities");
 /*  75 */     this. = min;
 /*  76 */     this. = max;
 /*     */   }
 /*     */ 
 /*     */   public void create()
 /*     */   {
 /*  90 */     this. = new HashMap();
 /*  91 */     this. = createList();
 /*  92 */     this.. = this.;
 /*  93 */     this.. = this.;
 /*  94 */     this.. = this.;
 /*     */   }
 /*     */ 
 /*     */   public void start()
 /*     */   {
 /*     */   }
 /*     */ 
 /*     */   public void stop()
 /*     */   {
 /* 112 */     if (this. != null)
 /*     */     {
 /* 114 */       flush();
 /*     */     }
 /*     */   }
 /*     */ 
 /*     */   public void destroy()
 /*     */   {
 /* 126 */     if (this. != null)
 /* 127 */       this..clear();
 /* 128 */     if (this. != null)
 /* 129 */       this..clear();
 /*     */   }
 /*     */ 
 /*     */   public Object get(Object key)
 /*     */   {
 /* 134 */     if (key == null)
 /*     */     {
 /* 136 */       throw new IllegalArgumentException("Requesting an object using a null key");
 /*     */     }
 /*     */ 
 /* 139 */     LRUCacheEntry value = (LRUCacheEntry)this..get(key);
 /* 140 */     if (value != null)
 /*     */     {
 /* 142 */       this..promote(value);
 /* 143 */       return value.m_object;
 /*     */     }
 /*     */ 
 /* 147 */     cacheMiss();
 /* 148 */     return null;
 /*     */   }
 /*     */ 
 /*     */   public Object peek(Object key)
 /*     */   {
 /* 153 */     if (key == null)
 /*     */     {
 /* 155 */       throw new IllegalArgumentException("Requesting an object using a null key");
 /*     */     }
 /*     */ 
 /* 158 */     LRUCacheEntry value = (LRUCacheEntry)this..get(key);
 /* 159 */     if (value == null)
 /*     */     {
 /* 161 */       return null;
 /*     */     }
 /*     */ 
 /* 165 */     return value.m_object;
 /*     */   }
 /*     */ 
 /*     */   public void insert(Object keyObject o)
 /*     */   {
 /* 170 */     if (o == nullthrow new IllegalArgumentException("Cannot insert a null object in the cache");
 /* 171 */     if (key == nullthrow new IllegalArgumentException("Cannot insert an object in the cache with null key");
 /* 172 */     if (this..containsKey(key))
 /*     */     {
 /* 174 */       throw new IllegalStateException("Attempt to put in the cache an object that is already there");
 /*     */     }
 /* 176 */     this..demote();
 /* 177 */     LRUCacheEntry entry = createCacheEntry(keyo);
 /* 178 */     this..put(keyentry);
 /* 179 */     this..promote(entry);
 /*     */   }
/*     */ 
/*     */   public void remove(Object key) {
/* 183 */     if (key == nullthrow new IllegalArgumentException("Removing an object using a null key");
/*     */ 
/* 185 */     Object value = this..remove(key);
/* 186 */     if (value != null)
/*     */     {
/* 188 */       this..remove((LRUCacheEntry)value);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void flush()
/*     */   {
/* 194 */     LRUCacheEntry entry = null;
/* 195 */     while ((entry = this..) != null)
/*     */     {
/* 197 */       ageOut(entry);
/*     */     }
/*     */   }
/*     */ 
/*     */   public int size() {
/* 202 */     return this..;
/*     */   }
/*     */ 
/*     */   protected LRUList createList()
/*     */   {
/* 213 */     return new LRUList();
/*     */   }
/*     */ 
/*     */   protected void ageOut(LRUCacheEntry entry)
/*     */   {
/* 221 */     remove(entry.m_key);
/*     */   }
/*     */ 
/*     */   protected void cacheMiss()
/*     */   {
/*     */   }
/*     */ 
/*     */   protected LRUCacheEntry createCacheEntry(Object keyObject value)
/*     */   {
/* 234 */     return new LRUCacheEntry(keyvalue);
/*     */   }
/*     */ 
/*     */   public class LRUCacheEntry
/*     */   {
/*     */     public LRUCacheEntry m_next;
/*     */     public LRUCacheEntry m_prev;
/*     */     public Object m_key;
/*     */     public Object m_object;
/*     */     public long m_time;
/*     */ 
/*     */     protected LRUCacheEntry(Object keyObject object)
/*     */     {
/* 463 */       this. = key;
/* 464 */       this. = object;
/* 465 */       this. = null;
/* 466 */       this. = null;
/* 467 */       this. = 0L;
/*     */     }
/*     */ 
/*     */     public String toString() {
/* 471 */       return "key: " + this. + ", object: " + (this. == null ? "null" : Integer.toHexString(this..hashCode())) + ", entry: " + Integer.toHexString(super.hashCode());
/*     */     }
/*     */   }
/*     */ 
/*     */   public class LRUList
/*     */   {
/*     */     public int m_maxCapacity;
/*     */     public int m_minCapacity;
/*     */     public int m_capacity;
/*     */     public int m_count;
/*     */     public LRUCachePolicy.LRUCacheEntry m_head;
/*     */     public LRUCachePolicy.LRUCacheEntry m_tail;
/*     */     public int m_cacheMiss;
/*     */ 
/*     */     protected LRUList()
/*     */     {
/* 264 */       this. = null;
/* 265 */       this. = null;
/* 266 */       this. = 0;
/*     */     }
/*     */ 
/*     */     protected void promote(LRUCachePolicy.LRUCacheEntry entry)
/*     */     {
/* 278 */       if (entry == nullthrow new IllegalArgumentException("Trying to promote a null object");
/* 279 */       if (this. < 1) throw new IllegalStateException("Can't work with capacity < 1");
/*     */ 
/* 281 */       entryPromotion(entry);
/*     */ 
/* 283 */       entry.m_time = System.currentTimeMillis();
/* 284 */       if (entry.m_prev == null)
/*     */       {
/* 286 */         if (entry.m_next == null)
/*     */         {
/* 289 */           if (this. == 0)
/*     */           {
/* 291 */             this. = entry;
/* 292 */             this. = entry;
/* 293 */             this. += 1;
/* 294 */             entryAdded(entry);
/*     */           }
/* 296 */           else if ((this. != 1) || (this. != entry)) {
/* 297 */             if (this. < this.)
/*     */             {
/* 299 */               entry.m_prev = null;
/* 300 */               entry.m_next = this.;
/* 301 */               this.. = entry;
/* 302 */               this. = entry;
/* 303 */               this. += 1;
/* 304 */               entryAdded(entry);
/*     */             }
/* 306 */             else if (this. < this.)
/*     */             {
/* 308 */               entry.m_prev = null;
/* 309 */               entry.m_next = this.;
/* 310 */               this.. = entry;
/* 311 */               this. = entry;
/* 312 */               this. += 1;
/* 313 */               int oldCapacity = this.;
/* 314 */               this. += 1;
/* 315 */               entryAdded(entry);
/* 316 */               capacityChanged(oldCapacity);
/*     */             } else {
/* 318 */               throw new IllegalStateException("Attempt to put a new cache entry on a full cache");
/*     */             }
/*     */           }
/*     */         }
/*     */ 
/*     */       }
/* 324 */       else if (entry.m_next == null)
/*     */       {
/* 326 */         LRUCachePolicy.LRUCacheEntry beforeLast = entry.m_prev;
/* 327 */         beforeLast.m_next = null;
/* 328 */         entry.m_prev = null;
/* 329 */         entry.m_next = this.;
/* 330 */         this.. = entry;
/* 331 */         this. = entry;
/* 332 */         this. = beforeLast;
/*     */       }
/*     */       else
/*     */       {
/* 336 */         LRUCachePolicy.LRUCacheEntry previous = entry.m_prev;
/* 337 */         previous.m_next = entry.m_next;
/* 338 */         entry.m_next.m_prev = previous;
/* 339 */         entry.m_prev = null;
/* 340 */         entry.m_next = this.;
/* 341 */         this.. = entry;
/* 342 */         this. = entry;
/*     */       }
/*     */     }
/*     */ 
/*     */     protected void demote()
/*     */     {
/* 353 */       if (this. < 1) throw new IllegalStateException("Can't work with capacity < 1");
/* 354 */       if (this. > this.throw new IllegalStateException("Cache list entries number (" + this. + ") > than the maximum allowed (" + this. + ")");
/* 355 */       if (this. == this.)
/*     */       {
/* 357 */         LRUCachePolicy.LRUCacheEntry entry = this.;
/*     */ 
/* 360 */         LRUCachePolicy.this.ageOut(entry);
/*     */       }
/*     */     }
/*     */ 
/*     */     protected void remove(LRUCachePolicy.LRUCacheEntry entry)
/*     */     {
/* 369 */       if (entry == nullthrow new IllegalArgumentException("Cannot remove a null entry from the cache");
/* 370 */       if (this. < 1) throw new IllegalStateException("Trying to remove an entry from an empty cache");
/*     */ 
/* 372 */       entry.m_key = (entry.m_object = null);
/* 373 */       if (this. == 1)
/*     */       {
/* 375 */         this. = (this. = null);
/*     */       }
/* 379 */       else if (entry.m_prev == null)
/*     */       {
/* 381 */         this. = entry.m_next;
/* 382 */         this.. = null;
/* 383 */         entry.m_next = null;
/*     */       }
/* 385 */       else if (entry.m_next == null)
/*     */       {
/* 387 */         this. = entry.m_prev;
/* 388 */         this.. = null;
/* 389 */         entry.m_prev = null;
/*     */       }
/*     */       else
/*     */       {
/* 393 */         entry.m_next.m_prev = entry.m_prev;
/* 394 */         entry.m_prev.m_next = entry.m_next;
/* 395 */         entry.m_prev = null;
/* 396 */         entry.m_next = null;
/*     */       }
/*     */ 
/* 399 */       this. -= 1;
/* 400 */       entryRemoved(entry);
/*     */     }
/*     */ 
/*     */     protected void entryPromotion(LRUCachePolicy.LRUCacheEntry entry)
/*     */     {
/*     */     }
/*     */ 
/*     */     protected void entryAdded(LRUCachePolicy.LRUCacheEntry entry)
/*     */     {
/*     */     }
/*     */ 
/*     */     protected void entryRemoved(LRUCachePolicy.LRUCacheEntry entry)
/*     */     {
/*     */     }
/*     */ 
/*     */     protected void capacityChanged(int oldCapacity)
/*     */     {
/*     */     }
/*     */ 
/*     */     protected void clear()
/*     */     {
/* 422 */       LRUCachePolicy.LRUCacheEntry entry = this.;
/* 423 */       this. = null;
/* 424 */       this. = null;
/* 425 */       this. = 0;
/* 426 */       for (; entry != nullentry = entry.m_next)
/* 427 */         entryRemoved(entry);
/*     */     }
/*     */ 
/*     */     public String toString()
/*     */     {
/* 432 */       String s = Integer.toHexString(super.hashCode());
/* 433 */       s = s + " size: " + this.;
/* 434 */       for (LRUCachePolicy.LRUCacheEntry entry = this.entry != nullentry = entry.m_next)
/*     */       {
/* 436 */         s = s + "\n" + entry;
/*     */       }
/* 438 */       return s;
/*     */     }
/*     */   }
/*     */ }
/* Location:           /home/mnovotny/projects/EMBEDDED_JBOSS_BETA3_COMMUNITY/embedded/output/lib/embedded-jboss/lib/jboss-embedded-all.jar
 * Qualified Name:     org.jboss.util.LRUCachePolicy
 * JD-Core Version:    0.6.0
 */
New to GrepCode? Check out our FAQ X