package de.unisiegen.tpml.core.expressions ;
import de.unisiegen.tpml.core.exceptions.NotOnlyFreeVariableException ;
import de.unisiegen.tpml.core.interfaces.DefaultExpressions ;
import de.unisiegen.tpml.core.latex.DefaultLatexCommand ;
import de.unisiegen.tpml.core.latex.LatexCommandList ;
import de.unisiegen.tpml.core.latex.LatexStringBuilder ;
import de.unisiegen.tpml.core.latex.LatexStringBuilderFactory ;
import de.unisiegen.tpml.core.prettyprinter.PrettyStringBuilder ;
import de.unisiegen.tpml.core.prettyprinter.PrettyStringBuilderFactory ;
import de.unisiegen.tpml.core.typechecker.TypeSubstitution ;
/**
* Instances of this class represent new expressions.
*
* @author Christian Fehler
* @version $Rev:1053 $
* @see Class
*/
public final class New extends Expression implements DefaultExpressions
{
/**
* Indeces of the child {@link Expression}s.
*/
private static final int [ ] INDICES_E = new int [ ]
{ - 1 } ;
/**
* String for the case that the expression is null.
*/
private static final String EXPRESSION_NULL = "expression is null" ; //$NON-NLS-1$
/**
* The caption of this {@link Expression}.
*/
private static final String CAPTION = Expression.getCaption ( New.class ) ;
/**
* Returns a set of needed latex commands for this latex printable object.
*
* @return A set of needed latex commands for this latex printable object.
*/
public static LatexCommandList getLatexCommandsStatic ( )
{
LatexCommandList commands = new LatexCommandList ( ) ;
commands.add ( new DefaultLatexCommand ( LATEX_KEY_NEW , 0 ,
"\\textbf{\\color{" + LATEX_COLOR_KEYWORD + "}{new}}" ) ) ; //$NON-NLS-1$ //$NON-NLS-2$
commands.add ( new DefaultLatexCommand ( LATEX_NEW , 1 , "\\color{" //$NON-NLS-1$
+ LATEX_COLOR_EXPRESSION + "}\\" + LATEX_KEY_NEW + "\\ #1" , "e" ) ) ; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
return commands ;
}
/**
* The expression.
*/
private Expression [ ] expressions ;
/**
* Allocates a new <code>And</code> instance with the specified
* <code>e1</code> and <code>e2</code>.
*
* @param pExpression The expression.
* @throws NullPointerException if <code>e</code> is <code>null</code>.
*/
public New ( Expression pExpression )
{
if ( pExpression == null )
{
throw new NullPointerException ( EXPRESSION_NULL ) ;
}
this.expressions = new Expression [ ]
{ pExpression } ;
this.expressions [ 0 ].setParent ( this ) ;
}
/**
* Allocates a new <code>And</code> instance with the specified
* <code>e1</code> and <code>e2</code>.
*
* @param pExpression The expression.
* @param pParserStartOffset The start offset of this {@link Expression} in
* the source code.
* @param pParserEndOffset The end offset of this {@link Expression} in the
* source code.
* @throws NullPointerException if <code>e</code> is <code>null</code>.
*/
public New ( Expression pExpression , int pParserStartOffset ,
int pParserEndOffset )
{
this ( pExpression ) ;
this.parserStartOffset = pParserStartOffset ;
this.parserEndOffset = pParserEndOffset ;
}
/**
* {@inheritDoc}
*
* @see Expression#clone()
*/
@ Override
public New clone ( )
{
return new New ( this.expressions [ 0 ].clone ( ) ) ;
}
/**
* {@inheritDoc}
*
* @see Expression#equals(Object)
*/
@ Override
public boolean equals ( Object pObject )
{
if ( pObject instanceof New )
{
New other = ( New ) pObject ;
return this.expressions [ 0 ].equals ( other.expressions [ 0 ] ) ;
}
return false ;
}
/**
* {@inheritDoc}
*/
@ Override
public String getCaption ( )
{
return CAPTION ;
}
/**
* Returns the expression.
*
* @return the expression.
*/
public Expression getE ( )
{
return this.expressions [ 0 ] ;
}
/**
* Returns the sub expressions.
*
* @return the sub expressions.
*/
public Expression [ ] getExpressions ( )
{
return this.expressions ;
}
/**
* Returns the indices of the child {@link Expression}s.
*
* @return The indices of the child {@link Expression}s.
*/
public int [ ] getExpressionsIndex ( )
{
return INDICES_E ;
}
/**
* Returns a set of needed latex commands for this latex printable object.
*
* @return A set of needed latex commands for this latex printable object.
*/
@ Override
public LatexCommandList getLatexCommands ( )
{
LatexCommandList commands = super.getLatexCommands ( ) ;
commands.add ( getLatexCommandsStatic ( ) ) ;
return commands ;
}
/**
* {@inheritDoc}
*
* @see Expression#hashCode()
*/
@ Override
public int hashCode ( )
{
return this.expressions [ 0 ].hashCode ( ) ;
}
/**
* {@inheritDoc}
*
* @see Expression#substitute(Identifier, Expression)
*/
@ Override
public New substitute ( Identifier pId , Expression pExpression )
{
if ( pExpression.getIdentifierFreeNotOnlyVariable ( ) )
{
throw new NotOnlyFreeVariableException ( ) ;
}
Expression newE = this.expressions [ 0 ].substitute ( pId , pExpression ) ;
return new New ( newE ) ;
}
/**
* {@inheritDoc}
*
* @see Expression#substitute(TypeSubstitution)
*/
@ Override
public New substitute ( TypeSubstitution pTypeSubstitution )
{
Expression newE = this.expressions [ 1 ].substitute ( pTypeSubstitution ) ;
return new New ( newE ) ;
}
/**
* {@inheritDoc}
*
* @see Expression#toLatexStringBuilder(LatexStringBuilderFactory,int)
*/
@ Override
public LatexStringBuilder toLatexStringBuilder (
LatexStringBuilderFactory pLatexStringBuilderFactory , int pIndent )
{
LatexStringBuilder builder = pLatexStringBuilderFactory.newBuilder (
PRIO_NEW , LATEX_NEW , pIndent , this.toPrettyString ( ).toString ( ) ,
this.expressions [ 0 ].toPrettyString ( ).toString ( ) ) ;
builder.addBuilder ( this.expressions [ 0 ].toLatexStringBuilder (
pLatexStringBuilderFactory , pIndent + LATEX_INDENT ) , PRIO_NEW_E ) ;
return builder ;
}
/**
* {@inheritDoc}
*
* @see Expression#toPrettyStringBuilder(PrettyStringBuilderFactory)
*/
@ Override
public PrettyStringBuilder toPrettyStringBuilder (
PrettyStringBuilderFactory pPrettyStringBuilderFactory )
{
if ( this.prettyStringBuilder == null )
{
this.prettyStringBuilder = pPrettyStringBuilderFactory.newBuilder ( this ,
PRIO_NEW ) ;
this.prettyStringBuilder.addKeyword ( PRETTY_NEW ) ;
this.prettyStringBuilder.addText ( PRETTY_SPACE ) ;
this.prettyStringBuilder.addBuilder ( this.expressions [ 0 ]
.toPrettyStringBuilder ( pPrettyStringBuilderFactory ) , PRIO_NEW_E ) ;
}
return this.prettyStringBuilder ;
}
}