Skip to main content
Rollback to Revision 2
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

UPDATE

After response from Pimgd, I've made some changes and I'm left with this. Is this as clean as it can be?

public class Properties
{
        /** String type properties. */
        private final List<String> m_stringProperties = Arrays.asList("str1", "str2", "str3");
    
        /** Float type properties. */
        private final List<String> m_floatProperties = Arrays.asList("float1", "float2", "float3");
    
        /** Integer type properties. */
        private final List<String> m_intProperties = Arrays.asList("int1", "int2");

    /**
     * Returns the value of the given property if it exists.
     *
     * @param name The name of the property.
     *
     * @return The property value if it exists, null otherwise.
     */
    public Object getProperty(String name)
    {
        return m_properties.get(name);
    }

    /**
     * Sets the value of the given string-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, String value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_stringProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for String property";
        }
    }

    /**
     * Sets the value of the given float-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Float value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_floatProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Float property";
        }
    }

    /**
     * Sets the value of the given integer-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Integer value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_intProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Integer property";
        }
    }
}

UPDATE

After response from Pimgd, I've made some changes and I'm left with this. Is this as clean as it can be?

public class Properties
{
        /** String type properties. */
        private final List<String> m_stringProperties = Arrays.asList("str1", "str2", "str3");
    
        /** Float type properties. */
        private final List<String> m_floatProperties = Arrays.asList("float1", "float2", "float3");
    
        /** Integer type properties. */
        private final List<String> m_intProperties = Arrays.asList("int1", "int2");

    /**
     * Returns the value of the given property if it exists.
     *
     * @param name The name of the property.
     *
     * @return The property value if it exists, null otherwise.
     */
    public Object getProperty(String name)
    {
        return m_properties.get(name);
    }

    /**
     * Sets the value of the given string-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, String value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_stringProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for String property";
        }
    }

    /**
     * Sets the value of the given float-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Float value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_floatProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Float property";
        }
    }

    /**
     * Sets the value of the given integer-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Integer value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_intProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Integer property";
        }
    }
}
Update following response.
Source Link
ksl
  • 141
  • 1
  • 2
  • 6

UPDATE

After response from Pimgd, I've made some changes and I'm left with this. Is this as clean as it can be?

public class Properties
{
        /** String type properties. */
        private final List<String> m_stringProperties = Arrays.asList("str1", "str2", "str3");
    
        /** Float type properties. */
        private final List<String> m_floatProperties = Arrays.asList("float1", "float2", "float3");
    
        /** Integer type properties. */
        private final List<String> m_intProperties = Arrays.asList("int1", "int2");

    /**
     * Returns the value of the given property if it exists.
     *
     * @param name The name of the property.
     *
     * @return The property value if it exists, null otherwise.
     */
    public Object getProperty(String name)
    {
        return m_properties.get(name);
    }

    /**
     * Sets the value of the given string-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, String value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_stringProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for String property";
        }
    }

    /**
     * Sets the value of the given float-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Float value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_floatProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Float property";
        }
    }

    /**
     * Sets the value of the given integer-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Integer value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_intProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Integer property";
        }
    }
}

UPDATE

After response from Pimgd, I've made some changes and I'm left with this. Is this as clean as it can be?

public class Properties
{
        /** String type properties. */
        private final List<String> m_stringProperties = Arrays.asList("str1", "str2", "str3");
    
        /** Float type properties. */
        private final List<String> m_floatProperties = Arrays.asList("float1", "float2", "float3");
    
        /** Integer type properties. */
        private final List<String> m_intProperties = Arrays.asList("int1", "int2");

    /**
     * Returns the value of the given property if it exists.
     *
     * @param name The name of the property.
     *
     * @return The property value if it exists, null otherwise.
     */
    public Object getProperty(String name)
    {
        return m_properties.get(name);
    }

    /**
     * Sets the value of the given string-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, String value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_stringProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for String property";
        }
    }

    /**
     * Sets the value of the given float-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Float value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_floatProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Float property";
        }
    }

    /**
     * Sets the value of the given integer-type property. If the property does not exist, it is added first.
     * The function asserts if an invalid property name is supplied.
     *
     * @param name  The name of the property. Asserts if null.
     * @param value The value of the property. Asserts if null.
     *
     * @return None.
     */
    public void setProperty(String name, Integer value)
    {
        assert name != null : "Null property name";
        assert value != null : "Null property value";

        if (m_intProperties.contains(name))
        {
            m_properties.put(name, value);
        }

        else
        {
            assert false : "Invalid name for Integer property";
        }
    }
}
edited body
Source Link
ksl
  • 141
  • 1
  • 2
  • 6

I'd like to know if there is a better way to implement this. Specifically, I'd like to avoid the use of Object and the casting that goes with it. I've experimented with a parametrised class and generic types and even a typesafe heterogenous container as described in Item 2029 of Effective Java.

I'd like to know if there is a better way to implement this. Specifically, I'd like to avoid the use of Object and the casting that goes with it. I've experimented with a parametrised class and generic types and even a typesafe heterogenous container as described in Item 20 of Effective Java.

I'd like to know if there is a better way to implement this. Specifically, I'd like to avoid the use of Object and the casting that goes with it. I've experimented with a parametrised class and generic types and even a typesafe heterogenous container as described in Item 29 of Effective Java.

Source Link
ksl
  • 141
  • 1
  • 2
  • 6
Loading