summaryrefslogtreecommitdiff
path: root/org/freedesktop/dbus/DBusMap.java
blob: 2ac65f1f789a32371581b6d359e60b7f003cff78 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
   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 Lesser 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.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

class DBusMap<K, V> implements Map<K, V>
{
   private Object[][] entries;
   public DBusMap(Object[][] entries)
   {
      this.entries=entries;
   }
   class Entry implements Map.Entry<K,V>, Comparable<Entry>
   {
      private int entry;
      public Entry(int i)
      {
         this.entry = i;
      }
      public boolean  equals(Object o)
      {
         if (null == o) return false;
         if (!(o instanceof DBusMap.Entry)) return false;
         return this.entry == ((Entry) o).entry;
      }
      @SuppressWarnings("unchecked")
      public K getKey()
      {
         return (K) entries[entry][0];
      }
      @SuppressWarnings("unchecked")
      public V getValue()
      {
         return (V) entries[entry][1];
      }
      public int hashCode()
      {
         return entries[entry][0].hashCode();
      }
      public V setValue(V value)
      {
         throw new UnsupportedOperationException();
      }
      public int compareTo(Entry e)
      {
         return entry - e.entry;
      }
   }

   public void clear()
   {
      throw new UnsupportedOperationException();
   }
   public boolean containsKey(Object key)
   {
      for (int i = 0; i < entries.length; i++)
         if (key == entries[i][0] || (key != null && key.equals(entries[i][0])))
            return true;
      return false;
   }
   public boolean containsValue(Object value)
   {
      for (int i = 0; i < entries.length; i++)
         if (value == entries[i][1] || (value != null && value.equals(entries[i][1])))
            return true;
      return false;
   }
   public Set<Map.Entry<K,V>> entrySet()
   {
      Set<Map.Entry<K,V>> s = new TreeSet<Map.Entry<K,V>>();
      for (int i = 0; i < entries.length; i++) 
         s.add(new Entry(i));
      return s;
   }
   @SuppressWarnings("unchecked")
   public V get(Object key)
   {
      for (int i = 0; i < entries.length; i++)
         if (key == entries[i][0] || (key != null && key.equals(entries[i][0])))
            return (V) entries[i][1];
      return null;
   }
   public boolean isEmpty() 
   { 
      return entries.length == 0;
   }
   @SuppressWarnings("unchecked")
   public Set<K> keySet()
   {
      Set<K> s = new TreeSet<K>();
      for (Object[] entry: entries)
         s.add((K) entry[0]);
      return s;
   }
   public V put(K key, V value)
   {
      throw new UnsupportedOperationException();
   }
   public void putAll(Map<? extends K,? extends V> t)
   {
      throw new UnsupportedOperationException();
   }
   public V remove(Object key)
   {
      throw new UnsupportedOperationException();
   }
   public int size()
   {
      return entries.length;
   }
   @SuppressWarnings("unchecked")
   public Collection<V> values()
   {
      List<V> l = new Vector<V>();
      for (Object[] entry: entries)
         l.add((V) entry[1]);
      return l;
   }
   public int hashCode() 
   {
      return Arrays.deepHashCode(entries);
   }
   @SuppressWarnings("unchecked")
   public boolean equals(Object o) 
   {
      if (null == o) return false;
      if (!(o instanceof Map)) return false;
      return ((Map<K,V>) o).entrySet().equals(entrySet());
   }
   public String toString()
   {
      String s = "{ ";
      for (int i = 0; i < entries.length; i++) 
         s += entries[i][0] + " => " + entries[i][1] + ",";
      return s.replaceAll(".$", " }");
   }
}