view src/main/java/suikwasha/distributedalgorithm/simulator/Summary.java @ 0:38a110b13db1

added SimpleDistributedAlgorithmFramework. added NaiveAlgorithm added ChangRobertsAlgorithm added PertersonAlgorithm
author suikwasha
date Fri, 19 Oct 2012 00:05:41 +0900
parents
children d24bcb819032
line wrap: on
line source

package suikwasha.distributedalgorithm.simulator;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import suikwasha.distributedalgorithm.framework.MessageChain;

public class Summary
{
	private final AtomicLong messageComplexity;
	private final AtomicLong bitComplexity;
	private final AtomicReference<MessageChain> longestMessageChain;
	
	public Summary()
	{
		messageComplexity = new AtomicLong(0);
		bitComplexity = new AtomicLong(0);
		longestMessageChain = new AtomicReference<MessageChain>(MessageChain.NIL_MESSAGECHAIN);
	}
	
	public void incrementMessageCount()
	{
		messageComplexity.incrementAndGet();
	}
	
	public void addMessageBitCount(long _size)
	{
		bitComplexity.addAndGet(_size);
	}
	
	public long getCurrentMessageComplexity()
	{
		return messageComplexity.get();
	}
	
	public long getCurrentBitComplexity()
	{
		return bitComplexity.get();
	}
	
	public MessageChain getLongestMessageChain()
	{
		return longestMessageChain.get();
	}
	
	public boolean trySetMessageChain(MessageChain _chain)
	{
		MessageChain currentLongestChain;
		do{
			currentLongestChain = longestMessageChain.get();
			if(currentLongestChain.getMessageCount() > _chain.getMessageCount()){
				return false;
			}
		}while(!longestMessageChain.compareAndSet(currentLongestChain,_chain));
		
		return true;
	}
	
	public void print()
	{
		System.out.println("Summary:");
		System.out.println(String.format("MessageComplexity :\t\t%d",messageComplexity.get()));
		System.out.println(String.format("MessageBitComplexity :\t\t%d",messageComplexity.get()));
		System.out.println(String.format("TimeComplexity :\t\t%d",longestMessageChain.get().getMessageCount()));
	}
}