Merge "Issue #2726: Add BoundedMap" into development

Former-commit-id: c53d411da1 [formerly 2ed5535235] [formerly 797eb5e8cf [formerly bf775eff602039caa654ab2f810befd3a89e3808]]
Former-commit-id: 797eb5e8cf
Former-commit-id: 450ceb3149
This commit is contained in:
Nate Jensen 2014-04-14 14:07:49 -05:00 committed by Gerrit Code Review
commit b31de05482
2 changed files with 111 additions and 26 deletions

View file

@ -23,9 +23,9 @@ import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.raytheon.uf.common.util.collections.BoundedMap;
/**
* System utilities such as hostname and pid lookup.
@ -44,30 +44,6 @@ import java.util.Map.Entry;
* @version 1.0
*/
public class SystemUtil {
/**
* Map that can be limited to a given number of entries.
*/
private static class BoundedMap<K, V> extends LinkedHashMap<K, V> {
private static final long serialVersionUID = 1L;
private final int maxSize;
public BoundedMap(int maxSize) {
super(16, 0.75f, true);
this.maxSize = maxSize;
}
/*
* (non-Javadoc)
*
* @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry)
*/
@Override
protected boolean removeEldestEntry(Entry<K, V> eldest) {
return size() > maxSize;
}
}
private static final Map<InetAddress, String> hostNameCache = Collections
.synchronizedMap(new BoundedMap<InetAddress, String>(100));

View file

@ -0,0 +1,109 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.uf.common.util.collections;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
/**
* Map implementation that limits the map to a specified number of entries.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Apr 14, 2014 2726 rjpeter Initial creation
*
* </pre>
*
* @author rjpeter
* @version 1.0
* @param <K>
* @param <V>
*/
public class BoundedMap<K, V> extends LinkedHashMap<K, V> {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_INITIAL_SIZE = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private final int maxSize;
/**
* BoundedMap with specified max size. Defaults to accessOrder elimination.
*
* @param maxSize
*/
public BoundedMap(int maxSize) {
this(maxSize, DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR, true);
}
/**
* BoundedMap with specified initial and max size. Defaults to accessOrder
* elimination.
*
* @param maxSize
* @param initialSize
*/
public BoundedMap(int maxSize, int initialSize) {
this(maxSize, initialSize, DEFAULT_LOAD_FACTOR, true);
}
/**
* BoundedMap with specified initial size, max size, and loadFactor.
* Defaults to accessOrder elimination.
*
* @param maxSize
* @param initialSize
* @param loadFactor
*/
public BoundedMap(int maxSize, int initialSize, float loadFactor) {
this(maxSize, initialSize, loadFactor, true);
}
/**
* BoundedMap with specified initial size, max size, loadFactor, and
* accessOrder elimination. If accessOrder is true, map is order by
* accessOrder, false is insertion order.
*
* @param maxSize
* @param initialSize
* @param loadFactor
* @param accessOrder
*/
public BoundedMap(int maxSize, int initialSize, float loadFactor,
boolean accessOrder) {
super(initialSize, loadFactor, accessOrder);
this.maxSize = maxSize;
}
/*
* (non-Javadoc)
*
* @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry)
*/
@Override
protected boolean removeEldestEntry(Entry<K, V> eldest) {
return size() > maxSize;
}
}