Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9d660400e5bce275d76225958cc722cd732022f8 (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.p2.repository.artifact;

import java.util.Iterator;
import java.util.Properties;
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.equinox.p2.query.MatchQuery;
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor;

/**
 * A general purpose query for matching {@link IArtifactDescriptor} instances
 * that satisfy various criteria.
 * 
 * @since 2.0
 */
public class ArtifactDescriptorQuery extends MatchQuery<IArtifactDescriptor> {

	/**
	 * A singleton query that will match all instances of {@link IArtifactDescriptor}.
	 */
	public static final ArtifactDescriptorQuery ALL_DESCRIPTORS = new ArtifactDescriptorQuery();

	private ArtifactDescriptor descriptor = null;
	private String format = null;
	private String id = null;
	private Properties properties = null;
	private VersionRange range = null;
	private IArtifactRepository repository = null;

	/**
	 * Clients must use {@link #ALL_DESCRIPTORS}.
	 */
	private ArtifactDescriptorQuery() {
		//matches everything
	}

	/**
	 * The query will match candidate descriptors where:
	 * <pre>
	 *     new ArtifactDescriptor(descriptor).equals(new ArtifactDescriptor(candidate))
	 * </pre>
	 * @param descriptor The descriptor to match
	 */
	public ArtifactDescriptorQuery(IArtifactDescriptor descriptor) {
		this.descriptor = (descriptor.getClass() == ArtifactDescriptor.class) ? (ArtifactDescriptor) descriptor : new ArtifactDescriptor(descriptor);
	}

	/**
	 * The query will match descriptors with the given id, version and format
	 * If any parameter is null, that attribute will be ignored.
	 * 
	 * @param id the descriptor id to match, or <code>null</code> to match any id
	 * @param versionRange the descriptor version range to match or <code>null</code> to match
	 * any version range
	 * @param format the descriptor {@link IArtifactDescriptor#FORMAT} value to match, or <code>null</code> to
	 * match any descriptor format
	 */
	public ArtifactDescriptorQuery(String id, VersionRange versionRange, String format) {
		this(id, versionRange, format, null);
	}

	/**
	 * The query will match descriptors with the given id, version range, format and repository
	 * if any parameter is null, that attribute will be ignored.
	 * 
	 * @param id the descriptor id to match, or <code>null</code> to match any id
	 * @param versionRange the descriptor version range to match or <code>null</code> to match
	 * any version range
	 * @param format the descriptor {@link IArtifactDescriptor#FORMAT} value to match, or <code>null</code> to
	 * match any descriptor format
	 * @param repository The repository of the descriptor to match, or <code>null</code>
	 * to match descriptors from any repository
	 */
	public ArtifactDescriptorQuery(String id, VersionRange versionRange, String format, IArtifactRepository repository) {
		this.id = id;
		this.range = versionRange;
		this.format = format;
		this.repository = repository;
	}

	/*(non-Javadoc)
	 * @see org.eclipse.equinox.p2.query.MatchQuery#isMatch(java.lang.Object)
	 */
	public boolean isMatch(IArtifactDescriptor candidate) {
		if (descriptor != null)
			return matchDescriptor(candidate);

		if (id != null && !id.equals(candidate.getArtifactKey().getId()))
			return false;

		if (range != null && !range.isIncluded(candidate.getArtifactKey().getVersion()))
			return false;

		if (format != null && !format.equals(candidate.getProperty(IArtifactDescriptor.FORMAT)))
			return false;

		if (repository != null && repository != candidate.getRepository())
			return false;

		if (properties != null) {
			for (Iterator<Object> iterator = properties.keySet().iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				String value = properties.getProperty(key);
				if (value != null && !value.equals(candidate.getProperty(key)))
					return false;
			}
		}
		return true;
	}

	private boolean matchDescriptor(IArtifactDescriptor candidate) {
		ArtifactDescriptor candidateDescriptor = (candidate.getClass() == ArtifactDescriptor.class) ? (ArtifactDescriptor) candidate : new ArtifactDescriptor(candidate);
		return descriptor.equals(candidateDescriptor);
	}

	/**
	 * Sets the properties that this query should match against. This query will only match
	 * descriptors that have property keys and values matching those provided here.
	 * @param properties The properties to query for
	 */
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
}

Back to the top