|
From: Brendan M. <mc...@us...> - 2006-11-20 19:52:59
|
Update of /cvsroot/jigs/jigs/src/edu/whitman/halfway/util In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv26959/src/edu/whitman/halfway/util Modified Files: PropProvider.java CernUtil.java Log Message: Index: PropProvider.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/PropProvider.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** PropProvider.java 11 Nov 2006 02:04:56 -0000 1.6 --- PropProvider.java 20 Nov 2006 19:52:24 -0000 1.7 *************** *** 6,104 **** import java.io.*; ! public class PropProvider{ ! static Logger log = Logger.getLogger(PropProvider.class.getName()); ! public String toString(){ ! return getProps().toString(); ! } ! ! public Properties getProps(){ ! return getProps(""); ! } ! public Properties getProps(String prefix){ ! Class cl = getClass(); ! Field[] fields = cl.getDeclaredFields(); ! Properties props = new Properties(); ! for(int i=0; i< fields.length; i++){ ! int mods = fields[i].getModifiers(); ! if(!Modifier.isStatic(mods) && !Modifier.isTransient(mods)){ ! try{ ! ! Object fldValue = fields[i].get(this); ! String s; ! if(fldValue == null){ ! s = "null"; ! }else{ ! s = fldValue.toString(); ! } ! props.setProperty(fields[i].getName(), s); ! }catch(IllegalAccessException e){ ! log.error("IllegalAccessException: Could not read value of field " + fields[i].getName()); ! } ! }else{ ! if(log.isDebugEnabled()){ ! log.debug("Skipping field " + fields[i].getName()); ! log.debug("Skipped because of modifiers: " + Modifier.toString(mods)); ! } ! } ! } ! return props; ! } ! public void setFromProps(Properties props){ ! if(props == null){ ! throw new IllegalArgumentException("Error, null props"); } ! ! Class cl = getClass(); ! Field[] fields = cl.getDeclaredFields(); ! for(int i=0; i< fields.length; i++){ ! int mods = fields[i].getModifiers(); ! if(!Modifier.isStatic(mods) && !Modifier.isTransient(mods)){ ! String s = props.getProperty(fields[i].getName()); ! if(s != null){ ! s = s.trim(); //XXX may not be necessary ! Object val = null; ! try{ ! val = PropUtil.makeObject(fields[i].getType(), s); ! fields[i].set(this, val); ! }catch(IllegalAccessException e){ ! log.error("IllegalAccessException: Could not set value of field " + fields[i].getName()); ! }catch(IllegalArgumentException e){ ! log.error("IllegalArgumentException: Could not set value of field " + fields[i].getName() + " to " + val); ! } ! }else{ ! log.warn("Got null string for field " + fields[i].getName()); ! } ! }else{ ! log.info("Skipping field " + fields[i].getName()); ! log.debug("Skipped because of modifiers: " + Modifier.toString(mods)); ! } } } ! public boolean saveToFile(File f){ ! Properties p = getProps(); ! try{ ! p.store(new FileOutputStream(f), null); ! }catch(IOException e){ ! return false; ! } ! return true; } ! public boolean loadFromFile(File f){ ! Properties p = new Properties(); ! try{ ! p.load(new FileInputStream(f)); ! setFromProps(p); ! }catch(IOException e){ ! return false; } ! ! return true; } } --- 6,122 ---- import java.io.*; ! public class PropProvider { ! static Logger log = Logger.getLogger(PropProvider.class.getName()); ! public String toString() { ! return getProps().toString(); ! } ! public Properties getProps() { ! return getProps(""); ! } + public Properties getProps(String prefix) { + Class cl = getClass(); + Field[] fields = cl.getDeclaredFields(); + Properties props = new Properties(); + for (int i = 0; i < fields.length; i++) { + int mods = fields[i].getModifiers(); + if (!Modifier.isStatic(mods) && !Modifier.isTransient(mods)) { + try { ! Object fldValue = fields[i].get(this); ! String s; ! if (fldValue == null) { ! s = "null"; ! } else { ! s = fldValue.toString(); ! } ! props.setProperty(fields[i].getName(), s); ! } catch (IllegalAccessException e) { ! log.error("IllegalAccessException: Could not read value of field " ! + fields[i].getName()); } ! } else { ! if (log.isDebugEnabled()) { ! log.debug("Skipping field " + fields[i].getName()); ! log.debug("Skipped because of modifiers: " + Modifier.toString(mods)); } + } } + return props; + } + public void setFromProps(Properties props, boolean includeSubclasses) { + if (!includeSubclasses) { + setFromProps(props); + } else { ! Class crntClass = this.getClass(); ! do { ! setFromProps(props, crntClass); ! } while ((crntClass = crntClass.getSuperclass()) != null); } + } ! public void setFromProps(Properties props) { ! setFromProps(props, getClass()); ! } ! ! protected void setFromProps(Properties props, Class myClass) { ! if (props == null) { ! throw new IllegalArgumentException("Error, null props"); ! } ! ! Field[] fields = myClass.getDeclaredFields(); ! for (int i = 0; i < fields.length; i++) { ! int mods = fields[i].getModifiers(); ! log.info("Processing field " + fields[i].getName()); ! if (!Modifier.isStatic(mods) && !Modifier.isTransient(mods)) { ! String s = props.getProperty(fields[i].getName()); ! if (s != null) { ! s = s.trim(); //XXX may not be necessary ! Object val = null; ! try { ! val = PropUtil.makeObject(fields[i].getType(), s); ! fields[i].set(this, val); ! log.info("set field " + fields[i].getName() + " to val " + val); ! } catch (IllegalAccessException e) { ! log.error("IllegalAccessException: Could not set value of field " ! + fields[i].getName()); ! } catch (IllegalArgumentException e) { ! log.error("IllegalArgumentException: Could not set value of field " ! + fields[i].getName() + " to " + val); ! } ! } else { ! log.warn("Got null string for field " + fields[i].getName()); } ! } else { ! log.info("Skipping field " + fields[i].getName()); ! log.debug("Skipped because of modifiers: " + Modifier.toString(mods)); ! } } + } + + public boolean saveToFile(File f) { + Properties p = getProps(); + try { + p.store(new FileOutputStream(f), null); + } catch (IOException e) { + return false; + } + return true; + } + + public boolean loadFromFile(File f) { + Properties p = new Properties(); + try { + p.load(new FileInputStream(f)); + setFromProps(p); + } catch (IOException e) { + return false; + } + + return true; + } } Index: CernUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/CernUtil.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** CernUtil.java 29 Aug 2006 19:29:56 -0000 1.12 --- CernUtil.java 20 Nov 2006 19:52:24 -0000 1.13 *************** *** 280,285 **** public static final double sum(DoubleMatrix1D a){ return a.aggregate(F.plus,F.identity); ! } ! public static final double sum(DoubleArrayList aList){ int n = aList.size(); --- 280,285 ---- public static final double sum(DoubleMatrix1D a){ return a.aggregate(F.plus,F.identity); ! } ! public static final double sum(DoubleArrayList aList){ int n = aList.size(); |