On 2009-12-30 03.52, Niclas Hedhman wrote:
On Wed, Dec 30, 2009 at 5:31 AM, Rickard Öberg<[email protected]>  wrote:

Would that work for you? Is there any reason you don't want to use any of
the above?

I just make an observation, where I end up with a lot of

if( abc == null )
{
    abc = .... ;
}

But that is actually not what you had in your initial example. There's a difference between getting a value and supplying defaults (which is a valid concern), and getting a value, seeing that it is null, and updating it (and hence persisting the new value).

For the second case I would prefer to use default values, either through properties files (for service configs) or assemblers. Then the value gets set immediately when you create the entity, instead of having to check it on usage.

For the "provide defaults on optional property usage", where the default value is not used to update the property, it might make more sense to do something like this:

@Optional
Property<String> configValue();

and then:
String value = Optionals.get(config.configValue(), "defaultValue");

which is implemented as:
public static <T> T get(Property<T> property, T defaultValue)
{
  T value = property.get();
  if (value == null)
  {
    value = defaultValue;
  }
  return value;
}
---
This is similar to how System.getProperty(<name>,<default>) works for system properties.

If you really want to update it if you find a null, then we could do this:
String value = Optionals.getAndSet(config.configValue(), "defaultValue");

which just does:
public static <T> T getAndSet(Property<T> property, T defaultValue)
{
  T value = property.get();
  if (value == null)
  {
    property.set(defaultValue);
    value = defaultValue;
  }
  return value;
}
---
with the obvious problem being that the config will actually not be updated unless the UnitOfWork is completed or applied. It also feels a bit funny that usage of a config value might update it.

WDYT?

/Rickard

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to