Changeset 72 for trunk

Show
Ignore:
Timestamp:
02/13/08 17:42:56 (9 months ago)
Author:
schst
Message:

Added new get() method, that casts the value before returning it

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/pom.xml

    r71 r72  
    55  <groupId>net.schst</groupId> 
    66  <artifactId>XJConf</artifactId> 
    7   <version>0.10.5</version> 
     7  <version>0.10.6</version> 
    88  <packaging>jar</packaging> 
    99 
  • trunk/src/main/java/net/schst/XJConf/XmlReader.java

    r62 r72  
    1414import net.schst.XJConf.exceptions.UnknownNamespaceException; 
    1515import net.schst.XJConf.exceptions.UnknownTagException; 
     16import net.schst.XJConf.exceptions.ValueNotAvailableException; 
    1617import net.schst.XJConf.exceptions.XJConfException; 
    1718 
     
    390391        return this.config.get(name); 
    391392    } 
     393 
     394    /** 
     395     * fetch a configuration option 
     396     *  
     397     * @param    name of the option 
     398     * @return   value 
     399     * @throws   XJConfException    if the value has not been set, or does not have the desired type 
     400     */ 
     401     public <T extends Object> T get(String name, Class<T> clazz) throws XJConfException { 
     402         Object val = this.config.get(name); 
     403         if (val == null) { 
     404             throw new ValueNotAvailableException("Value with name " + name + " not available in the configuration."); 
     405         } 
     406         if (!clazz.isAssignableFrom(val.getClass())) { 
     407             throw new ValueNotAvailableException("Value with name " + name + " is not of the desired type " + clazz.getName() + "."); 
     408         } 
     409         return (T)val; 
     410     } 
    392411} 
  • trunk/src/test/java/net/schst/XJConf/tests/helpers/PrimitivesContainer.java

    r60 r72  
    11package net.schst.XJConf.tests.helpers; 
    22 
    3 public class PrimitivesContainer
     3public class PrimitivesContainer implements IPrimitivesContainer
    44    private boolean booleanValue; 
    55    private int intValue; 
     
    99    private short shortValue; 
    1010     
    11     /** 
    12      * @return Returns the booleanValue. 
     11    /* (non-Javadoc) 
     12     * @see net.schst.XJConf.tests.helpers.IPrimitivesContainer#getBooleanValue() 
    1313     */ 
    1414    public boolean getBooleanValue() { 
     
    2121        this.booleanValue = booleanValue; 
    2222    } 
    23     /** 
    24      * @return Returns the floatValue. 
     23    /* (non-Javadoc) 
     24     * @see net.schst.XJConf.tests.helpers.IPrimitivesContainer#getFloatValue() 
    2525     */ 
    2626    public float getFloatValue() { 
     
    3333        this.floatValue = floatValue; 
    3434    } 
    35     /** 
    36      * @return Returns the intValue. 
     35    /* (non-Javadoc) 
     36     * @see net.schst.XJConf.tests.helpers.IPrimitivesContainer#getIntValue() 
    3737     */ 
    3838    public int getIntValue() { 
     
    4545        this.intValue = intValue; 
    4646    } 
    47     /** 
    48      * @return Returns the longValue. 
     47    /* (non-Javadoc) 
     48     * @see net.schst.XJConf.tests.helpers.IPrimitivesContainer#getLongValue() 
    4949     */ 
    5050    public long getLongValue() { 
     
    5757        this.longValue = longValue; 
    5858    } 
    59     /** 
    60      * @return Returns the doubleParam. 
     59    /* (non-Javadoc) 
     60     * @see net.schst.XJConf.tests.helpers.IPrimitivesContainer#getDoubleValue() 
    6161     */ 
    6262    public double getDoubleValue() {