File: gitg-ext-message-id.vala

package info (click to toggle)
gitg 41-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,876 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (179 lines) | stat: -rw-r--r-- 4,049 bytes parent folder | download | duplicates (5)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * This file is part of gitg
 *
 * Copyright (C) 2012 - Jesse van den Kieboom
 *
 * gitg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * gitg 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
 */

namespace GitgExt
{

/**
 * Message identifier object.
 *
 * The message identifier object is used to identify messages sent over the
 * MessageBus. The message identifier contains an object path and a method.
 * Both are simple strings and combined describe the location of a message as
 * a kind of method on an object.
 *
 * Valid object paths start with a forward slash and further path elements are
 * seperated by more forward slashes. The first element needs to start with
 * an alpha character (or underscore) while further characters can be
 * alpha numeric or underscores. An example of a valid path is:
 *
 * /path/to/object
 *
 * Method names on the other hand do not have any restrictions.
 *
 */
public class MessageId : Object
{
	/**
	 * Message object path.
	 */
	public string object_path { construct set; get; }

	/**
	 * Message method.
	 */
	public string method { construct set; get; }

	/**
	 * Full id of the message.
	 *
	 * Get the full id of the message identifier. The full id is simply
	 * <path>.<method>
	 *
	 */
	public string id
	{
		owned get { return object_path + "." + method; }
	}

	/**
	 * Message hash.
	 *
	 * Get a hash for the message identifier suitable for use in a hash table.
	 * The hash is simply a string hash of the full id of the message identifier.
	 *
	 * @return the message identifier hash.
	 *
	 */
	public uint hash()
	{
		return id.hash();
	}

	/**
	 * Compare two messages for equality.
	 *
	 * Compare two messages. Two message identifiers are equal when they have
	 * the same object path and the same method name.
	 *
	 * @param other the message identifier to compare to.
	 *
	 * @return true if the message identifiers are equal, false otherwise.
	 *
	 */
	public bool equal(MessageId other)
	{
		return id == other.id;
	}

	/**
	 * Construct message identifier with object path and method.
	 *
	 * Create a new message identifier object with the given object path and
	 * method name.
	 *
	 * @param object_path the object path
	 * @param method the method name
	 *
	 * @return a new message identifier.
	 *
	 */
	public MessageId(string object_path, string method)
	{
		Object(object_path: object_path, method: method);
	}

	/**
	 * Create a copy of the message identifier.
	 *
	 * Create an exact copy of the message identifier.
	 *
	 * @return a new message identifier.
	 *
	 */
	public MessageId copy()
	{
		return new MessageId(object_path, method);
	}

	/**
	 * Check whether an object path is a valid path.
	 *
	 * Check whether the given path is a valid object path. A valid object path
	 * starts with a forward slash, followed by at least one alpha character,
	 * or underscore. Further valid characters include alphanumeric characters,
	 * underscores or path separators (forward slash).
	 *
	 * Example: /path/to/object
	 *
	 * @return true if the specified path is valid, false otherwise
	 *
	 */
	public static bool valid_object_path(string path)
	{
		if (path == null)
		{
			return false;
		}

		if (path[0] != '/')
		{
			return false;
		}

		int i = 0;

		while (i < path.length)
		{
			var c = path[i];

			if (c == '/')
			{
				++i;

				if (i == path.length || !(c.isalpha() || c == '_'))
				{
					return false;
				}
			}
			else if (!(c.isalnum() || c == '_'))
			{
				return false;
			}

			++i;
		}

		return true;
	}
}

}

// ex:set ts=4 noet: