📄 toint.java
字号:
package com.sri.oaa2.icl;
/**
* Objects of this class convert IclTerms to ints, if possible. Thus, this
* works for IclInt and IclFloat. If you don't want to implicitly convert
* IclFloats to ints, first check if the IclTerm is an IclInt using
* IclTerm.isInt().
*/
public final class ToInt implements OaaPrologVisitor
{
/// Our singleton instance
private static final ToInt singleton = new ToInt();
protected ToInt()
{
}
public final Object visit(IclTerm node, Object data)
{
return null;
}
public final Object visit(IclStruct node, Object data)
{
return null;
}
public final Object visit(IclList node, Object data)
{
return null;
}
public final Object visit(IclGroup node, Object data)
{
return null;
}
public final Object visit(IclInt node, Object data)
{
return node.toInteger();
}
public final Object visit(IclFloat node, Object data)
{
return new Integer(node.getValue().intValue());
}
public final Object visit(IclStr node, Object data)
{
return null;
}
public final Object visit(IclVar node, Object data)
{
return null;
}
/**
* Get an instance of a ToInt object. This avoids multiple calls to the constructor.
* ToInt is thread safe.
*/
public final static ToInt getInstance()
{
return singleton;
}
/**
* Convert an IclTerm to an int, if possible. If not, return the given default.
*
* @param IclTerm t: the term to convert
* @param int def: the default to use
* @return int: the IclTerm as an int, or the default value
*/
public final int from(IclTerm t, int def)
{
Integer i = (Integer)t.accept(this, null);
if(i == null) {
return def;
}
return i.intValue();
}
/**
* Convert an IclTerm to an int, if possible. If not, throw an UnsupportedOperationException.
*
* @param IclTerm t: the term to convert
* @return int: the term as an int
* @throws UnsupportedOperationException if no such conversion possible
*/
public final int from(IclTerm t) throws UnsupportedOperationException
{
Integer i = (Integer)t.accept(this, null);
if(i == null) {
throw new UnsupportedOperationException("ToInt not supported by " + OaaPrologParser._tokenNames[t.getType()]);
}
return i.intValue();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -