MOA 12.03
Real Time Analytics for Data Streams
AbstractClassOption.java
Go to the documentation of this file.
00001 /*
00002  *    AbstractClassOption.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
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.io.File;
00023 
00024 import javax.swing.JComponent;
00025 
00026 import moa.core.ObjectRepository;
00027 import moa.core.SerializeUtils;
00028 import moa.tasks.Task;
00029 import moa.tasks.TaskMonitor;
00030 
00037 public abstract class AbstractClassOption extends AbstractOption {
00038 
00039     private static final long serialVersionUID = 1L;
00040 
00042     public static final String FILE_PREFIX_STRING = "file:";
00043 
00045     public static final String INMEM_PREFIX_STRING = "inmem:";
00046 
00048     protected Object currentValue;
00049 
00051     protected Class<?> requiredType;
00052 
00054     protected String defaultCLIString;
00055 
00057     protected String nullString;
00058 
00070     public AbstractClassOption(String name, char cliChar, String purpose,
00071             Class<?> requiredType, String defaultCLIString) {
00072         this(name, cliChar, purpose, requiredType, defaultCLIString, null);
00073     }
00074 
00087     public AbstractClassOption(String name, char cliChar, String purpose,
00088             Class<?> requiredType, String defaultCLIString, String nullString) {
00089         super(name, cliChar, purpose);
00090         this.requiredType = requiredType;
00091         this.defaultCLIString = defaultCLIString;
00092         this.nullString = nullString;
00093         resetToDefault();
00094     }
00095 
00101     public void setCurrentObject(Object obj) {
00102         if (((obj == null) && (this.nullString != null))
00103                 || this.requiredType.isInstance(obj)
00104                 || (obj instanceof String)
00105                 || (obj instanceof File)
00106                 || ((obj instanceof Task) && this.requiredType.isAssignableFrom(((Task) obj).getTaskResultType()))) {
00107             this.currentValue = obj;
00108         } else {
00109             throw new IllegalArgumentException("Object not of required type.");
00110         }
00111     }
00112 
00118     public Object getPreMaterializedObject() {
00119         return this.currentValue;
00120     }
00121 
00127     public Class<?> getRequiredType() {
00128         return this.requiredType;
00129     }
00130 
00136     public String getNullString() {
00137         return this.nullString;
00138     }
00139 
00147     public Object materializeObject(TaskMonitor monitor,
00148             ObjectRepository repository) {
00149         if ((this.currentValue == null)
00150                 || this.requiredType.isInstance(this.currentValue)) {
00151             return this.currentValue;
00152         } else if (this.currentValue instanceof String) {
00153             if (repository != null) {
00154                 Object inmemObj = repository.getObjectNamed((String) this.currentValue);
00155                 if (inmemObj == null) {
00156                     throw new RuntimeException("No object named "
00157                             + this.currentValue + " found in repository.");
00158                 }
00159                 return inmemObj;
00160             }
00161             throw new RuntimeException("No object repository available.");
00162         } else if (this.currentValue instanceof Task) {
00163             Task task = (Task) this.currentValue;
00164             Object result = task.doTask(monitor, repository);
00165             return result;
00166         } else if (this.currentValue instanceof File) {
00167             File inputFile = (File) this.currentValue;
00168             Object result = null;
00169             try {
00170                 result = SerializeUtils.readFromFile(inputFile);
00171             } catch (Exception ex) {
00172                 throw new RuntimeException("Problem loading "
00173                         + this.requiredType.getName() + " object from file '"
00174                         + inputFile.getName() + "':\n" + ex.getMessage(), ex);
00175             }
00176             return result;
00177         } else {
00178             throw new RuntimeException(
00179                     "Could not materialize object of required type "
00180                     + this.requiredType.getName() + ", found "
00181                     + this.currentValue.getClass().getName()
00182                     + " instead.");
00183         }
00184     }
00185 
00186     @Override
00187     public String getDefaultCLIString() {
00188         return this.defaultCLIString;
00189     }
00190 
00198     public static String classToCLIString(Class<?> aClass, Class<?> requiredType) {
00199         String className = aClass.getName();
00200         String packageName = requiredType.getPackage().getName();
00201         if (className.startsWith(packageName)) {
00202             // cut off package name
00203             className = className.substring(packageName.length() + 1, className.length());
00204         } else if (Task.class.isAssignableFrom(aClass)) {
00205             packageName = Task.class.getPackage().getName();
00206             if (className.startsWith(packageName)) {
00207                 // cut off task package name
00208                 className = className.substring(packageName.length() + 1,
00209                         className.length());
00210             }
00211         }
00212         return className;
00213     }
00214 
00215     @Override
00216     public abstract String getValueAsCLIString();
00217 
00218     @Override
00219     public abstract void setValueViaCLIString(String s);
00220 
00221     @Override
00222     public abstract JComponent getEditComponent();
00223 
00231     public static String stripPackagePrefix(String className, Class<?> expectedType) {
00232         if (className.startsWith(expectedType.getPackage().getName())) {
00233             return className.substring(expectedType.getPackage().getName().length() + 1);
00234         }
00235         return className;
00236     }
00237 }
 All Classes Namespaces Files Functions Variables Enumerations