MOA 12.03
Real Time Analytics for Data Streams
ListOption.java
Go to the documentation of this file.
00001 /*
00002  *    ListOption.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 
00028 public class ListOption extends AbstractOption {
00029 
00030     private static final long serialVersionUID = 1L;
00031 
00032     protected Option[] currentList;
00033 
00034     protected Option expectedType;
00035 
00036     protected Option[] defaultList;
00037 
00038     protected char separatorChar;
00039 
00040     public ListOption(String name, char cliChar, String purpose,
00041             Option expectedType, Option[] defaultList, char separatorChar) {
00042         super(name, cliChar, purpose);
00043         this.expectedType = expectedType;
00044         this.defaultList = defaultList.clone();
00045         this.separatorChar = separatorChar;
00046         resetToDefault();
00047     }
00048 
00049     public void setList(Option[] optList) {
00050         Option[] newArray = new Option[optList.length];
00051         for (int i = 0; i < optList.length; i++) {
00052             newArray[i] = this.expectedType.copy();
00053             newArray[i].setValueViaCLIString(optList[i].getValueAsCLIString());
00054         }
00055         this.currentList = newArray;
00056     }
00057 
00058     public Option[] getList() {
00059         return this.currentList.clone();
00060     }
00061 
00062     @Override
00063     public String getDefaultCLIString() {
00064         return optionArrayToCLIString(this.defaultList, this.separatorChar);
00065     }
00066 
00067     @Override
00068     public String getValueAsCLIString() {
00069         return optionArrayToCLIString(this.currentList, this.separatorChar);
00070     }
00071 
00072     @Override
00073     public void setValueViaCLIString(String s) {
00074         this.currentList = cliStringToOptionArray(s, this.separatorChar,
00075                 this.expectedType);
00076     }
00077 
00078     public static Option[] cliStringToOptionArray(String s, char separator,
00079             Option expectedType) {
00080          if (s == null || s.length() < 1) {
00081              return new Option[0];
00082          }
00083         String[] subStrings = s.split(Character.toString(separator));
00084         Option[] options = new Option[subStrings.length];
00085         for (int i = 0; i < options.length; i++) {
00086             options[i] = expectedType.copy();
00087             options[i].setValueViaCLIString(subStrings[i]);
00088         }
00089         return options;
00090     }
00091 
00092     public static String optionArrayToCLIString(Option[] os, char separator) {
00093         StringBuilder sb = new StringBuilder();
00094         for (int i = 0; i < os.length; i++) {
00095             if (i > 0) {
00096                 sb.append(separator);
00097             }
00098             sb.append(os[i].getValueAsCLIString());
00099         }
00100         return sb.toString();
00101     }
00102 }
 All Classes Namespaces Files Functions Variables Enumerations