maddressmanager.java
来自「httptunnel.jar httptunnel java 源码」· Java 代码 · 共 100 行
JAVA
100 行
package net.jumperz.app.MGuardian;
import java.util.*;
import net.jumperz.util.MCount;
public final class MAddressManager
{
private static MAddressManager instance = new MAddressManager();
private static final int INIT_MAP_SIZE = 200;
private static final int INIT_MAX_COUNT = 30;
private int maxCount = INIT_MAX_COUNT;
private Map addressCountMap;
private Set allowedAddressSet;
//---------------------------------------------------------------------------------------
private MAddressManager()
{
// Singleton
addressCountMap = new HashMap( INIT_MAP_SIZE );
}
//---------------------------------------------------------------------------------------
public static MAddressManager getInstance()
{
return instance;
}
//---------------------------------------------------------------------------------------
public void setMaxCount( int in_maxCount )
{
maxCount = in_maxCount;
}
//---------------------------------------------------------------------------------------
public synchronized boolean isFull( String address )
{
// if address is allowed, always return false
if( allowedAddressSet.contains( address ) )
{
return false;
}
if( addressCountMap.containsKey( address ) )
{
MCount count = ( MCount )addressCountMap.get( address );
if( count.getValue() == maxCount )
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
//---------------------------------------------------------------------------------------
public synchronized void add( String address )
{
if( addressCountMap.containsKey( address ) )
{
MCount count = ( MCount )addressCountMap.get( address );
count.inc();
}
else
{
addressCountMap.put( address, new MCount() );
}
}
//---------------------------------------------------------------------------------------
public synchronized void remove( String address )
{
if( !addressCountMap.containsKey( address ) )
{
Exception e = new Exception( "Should not happen. address not found." );
e.printStackTrace();
return;
}
MCount count = ( MCount )addressCountMap.get( address );
count.dec();
if( count.getValue() == 0 )
{
addressCountMap.remove( address );
}
}
//---------------------------------------------------------------------------------------
public synchronized void setAllowedAddress( Set in_allowedAddressSet )
{
allowedAddressSet = in_allowedAddressSet;
}
//---------------------------------------------------------------------------------------
public void print()
{
System.out.println( addressCountMap );
}
//---------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?