View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.logging;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  /**
23   * Stores log messages, can be sent to log service.
24   */
25  public class LogBuffer implements Serializable {
26  	private static final long serialVersionUID = 1L;
27  	
28  	int maxSize = Integer.MAX_VALUE;
29  	List<LogMessage> buffer = new ArrayList<LogMessage>();
30  	
31  	/**
32  	 * Creates an empty LogBuffer with a size limit of Integer.MAX_VALUE
33  	 */
34  	public LogBuffer() {
35  		
36  	}
37  	
38  	/**
39  	 * Creates an empty LogBuffer with the specified size limit.
40  	 * When size limit is reached, older messages are removed as newer ones are added.
41  	 * @param maxSize
42  	 */
43  	public LogBuffer(int maxSize) {
44  		this.maxSize = maxSize;
45  	}
46  	
47  	/**
48  	 * Adds a message to the buffer
49  	 * @param message
50  	 */
51  	public void add(LogMessage message) {
52  		buffer.add(message);
53  		checkLimit();
54  	}
55  	
56  	/**
57  	 * Returns the underlying log buffer.
58  	 * @return the underlying log buffer as List<LogMessage> 
59  	 */
60  	public List<LogMessage> getLogMessages() {
61  		return buffer;
62  	}
63  	/**
64  	 * Removes excess messages, oldest first.
65  	 */
66  	private void checkLimit() {
67  		while (buffer.size() > maxSize) {
68  			buffer.remove(0);
69  		}
70  	}
71  }