Correct prototype for a method returning a Map of Variants

Matthew Johnson dbus at matthew.ath.cx
Fri May 23 11:20:27 PDT 2008


On Sat May 24 02:37, Gilles GIGAN wrote:
> public interface Device extends DBusInterface {
>     public Map<String,Variant<Object>> GetAllProperties();
> ...
> }

This is correct.

> I invoke this method with :
> Device d = (Device)
> conn.getRemoteObject("org.freedesktop.Hal","/org/freedesktop/Hal/devices/computer",Device.class);
> d.GetAllProperties();

This is correct (although the cast should be unneccessary, the method is
generic)

> I managed to get the key (String) and the value (Variant<Object>) with:
> Variant<Map<String,Variant<Object>>> v = new
> Variant<Map<String,Variant<Object>>>(d.GetAllProperties(), new
> DBusMapType(String.class, Variant.class));
> Map<String,Variant<Object>> mp = v.getValue();
> Iterator<String> iter = mp.keySet().iterator();
> while(iter.hasNext()) {
>                 s = iter.next();
>                 System.out.println("Property: "+s + "-
> "+mp.get(s).toString());
> }

um, this is a bit uneccessary:

   Map<String,Variant<Object>> mp = d.GetAllProperties();

then:
   
   String foo = (String) mp.get("astringkey").getValue();
   Integer bar = (Integer) mp.get("anintegerkey").getValue();
   ...

alternatively, you can iterate over the map, get the values out and then
test the type:

   if (i instanceof String) {
      ...
   }


Matt
--
www.matthew.ath.cx
D-Bus Java
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.freedesktop.org/archives/dbus/attachments/20080523/8e611b32/attachment.pgp 


More information about the dbus mailing list