Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2005, 2010). All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.osgi.service.event;
 
 import static org.osgi.service.event.EventConstants.EVENT_TOPIC;
 
 import java.util.List;
 import java.util.Map;
 
An event. Event objects are delivered to EventHandler services which subscribe to the topic of the event.

Version:
$Id: 3f12c0b086a6cda93122ce3cb2b83e21a5023ecc $
Immutable:
 
 public class Event {
The topic of this event.
 
 	private final String	topic;
The properties carried by this event. Keys are strings and values are objects
 
 	private final EventProperties	properties;

Constructs an event.

Parameters:
topic The topic of the event.
properties The event's properties (may be null). A property whose key is not of type String will be ignored.
Throws:
java.lang.IllegalArgumentException If topic is not a valid topic name.
Since:
1.2
 
 	public Event(String topicMap<String, ? > properties) {
 		this. = topic;
 		// safely publish the event properties
 		this. = (properties instanceof EventProperties) ? (EventPropertiesproperties
 				: new EventProperties(properties);
 	}

Constructs an event.

Parameters:
topic The topic of the event.
properties The event's properties (may be null). A property whose key is not of type String will be ignored.
Throws:
java.lang.IllegalArgumentException If topic is not a valid topic name.
 
 	public Event(String topicDictionary<String, ? > properties) {
 		this. = topic;
 		// safely publish the event properties
 		this. = new EventProperties(properties);
 	}

Retrieve the value of an event property. The event topic may be retrieved with the property name "event.topics".

Parameters:
name The name of the property to retrieve.
Returns:
The value of the property, or null if not found.
 
 	public final Object getProperty(String name) {
 		if (.equals(name)) {
 			return ;
 		}
 		return .get(name);
 	}

Indicate the presence of an event property. The event topic is present using the property name "event.topics".

Parameters:
name The name of the property.
Returns:
true if a property with the specified name is in the event. This property may have a null value. false otherwise.
Since:
1.3
	public final boolean containsProperty(String name) {
		if (.equals(name)) {
			return true;
		}
		return .containsKey(name);
	}

Returns a list of this event's property names. The list will include the event topic property name "event.topics".

Returns:
A non-empty array with one element per property.
	public final String[] getPropertyNames() {
		int size = .size();
		String[] result = new String[size + 1];
		result[size] = ;
		return result;
	}

Returns the topic of this event.

Returns:
The topic of this event.
	public final String getTopic() {
		return ;
	}

Tests this event's properties against the given filter using a case sensitive match.

Parameters:
filter The filter to test.
Returns:
true If this event's properties match the filter, false otherwise.
	public final boolean matches(Filter filter) {
		return filter.matchCase(new FilterProperties());
	}

Compares this Event object to another object.

An event is considered to be equal to another event if the topic is equal and the properties are equal. The properties are compared using the java.util.Map.equals() rules which includes identity comparison for array values.

Parameters:
object The Event object to be compared.
Returns:
true if object is a Event and is equal to this object; false otherwise.
	public boolean equals(Object object) {
		if (object == this) { // quick test
			return true;
		}
		if (!(object instanceof Event)) {
			return false;
		}
		Event event = (Eventobject;
		return .equals(event.topic) && .equals(event.properties);
	}

Returns a hash code value for this object.

Returns:
An integer which is a hash code value for this object.
	public int hashCode() {
		int h = 31 * 17 + .hashCode();
		h = 31 * h + .hashCode();
		return h;
	}

Returns the string representation of this event.

Returns:
The string representation of this event.
	public String toString() {
		return getClass().getName() + " [topic=" +  + "]";
	}

Called by the constructor to validate the topic name.

Parameters:
topic The topic name to validate.
Throws:
java.lang.IllegalArgumentException If the topic name is invalid.
	private static void validateTopicName(String topic) {
		char[] chars = topic.toCharArray();
		int length = chars.length;
		if (length == 0) {
			throw new IllegalArgumentException("empty topic");
		}
		for (int i = 0; i < lengthi++) {
			char ch = chars[i];
			if (ch == '/') {
				// Can't start or end with a '/' but anywhere else is okay
				if (i == 0 || (i == length - 1)) {
					throw new IllegalArgumentException("invalid topic: "
topic);
				}
				// Can't have "//" as that implies empty token
				if (chars[i - 1] == '/') {
					throw new IllegalArgumentException("invalid topic: "
topic);
				}
				continue;
			}
			if (('A' <= ch) && (ch <= 'Z')) {
				continue;
			}
			if (('a' <= ch) && (ch <= 'z')) {
				continue;
			}
			if (('0' <= ch) && (ch <= '9')) {
				continue;
			}
			if ((ch == '_') || (ch == '-')) {
				continue;
			}
			throw new IllegalArgumentException("invalid topic: " + topic);
		}
	}

Dictionary to use for Filter matching.
	static private final class FilterProperties extends
		private final String			topic;
		private final EventProperties	properties;
		FilterProperties(String topicEventProperties properties) {
			this. = topic;
			this. = properties;
		}
			List<Objectresult = new ArrayList<Object>(values.size() + 1);
			result.add();
			result.addAll(values);
			return Collections.enumeration(result);
		}
		public Object get(Object key) {
			if (.equals(key)) {
				return ;
			}
			return .get(key);
		}
		public boolean isEmpty() {
			return false;
		}
		public Enumeration<Stringkeys() {
			List<Stringresult = new ArrayList<String>(keys.size() + 1);
			result.addAll(keys);
			return Collections.enumeration(result);
		}
		public Object put(String keyObject value) {
		}
		public Object remove(Object key) {
		}
		public int size() {
			return .size() + 1;
		}
	}
New to GrepCode? Check out our FAQ X