package edu.vt.marian.search; import java.io.*; import java.util.*; import edu.vt.marian.common.*; /** A Weighted Object Set with only one element.
JDK Version : 1.1.5
@author Priyamvadha Lakshminarayanan
@author Prajakta Joshi
@see WtdObjSet
@see edu.vt.marian.common.WtdObj
*/
public class SingletonWtdObjSet implements WtdObjSet
{
protected Debug debug;
/** The single element in this set.
*/
protected WtdObj onlyElt;
/**
A WtdObjSetEnumeration for a SingletonWtdObjSet.
*/
public class Enum implements WtdObjSetEnumeration
{
boolean eltPassed = false;
public Enum()
{
}
public synchronized Object nextElement() throws NoSuchElementException
{
if ( eltPassed )
throw(new NoSuchElementException() );
else
{
eltPassed = true;
return( new WtdObj(onlyElt, debug) );
}
}
public boolean hasMoreElements()
{
return( ! eltPassed );
}
public synchronized void skip(int k)
{
if ( k > 1)
throw( new NoSuchElementException() );
if ( k == 1 )
{
if ( eltPassed )
throw( new NoSuchElementException() );
eltPassed = true;
}
}
public synchronized int sample(int num, WtdObjBag sampleBag)
{
int err;
if( eltPassed )
return( ReturnCodes.OK );
if (num > 0)
{
if ((err = sampleBag.add(onlyElt))
!= ReturnCodes.OK)
return( err );
eltPassed = true;
}
return( ReturnCodes.OK );
}
public synchronized int sampleToWt(Weight minWt, WtdObjBag sampleBag)
{
int err = 0;
if( eltPassed )
return( ReturnCodes.OK );
if( onlyElt.getWeight().getUnderlyingValue() >= minWt.getUnderlyingValue() )
{
if ( (err = sampleBag.add(onlyElt))
!= ReturnCodes.OK)
return( err );
eltPassed = true;
}
return( ReturnCodes.OK );
}
public int exactNumRemaining()
{
if ( eltPassed )
return( 0 );
else
return( 1 );
}
public int approxNumRemaining()
{
if ( eltPassed )
return( 0 );
else
return( 1 );
}
public int maxNumRemaining()
{
if ( eltPassed )
return( 0 );
else
return( 1 );
}
} // End of SingleWtdObjSet.Enum
/**
Construct a singleton set from a single object.
@param w wtdObj
@param d Debug
*/
public SingletonWtdObjSet(WtdObj w, Debug d)
{
debug = d;
onlyElt = new WtdObj(w, d);
// Check the implementation
}
/**
Construct a singleton set from a single id and a weight.
@param FullID ID of the only element.
@param wt Weight for the element.
@param d Debug
*/
public SingletonWtdObjSet(FullID id, Weight wt, Debug d)
{
debug = d;
onlyElt = new WtdObj(id, wt, d);
}
/**
Construct a singleton set from details.
@param clID Class Id
@param instID Instance ID
@param wt Weight
*/
public SingletonWtdObjSet(int clID, int instID, Weight wt, Debug d)
{
debug = d;
onlyElt = new WtdObj(clID, instID, wt, d);
}
/**
Create an Enumeration for this set.
*/
public WtdObjSetEnumeration elements()
{
return( new Enum() );
}
/**
Is a given ID an Element of this set?
@param id The (ID of the) element to be tested.
@param Wt Set to the Weight of id in this set if id is an element.
@return true / false
*/
public boolean isElt(FullID id, Weight wt)
{
if (id != onlyElt.getID())
return( false );
wt.set(onlyElt.getWeight());
return( true );
}
/**
Is a given ID an element of this set?
@param id The (ID of the) element to be tested.
@return null -- the element is not in the set.
any valid Weight -- the element is in the set with that Weight.
*/
public Weight isElt(FullID id)
{
if ( id == onlyElt.getID() )
return( onlyElt.getWeight() );
else
return( null );
}
/**
Is this set empty?
@return true / false
*/
public boolean isEmpty()
{
return( false );
}
/**
Return the exact number of elements in this set (may be costly).
*/
public int exactSize()
{
return( 1 );
}
/**
Return the approximate number of elements in this set (can be cheap
and dirty).
*/
public int approxSize()
{
return( 1 );
}
/**
Return the maximum number of elements in this set (can be cheap
and dirty).
*/
public int maxSize()
{
return( 1 );
}
/**
Return a human-readable string for this entire set (may be large).
*/
public String toString()
{
return( new String("{" + onlyElt.toString() + "}") );
}
/**
Return a short human-readable string that quickly describes this set.
*/
public String profile()
{
return( new String("{" + onlyElt.toString() + "}") );
}
} // End of SingletonWtdObjSet.