MOA 12.03
Real Time Analytics for Data Streams
AbstractOptionHandler.java
Go to the documentation of this file.
00001 /*
00002  *    AbstractOptionHandler.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby ([email protected])
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.options;
00021 
00022 import java.lang.reflect.Field;
00023 import java.util.HashMap;
00024 import java.util.LinkedList;
00025 import java.util.List;
00026 import java.util.Map;
00027 
00028 import moa.AbstractMOAObject;
00029 import moa.core.ObjectRepository;
00030 import moa.tasks.NullMonitor;
00031 import moa.tasks.TaskMonitor;
00032 
00040 public abstract class AbstractOptionHandler extends AbstractMOAObject implements
00041         OptionHandler {
00042 
00043     private static final long serialVersionUID = 1L;
00044 
00046     protected Options options;
00047 
00049     protected Map<String, Object> classOptionNamesToPreparedObjects;
00050 
00051     @Override
00052     public String getPurposeString() {
00053         return "Anonymous object: purpose undocumented.";
00054     }
00055 
00056     @Override
00057     public Options getOptions() {
00058         if (this.options == null) {
00059             this.options = new Options();
00060             Option[] myOptions = discoverOptionsViaReflection();
00061             for (Option option : myOptions) {
00062                 this.options.addOption(option);
00063             }
00064         }
00065         return this.options;
00066     }
00067 
00068     @Override
00069     public void prepareForUse() {
00070         prepareForUse(new NullMonitor(), null);
00071     }
00072 
00073     @Override
00074     public void prepareForUse(TaskMonitor monitor, ObjectRepository repository) {
00075         prepareClassOptions(monitor, repository);
00076         prepareForUseImpl(monitor, repository);
00077     }
00078 
00088     protected abstract void prepareForUseImpl(TaskMonitor monitor,
00089             ObjectRepository repository);
00090 
00091     @Override
00092     public String getCLICreationString(Class<?> expectedType) {
00093         return ClassOption.stripPackagePrefix(this.getClass().getName(),
00094                 expectedType)
00095                 + " " + getOptions().getAsCLIString();
00096     }
00097 
00098     @Override
00099     public OptionHandler copy() {
00100         return (OptionHandler) super.copy();
00101     }
00102 
00108     protected Option[] discoverOptionsViaReflection() {
00109         Class<? extends AbstractOptionHandler> c = this.getClass();
00110         Field[] fields = c.getFields();
00111         List<Option> optList = new LinkedList<Option>();
00112         for (Field field : fields) {
00113             String fName = field.getName();
00114             Class<?> fType = field.getType();
00115             if (fName.endsWith("Option")) {
00116                 if (Option.class.isAssignableFrom(fType)) {
00117                     Option oVal = null;
00118                     try {
00119                         field.setAccessible(true);
00120                         oVal = (Option) field.get(this);
00121                     } catch (IllegalAccessException ignored) {
00122                         // cannot access this field
00123                     }
00124                     if (oVal != null) {
00125                         optList.add(oVal);
00126                     }
00127                 }
00128             }
00129         }
00130         return optList.toArray(new Option[optList.size()]);
00131     }
00132 
00139     protected void prepareClassOptions(TaskMonitor monitor,
00140             ObjectRepository repository) {
00141         this.classOptionNamesToPreparedObjects = null;
00142         Option[] optionArray = getOptions().getOptionArray();
00143         for (Option option : optionArray) {
00144             if (option instanceof ClassOption) {
00145                 ClassOption classOption = (ClassOption) option;
00146                 monitor.setCurrentActivity("Materializing option "
00147                         + classOption.getName() + "...", -1.0);
00148                 Object optionObj = classOption.materializeObject(monitor,
00149                         repository);
00150                 if (monitor.taskShouldAbort()) {
00151                     return;
00152                 }
00153                 if (optionObj instanceof OptionHandler) {
00154                     monitor.setCurrentActivity("Preparing option "
00155                             + classOption.getName() + "...", -1.0);
00156                     ((OptionHandler) optionObj).prepareForUse(monitor,
00157                             repository);
00158                     if (monitor.taskShouldAbort()) {
00159                         return;
00160                     }
00161                 }
00162                 if (this.classOptionNamesToPreparedObjects == null) {
00163                     this.classOptionNamesToPreparedObjects = new HashMap<String, Object>();
00164                 }
00165                 this.classOptionNamesToPreparedObjects.put(option.getName(),
00166                         optionObj);
00167             }
00168         }
00169     }
00170 
00177     protected Object getPreparedClassOption(ClassOption opt) {
00178         return this.classOptionNamesToPreparedObjects.get(opt.getName());
00179     }
00180 }
 All Classes Namespaces Files Functions Variables Enumerations