aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider/key_usage_counter.vala
blob: 206cbaabb6ddc2e8f9615fe9c12362a2317a9a3a (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
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

using GLib;

namespace ContextProvider {

	/**
	 * SECTION:KeyUsageCounter
	 * @short_description: A subscription counter for keys.
	 * 
	 * A data structure which tracks the number of subscriptions for 
	 * keys.
	 */

	internal class KeyUsageCounter : GLib.Object {
	/**
	 * ContextProviderKeyUsageCounter
	 *
	 * A data structure which tracks the number of subscriptions for
	 * keys.
	 */

		// The number of subscribers for each key (over all subscriber objects)
		internal Gee.HashMap<string, int> subscriber_count_table {get; private set;}
		public StringSet subscribed_keys { get; private set; }

		public signal void keys_added (StringSet new_keys);
		public signal void keys_removed (StringSet keys_removed, StringSet keys_remaining);

		/**
		 * Constructs a new KeyUsageCounter.
		 */
		public KeyUsageCounter() {
			subscribed_keys = new StringSet();
			subscriber_count_table = new Gee.HashMap<string, int>(str_hash, str_equal);
		}

		/**
		 * @key: a string
		 * Increases the subscription count of the #key.
		 */
		public void add (StringSet keys) {
			StringSet first_subscribed_keys = new StringSet();

			foreach (var key in keys) {
				if (subscriber_count_table.contains (key)) {
					int old_value = subscriber_count_table.get (key);
					subscriber_count_table.set (key, old_value + 1);

					if (old_value == 0) {
						// This is the first subscribed to the key
						first_subscribed_keys.add (key);
					}

					debug ("Subscriber count of %s is now %d", key, old_value + 1);
				}
				else { // Key name not present in the key table
					subscriber_count_table.set (key, 1);

					first_subscribed_keys.add (key);
					debug ("Subscriber count of %s is now %d", key, 1);
				}
			}

			if (first_subscribed_keys.size() > 0) {
				keys_added (first_subscribed_keys);
			}

			subscribed_keys = new StringSet.union (subscribed_keys, first_subscribed_keys);
		}

		/**
		 * @key: a string
		 * Decreases the subscription count of the #key.
		 */
		public void remove (StringSet keys) {
			StringSet last_unsubscribed_keys = new StringSet();

			foreach (var key in keys) {
				if (subscriber_count_table.contains (key)) {
					int value = subscriber_count_table.get (key);

					if (value >= 1) {
						value--;
						subscriber_count_table.set (key, value);
					}

					if (value == 0) {
						// The last subscriber for this key unsubscribed
						last_unsubscribed_keys.add (key);
					}
					debug ("Subscriber count of %s is now %d", key, value);
				}
				else {
					// This should not happen
					debug ("Error: decreasing the subscriber count of an unknown key.");
				}
			}

			subscribed_keys = new StringSet.difference (subscribed_keys, last_unsubscribed_keys);

			if (last_unsubscribed_keys.size() > 0) {
				// Signal that some keys are not subscribed to anymore
				keys_removed (last_unsubscribed_keys, subscribed_keys);
			}

		}

		/**
		 * @key: a string
		 * Returns: The subscription count of the #key.
		 */
		public int number_of_subscribers (string key) {
			if (subscriber_count_table.contains (key)) {
				return subscriber_count_table.get (key);
			}
			return 0;
		}
	}
}