summaryrefslogtreecommitdiff
path: root/org/freedesktop/dbus/Container.java
blob: 55df41ddbcd7917e0985c1be79426ef3de469840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
   D-Bus Java Implementation
   Copyright (c) 2005-2006 Matthew Johnson

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the COPYING file with this program.
*/
package org.freedesktop.dbus;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
 * This class is the super class of both Structs and Tuples 
 * and holds common methods.
 */
abstract class Container
{
   private static Map<Type,Type[]> typecache = new HashMap<Type,Type[]>();
   static void putTypeCache(Type k, Type[] v)
   {
      typecache.put(k, v);
   }
   static Type[] getTypeCache(Type k)
   {
      return typecache.get(k);
   }
   private Object[] parameters = null;
   public Container() {}
   private void setup()
   {
      Field[] fs = getClass().getDeclaredFields();
      Object[] args = new Object[fs.length];

      int diff = 0;
      for (Field f : fs) {
         Position p = f.getAnnotation(Position.class);
         if (null == p) {
            diff++;
            continue;
         }
         try {
            args[p.value()] = f.get(this);
         } catch (IllegalAccessException IAe) {}
      }
         
      this.parameters = new Object[args.length - diff];
      System.arraycopy(args, 0, parameters, 0, parameters.length);
   }
   /**
    * Returns the struct contents in order.
    * @throws DBusException If there is  a problem doing this.
    */
   public final Object[] getParameters()
   {
      if (null != parameters) return parameters;
      setup();
      return parameters;
   }
   /** Returns this struct as a string. */
   public final String toString()
   {
      String s = getClass().getName()+"<";
      if (null == parameters)
         setup();
      if (0 == parameters.length) 
         return s+">";
      for (Object o: parameters)
         s += o+", ";
      return s.replaceAll(", $", ">");
   }
   public final boolean equals(Object other)
   {
      if (other instanceof Container)  {
         Container that = (Container) other;
         if (this.getClass().equals(that.getClass()))
            return Arrays.equals(this.getParameters(), that.getParameters());
         else return false;
      }
      else return false;
   }
}