View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.turbo;
11  
12  import java.util.LinkedHashMap;
13  import java.util.Map;
14  
15  class LRUMessageCache extends LinkedHashMap<String, Integer> {
16  
17    private static final long serialVersionUID = 1L;
18    
19    final int cacheSize;
20    
21    LRUMessageCache(int cacheSize) {
22      super((int) (cacheSize * (4.0f / 3)), 0.75f, true);
23      if (cacheSize < 1) {
24        throw new IllegalArgumentException("Cache size cannnot be smaller than 1");
25      }
26      this.cacheSize = cacheSize;
27    }
28    
29    int getMessageCount(String msg) {
30      Integer i = super.get(msg);
31      if(i == null) {
32        i = 0;
33      } else {
34        i = new Integer(i.intValue()+1);
35      }
36      super.put(msg, i);
37      return i;
38    }
39    
40    protected boolean removeEldestEntry(Map.Entry eldest) {
41      return (size() > cacheSize);
42    }
43  }