using System;
using System.Collections.Generic;
using System.Text;
namespace ScrewTurn.Wiki.SearchEngine {
///
/// Represents the relevance of a search result with two states: non-finalized and finalized.
/// When the state is non-finalized, the value of the relevance has an unknown meaning. When the state
/// is finalized, the value of the relevance is a percentage value representing the relevance of a search result.
///
/// All members are not thread-safe.
public class Relevance {
///
/// The value of the relevance.
///
protected float value;
///
/// A value indicating whether the relevance value is finalized.
///
protected bool isFinalized;
///
/// Initializes a new instance of the class.
///
public Relevance()
: this(0) { }
///
/// Initializes a new instance of the class.
///
/// The initial relevance value, non-finalized.
/// If is less than zero.
public Relevance(float value) {
if(value < 0) throw new ArgumentOutOfRangeException("value", "Value must be greater than or equal to zero");
this.value = value;
this.isFinalized = false;
}
///
/// Sets the non-finalized value of the relevance.
///
/// The value.
/// If is less than zero.
/// If is true ( was called).
public void SetValue(float value) {
if(value < 0) throw new ArgumentOutOfRangeException("value", "Value must be greater than or equal to zero");
if(isFinalized) throw new InvalidOperationException("After finalization, the value cannot be changed anymore");
this.value = value;
}
///
/// Finalizes the value of the relevance.
///
/// The global relevance value.
/// The method sets the finalized value of the relevance to value / total * 100.
/// If is less than zero.
/// If is true ( was called).
public void Finalize(float total) {
if(total < 0) throw new ArgumentOutOfRangeException("total", "Total must be greater than or equal to zero");
if(isFinalized) throw new InvalidOperationException("Finalization already performed");
value = value / total * 100;
isFinalized = true;
}
///
/// Normalizes the relevance after finalization.
///
/// The normalization factor.
/// If is false ( was not called).
public void NormalizeAfterFinalization(float factor) {
if(factor < 0) throw new ArgumentOutOfRangeException("factor", "Factor must be greater than or equal to zero");
if(!isFinalized) throw new InvalidOperationException("Normalization can be performed only after finalization");
value = value * factor;
}
///
/// Gets a value indicating whether the relevance value is finalized.
///
public bool IsFinalized {
get { return isFinalized; }
}
///
/// Gets the value of the relevance.
///
public float Value {
get { return value; }
}
}
}