Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2160964e4222b4f1c97cda2780212afdab859331 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.core.identity;

import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Namespace base class
 * 
 * All implementers of the 'namespace' extension point must
 * implement a subclass of this Namespace class
 * 
 * This class defines the properties associated with an
 * identity Namespace. Namespaces are defined by a unique 'name' (e.g. 'email',
 * 'icq', 'aolim').
 * 
 */
public abstract class Namespace implements Serializable {
	private static final long serialVersionUID = 3976740272094720312L;
	private String name;
	private String description;
	private int hashCode;
	private boolean isInitialized = false;

	public Namespace() {
	}

	public final boolean initialize(String name, String desc) {
		if (name == null)
			throw new RuntimeException(new InstantiationException(
					"Namespace<init> name cannot be null"));
		if (!isInitialized) {
			this.name = name;
			this.description = desc;
			this.hashCode = name.hashCode();
			return true;
		} else
			return false;
	}

	public Namespace(String name, String desc) {
		initialize(name, desc);
	}

	/**
	 * Override of Object.equals. This equals method returns true if the
	 * provided Object is also a Namespace instance, and the names of the two
	 * instances match.
	 * 
	 * @param other
	 *            the Object to test for equality
	 */
	public boolean equals(Object other) {
		if (!(other instanceof Namespace))
			return false;
		return ((Namespace) other).name.equals(name);
	}

	public int hashCode() {
		return hashCode;
	}

	protected boolean testIDEquals(BaseID first, BaseID second) {
		// First check that namespaces are the same and non-null
		Namespace sn = second.getNamespace();
		if (sn == null || !this.equals(sn))
			return false;
		return first.namespaceEquals(second);
	}

	protected String getNameForID(BaseID id) {
		return id.namespaceGetName();
	}

	protected URI getURIForID(BaseID id) throws URISyntaxException {
		return id.namespaceToURI();
	}

	protected int getCompareToForObject(BaseID first, BaseID second) {
		return first.namespaceCompareTo(second);
	}

	protected int getHashCodeForID(BaseID id) {
		return id.namespaceHashCode();
	}

	/**
	 * Get the name of this namespace.  May not return null.
	 * 
	 * @return String name of Namespace instance
	 * 
	 */
	public String getName() {
		return name;
	}
	/**
	 * Get the description, associated with this Namespace.  The returned value may be null.
	 * @return the description associated with this Namespace
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * Make an instance of this namespace.  Namespace subclasses, provided by plugins
	 * must implement this method to construct ID instances for the given namespace.
	 * 
	 * @param argTypes a Class[] of classes defining the types of the args.  May be null if
	 * no arguments given.  The arity and classes provided must correspond to the types of 
	 * the associated arguments
	 * @param args an Object[] of arguments for creating ID instances.  May be null if no
	 * arguments provided
	 * @return a non-null ID instance.  The class used may extend BaseID or may implement
	 * the ID interface directly
	 * @throws IDInstantiationException if construction fails
	 */
	public abstract ID createInstance(Class[] argTypes, Object[] args)
			throws IDInstantiationException;

	/**
	 * Get the URI scheme associated with this namespace instance.  For example,
	 * a namespace with name "ecf.http" would have an associated scheme identifier of "http".
	 * Subclasses must provide an implementation that returns a valid non-null scheme 
	 * identifier.
	 * 
	 * @return a String scheme identifier
	 */
	public abstract String getScheme();
	
	public String toString() {
		StringBuffer b = new StringBuffer("Namespace[");
		b.append("name=").append(name).append(";");
		b.append("scheme=").append(getScheme()).append(";");
		b.append("description=").append("]");
		return b.toString();
	}
}

Back to the top