/* * Copyright 2007 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package Sequenic.T2.Seq; import Sequenic.T2.Pool; import Sequenic.T2.Msg.T2Error; import Sequenic.T2.Obj.Cloner; import Sequenic.T2.export.Visitor; /** * This represents a MkVal-step in which an object is 'created' by picking a * 'constant', e.g. from the {@link Sequenic.T2.BaseDomain base domain}. * The object used as 'constant' is remembered in this step. * */ public abstract class CONST extends MkValStep { public abstract Object getObject(); public abstract Class getObjectClass(); public abstract String getObjectClassName(); public abstract boolean isNull(); @Override public void visit(Visitor visitor) { visitor.visit(this); } public static CONST nonNull(final Object val) { if(val==null) throw new NullPointerException(); else return new CONST() { @Override public Object getObject() { return val; } @Override public Class getObjectClass() { return val.getClass(); } @Override public String getObjectClassName() { return val.getClass().getName(); } @Override public boolean isNull() { return false; } @Override public Object exec(Class CUT, Pool pool) { try { return Cloner.clone(val); } catch (Exception e) { throw new T2Error("Fail to clone. Perhaps because Base Domain produces unclonable objects.", e); } } @Override public String toString() { if(val instanceof String) { return "CONST "; // TODO filter unprintable chars and show real value? } else { return "CONST " + val; //StringFormater.indentButFirst(val.toString(),4) ; } } }; } public static CONST nullConst(final Class type) { return new CONST() { @Override public Object exec(Class CUT, Pool pool) { return null; } @Override public Object getObject() { return null; } @Override public String getObjectClassName() { return type.getName(); } @Override public boolean isNull() { return true; } @Override public String toString() { return "CONST null"; } @Override public Class getObjectClass() { return type; } }; } @Override public boolean isObsolete() { return false; } }