Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/realizer/BMLParams.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 package edu.wpi.hri.bml.realizer; 00035 00036 import java.util.Hashtable; 00037 00038 import ros.Ros; 00039 00049 public enum BMLParams { 00051 DEBUG_LEVEL("debug_level", Type.STRING), 00053 LOGGER_TYPE("logger_type", Type.STRING); 00054 00056 private static final ParamTable<Boolean> boolParam = new ParamTable<Boolean>( 00057 false); 00059 private static final ParamTable<Integer> intParam = new ParamTable<Integer>( 00060 0); 00062 private static final ParamTable<Double> doubleParam = new ParamTable<Double>( 00063 0.0); 00065 private static final ParamTable<String> stringParam = new ParamTable<String>( 00066 ""); 00067 00068 // private stuff from here 00069 private static final String NAMESPACE = "realizer/conf"; 00070 private final String param; 00071 private final Type type; 00072 00073 private BMLParams(String param, Type type) { 00074 this.param = param; 00075 this.type = type; 00076 } 00077 00084 public void setBoolean(boolean value) { 00085 if (this.type == Type.BOOLEAN) 00086 boolParam.set(this.param, value); 00087 } 00088 00095 public void setDouble(double value) { 00096 if (this.type == Type.DOUBLE) 00097 doubleParam.set(this.param, value); 00098 } 00099 00106 public void setInt(int value) { 00107 if (this.type == Type.INT) 00108 intParam.set(this.param, value); 00109 } 00110 00117 public void setString(String value) { 00118 if (this.type == Type.STRING) 00119 stringParam.set(this.param, value); 00120 } 00121 00127 public boolean getBoolean() { 00128 if (this.type == Type.BOOLEAN) 00129 try { 00130 return Ros.getInstance().createNodeHandle(NAMESPACE).getBooleanParam( 00131 this.param, false); 00132 } catch (Exception e) { 00133 return boolParam.get(this.param); 00134 } 00135 else 00136 return false; 00137 } 00138 00144 public double getDouble() { 00145 if (this.type == Type.DOUBLE) 00146 try { 00147 return Ros.getInstance().createNodeHandle(NAMESPACE).getDoubleParam( 00148 this.param, false); 00149 } catch (Exception e) { 00150 return doubleParam.get(this.param); 00151 } 00152 else 00153 return 0.0; 00154 } 00155 00161 public int getInt() { 00162 if (this.type == Type.INT) 00163 try { 00164 return Ros.getInstance().createNodeHandle(NAMESPACE).getIntParam( 00165 this.param, false); 00166 } catch (Exception e) { 00167 return intParam.get(this.param); 00168 } 00169 else 00170 return 0; 00171 } 00172 00178 public String getString() { 00179 if (this.type == Type.STRING) 00180 try { 00181 return Ros.getInstance().createNodeHandle(NAMESPACE).getStringParam( 00182 this.param, false); 00183 } catch (Exception e) { 00184 return stringParam.get(this.param); 00185 } 00186 else 00187 return ""; 00188 } 00189 00190 private static enum Type { 00191 STRING, INT, DOUBLE, BOOLEAN; 00192 } 00193 00200 private static class ParamTable<T> { 00201 final Hashtable<String, T> table; 00202 final T defaultValue; 00203 00204 ParamTable(T def) { 00205 this.table = new Hashtable<String, T>(); 00206 this.defaultValue = def; 00207 } 00208 00209 void set(String s, T value) { 00210 this.table.put(s, value); 00211 } 00212 00213 T get(String s) { 00214 T value = this.table.get(s); 00215 if (value != null) 00216 return value; 00217 else 00218 return this.defaultValue; 00219 } 00220 } 00221 } |