Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1143a1207792e6fdfbd719e59175af12dc656c6d (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
/******************************************************************************
 * Copyright (c) 2016 Alex Blewitt 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:
 *     Alex Blewitt - initial API and implementation
 ******************************************************************************/
package org.eclipse.osgi.internal.loader.classpath;

/**
 * Stores a pair of {@link TitleVersionVendor} package attributes for the Implementation- and Specification- entries.
 */
class ManifestPackageAttributes {
	/**
	 * Constant used when no titles, versions or vendors are specified for the package.
	 */
	static final ManifestPackageAttributes NONE = new ManifestPackageAttributes(TitleVersionVendor.NONE, TitleVersionVendor.NONE);
	private final TitleVersionVendor implementation;
	private final TitleVersionVendor specification;

	/**
	 * Utility method to return the first version, or the second if it is null
	 * @param first the string to return if not null
	 * @param second the fallback value if the first is null
	 * @return the first value, or the second value if the first is null
	 */
	private static String or(String first, String second) {
		return first == null ? second : first;
	}

	/**
	 * Factory method for creating ManifestPackageAttributes.  If any of the given title,
	 * version, or vendor values are <code>null</code> then the defaultAttributes will be
	 * used.  If the defaultAttributes is null or returns all <code>null</code> values then
	 * <code>null</code> values will be used.  If <code>null</code> values are used for all of the 
	 * versions, titles, and vendors then {@link #NONE} is returned. 
	 * @param specificationTitle the package specification title
	 * @param specificationVersion the package specification version
	 * @param specificationVendor the package specification vendor
	 * @param implementationTitle the package implementation title
	 * @param implementationVersion the package implementation version
	 * @param implementationVendor the package implementation vendor
	 * @param defaultAttributes the default attributes to use when the specified title, version or vendor is <code>null</code>.
	 * @return
	 */
	static ManifestPackageAttributes of(String specificationTitle, String specificationVersion, String specificationVendor, String implementationTitle, String implementationVersion, String implementationVendor, ManifestPackageAttributes defaultAttributes) {
		if (defaultAttributes == null) {
			defaultAttributes = NONE;
		}
		return of(//
				or(specificationTitle, defaultAttributes.getSpecification().getTitle()), //
				or(specificationVersion, defaultAttributes.getSpecification().getVersion()), //
				or(specificationVendor, defaultAttributes.getSpecification().getVendor()), //
				or(implementationTitle, defaultAttributes.getImplementation().getTitle()), //
				or(implementationVersion, defaultAttributes.getImplementation().getVersion()), //
				or(implementationVendor, defaultAttributes.getImplementation().getVendor())//
		);
	}

	private static ManifestPackageAttributes of(String specificationTitle, String specificationVersion, String specificationVendor, String implementationTitle, String implementationVersion, String implementationVendor) {
		TitleVersionVendor specification = TitleVersionVendor.of(specificationTitle, specificationVersion, specificationVendor);
		TitleVersionVendor implementation = TitleVersionVendor.of(implementationTitle, implementationVersion, implementationVendor);
		if (specification == TitleVersionVendor.NONE && implementation == TitleVersionVendor.NONE) {
			return NONE;
		}
		return new ManifestPackageAttributes(implementation, specification);
	}

	private ManifestPackageAttributes(TitleVersionVendor implementation, TitleVersionVendor specification) {
		if (implementation == null || specification == null) {
			throw new IllegalArgumentException();
		}
		this.implementation = implementation;
		this.specification = specification;
	}

	/**
	 * Returns the title, version and vendor for the package implementation.
	 * @return the title, version and vendor for the package implemetnation.
	 */
	TitleVersionVendor getImplementation() {
		return implementation;
	}

	/**
	 * Returns the title, version and vendor for the package specification.
	 * @return the title, version and vendor for the package specification.
	 */
	TitleVersionVendor getSpecification() {
		return specification;
	}
}

Back to the top