📄 clustermanager.java
字号:
/* * ClusterManager.java * * Created on May 4, 2005, 1:34 PM */package jwsgrid.resourcemanager.priv;import java.util.Vector;import java.util.LinkedHashMap;import java.util.Set;import java.util.Iterator;import java.util.Map;/** * * @author sean */public class ClusterManager { //////////////////////////////////////////////////////////////////////////// // private classes // private class Weight { private int weight = 0; public Weight() { } public Weight( int weight ) { this.weight = weight; } public int getWeight() { return weight; } public void setWeight( int weight ) { this.weight = weight; } } private LinkedHashMap<String,Integer> table = new LinkedHashMap(); private Vector<Vector<Weight>> distMatrix = new Vector(); //////////////////////////////////////////////////////////////////////////// // public methods // /** Creates a new instance of ClusterManager */ public ClusterManager() { } public void addCluster( String name ) { Integer value = table.get( name ); if ( value != null ) { return; } // add a weight to existing vectors in matrix for ( int i = 0; i < distMatrix.size(); i++ ) { distMatrix.get( i ).addElement( new Weight() ); } // insert cluster name and index into table table.put( name, new Integer( distMatrix.size() ) ); // add new weight vector to matrix Vector<Weight> weightVector = new Vector(); for ( int i = 0; i <= distMatrix.size(); i++ ) { weightVector.addElement( new Weight() ); } distMatrix.addElement( weightVector ); } public void setWeight( String srcCluster, String destCluster, int weight ) throws Exception { Integer srcIndex = table.get( srcCluster ); if ( srcIndex == null ) { throw new Exception( "source cluster '" + srcCluster + "' does not exist" ); } Integer destIndex = table.get( destCluster ); if ( destIndex == null ) { throw new Exception( "destination cluster '" + srcCluster + "' does not exist" ); } Vector<Weight> vec = distMatrix.get( srcIndex.intValue() ); vec.get( destIndex.intValue() ).setWeight( weight ); vec = distMatrix.get( destIndex.intValue() ); vec.get( srcIndex.intValue() ).setWeight( weight ); } public int getWeight( String srcCluster, String destCluster ) throws Exception { Integer srcIndex = table.get( srcCluster ); if ( srcIndex == null ) { throw new Exception( "source cluster '" + srcCluster + "' does not exist" ); } Integer destIndex = table.get( destCluster ); if ( destIndex == null ) { throw new Exception( "destination cluster '" + srcCluster + "' does not exist" ); } Vector<Weight> vec = distMatrix.get( srcIndex.intValue() ); return vec.get( destIndex.intValue() ).getWeight(); } public String toString() { String str = ""; Set set = table.entrySet(); Iterator iter = set.iterator(); Vector<String> names = new Vector(); // get names while ( iter.hasNext() ) { Map.Entry entry = (Map.Entry) iter.next(); names.addElement( entry.getKey().toString() ); } // // header // for ( int j = 0; j < 16; j++ ) { str += " "; } for ( int i = 0; i < names.size(); i++ ) { str += format( names.get( i ) ); } str += "\n"; // // weights // for ( int i = 0; i < names.size(); i++ ) { // print cluster name str += format( names.get( i ) ); // print weight Vector<Weight> vec = distMatrix.get( i ); for ( int j = 0; j < vec.size(); j++ ) { String weight = "" + vec.get( j ).getWeight(); str += format( weight ); } str += "\n"; } return str; } public boolean contains( String clusterId ) { if ( table.get( clusterId ) == null ) { return false; } return true; } private String format( String str ) { String copy = ""; int index = str.length(); if ( index > 12 ) { index = 12; } for ( int i = 0; i < index; i++ ) { copy += str.charAt( i ); } for ( int i = index; i < 16; i++ ) { copy += " "; } return copy; } public static void main (String[] args) { try { ClusterManager mgr = new ClusterManager(); mgr.addCluster( "CLUSTER1" ); mgr.addCluster( "CLUSTER2" ); mgr.addCluster( "CLUSTER3" ); mgr.setWeight( "CLUSTER1", "CLUSTER2", 3 ); mgr.setWeight( "CLUSTER1", "CLUSTER3", 1 ); mgr.setWeight( "CLUSTER2", "CLUSTER3", 7 ); System.out.println( mgr.toString() ); System.out.println( mgr.getWeight( "CLUSTER2", "CLUSTER3" )); System.out.println( mgr.getWeight( "CLUSTER3", "CLUSTER3" )); System.out.println( mgr.getWeight( "CLUSTER1", "CLUSTER2" )); } catch ( Exception ex ) { System.out.println( "**error: " + ex.getMessage() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -