package network;

import java.text.DecimalFormat;

/**
 * This class is the model structure. It calculate certain data for Maximum data
 * rate with band width and signal to noise.
 * 
 * 
 * @author 
 * @version 1.8.0 2017/02/11
 */
public class ShannonsModel {

	/**
	 * Provides a value in hertz, required for maximumDataRate Calculation.
	 */
	private double bandwidth;
	/**
	 * Provides a value in decibels, required for maximumDataRate Calculation.
	 */
	private double signalToNoise;

	/**
	 * Creates new ShannonsModel Object.
	 */
	public ShannonsModel() {
		super();
	}

	/**
	 * ShannonsModel constructor with two parameter: Bandwidth and Signal to
	 * noise.
	 * 
	 * @param b
	 *            double value of Bandwidth.
	 * @param s
	 *            double value of Signal to noise.
	 */
	public ShannonsModel(double b, double s) {
		if (b < 0 || s < 0) {

			throw new NumberFormatException();

		}
		this.bandwidth = b;
		this.signalToNoise = s;
	}

	/**
	 * Getter for bandwidth.
	 * 
	 * @return bandwidth
	 */
	public double getBandwidth() {
		if (bandwidth < 0) {

			throw new NumberFormatException();

		}
		return bandwidth;
	}

	/**
	 * Getter for signalToNoise.
	 * 
	 * @return signalToNoise
	 */
	public double getSignalToNoise() {
		if (signalToNoise < 0) {

			throw new NumberFormatException();

		}
		return signalToNoise;
	}

	/**
	 * Setter for bandwidth.
	 * 
	 * @param bandwidth
	 *            Bandwidth
	 */
	public void setBandwidth(double bandwidth) throws NumberFormatException {
		if (bandwidth < 0) {

			throw new NumberFormatException();

		}
		this.bandwidth = bandwidth;
	}

	/**
	 * Setter for signalToNoise.
	 * 
	 * @param snr
	 *            Signal to Noise
	 */
	public void setSignalToNoise(double snr) throws NumberFormatException {

		if (snr < 0) {

			throw new NumberFormatException();

		}
		this.signalToNoise = snr;
	}

	/**
	 * Calls overloaded maximumDataRate(hertz, snr) and returns it's calculated
	 * value.
	 * 
	 * @return maximumDataRate(bandwidth, signalToNoise)
	 */
	public double getMaximumDataRate() {

		return getMaximumDataRate(bandwidth, signalToNoise);

	}

	/**
	 * Calculates the maximum data rate.
	 * 
	 * @param hertz
	 *            Double value of Bandwidth
	 * @param signalToNoise
	 *            Double value of Signal to noise
	 * @return (bandwidth * (Math.log(1 + Math.pow(10, signalToNoise / 10)) /
	 *         Math .log(2))
	 */
	private double getMaximumDataRate(double hertz, double signalToNoise) throws NumberFormatException {
		if (hertz < 0 || signalToNoise < 0) {

			throw new NumberFormatException();

		}
		return (hertz * (Math.log10(1 + (signalToNoise)) / Math.log10(2)));
	}

	/**
	 * Returns information related to the attributes and calculations for
	 * maximumDataRate. maximumDataRate is displayed in a 2 decimal place
	 * rounding format.
	 */
	@Override
	public String toString() {

		return "Bandwidth: " + bandwidth + "    SignalToNoise: " + signalToNoise + "    MDR: "
				+ new DecimalFormat("#.##").format(getMaximumDataRate(bandwidth, signalToNoise));
	}

}// End of the ShannonsModel class.
