Skip to main content
summaryrefslogtreecommitdiffstats
blob: aea6742474bc5e813131cae33416d73274848f9a (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
/*******************************************************************************
 * Copyright (c) 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.db;

/**
 * This interface allows clients of the Dali db package to plug in a custom
 * strategy for comparing an identifier to the names of a collection of
 * database objects.
 * 
 * Provisional API: This interface is part of an interim API that is still
 * under development and expected to change significantly before reaching
 * stability. It is available at this early stage to solicit feedback from
 * pioneering adopters on the understanding that any code that uses this API
 * will almost certainly be broken (repeatedly) as the API evolves.
 * 
 * This interface is not intended to be implemented by clients.
 */
public interface DatabaseFinder {

	/**
	 * Select and return from the specified list of database objects the
	 * database object identified by the specified identifier.
	 */
	<T extends DatabaseObject> T selectDatabaseObjectForIdentifier(T[] databaseObjects, String identifier, DefaultCallback defaultCallback);

	/**
	 * The platform-provided finder is passed a "default" callback that can be
	 * used if appropriate.
	 */
	interface DefaultCallback {

		/**
		 * Select and return from the specified list of database objects the
		 * database object identified by the specified identifier.
		 */
		<T extends DatabaseObject> T selectDatabaseObjectForIdentifier(T[] databaseObjects, String identifier);

	}

	/**
	 * This finder searches for an exact match.
	 */
	final class Simple implements DatabaseFinder {
		public static final DatabaseFinder INSTANCE = new Simple();
		public static DatabaseFinder instance() {
			return INSTANCE;
		}
		// ensure single instance
		private Simple() {
			super();
		}
		// search for an exact match on the name
		public <T extends DatabaseObject> T selectDatabaseObjectForIdentifier(T[] databaseObjects, String identifier, DefaultCallback defaultCallback) {
			for (T databaseObject : databaseObjects) {
				if (databaseObject.getName().equals(identifier)) {
					return databaseObject;
				}
			}
			return null;
		}
		@Override
		public String toString() {
			return "DatabaseFinder.Simple"; //$NON-NLS-1$
		}
	}

	/**
	 * This finder uses the passed in callback to search the list of database objects.
	 */
	final class Default implements DatabaseFinder {
		public static final DatabaseFinder INSTANCE = new Default();
		public static DatabaseFinder instance() {
			return INSTANCE;
		}
		// ensure single instance
		private Default() {
			super();
		}
		// simply use the callback
		public <T extends DatabaseObject> T selectDatabaseObjectForIdentifier(T[] databaseObjects, String identifier, DefaultCallback defaultCallback) {
			return defaultCallback.selectDatabaseObjectForIdentifier(databaseObjects, identifier);
		}
		@Override
		public String toString() {
			return "DatabaseFinder.Default"; //$NON-NLS-1$
		}
	}

}

Back to the top