Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3747d1442ef3568acf18e9029b65d4f4b0e3bdff (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
package org.eclipse.cdt.arduino.core.internal.board;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LibraryIndex {

	private List<ArduinoLibrary> libraries;

	// category name to library name
	private Map<String, Set<String>> categories = new HashMap<>();
	// library name to latest version of library
	private Map<String, ArduinoLibrary> latestLibs = new HashMap<>();

	public void resolve() {
		for (ArduinoLibrary library : libraries) {
			String name = library.getName();

			String category = library.getCategory();
			if (category == null) {
				category = "Uncategorized"; //$NON-NLS-1$
			}

			Set<String> categoryLibs = categories.get(category);
			if (categoryLibs == null) {
				categoryLibs = new HashSet<>();
				categories.put(category, categoryLibs);
			}
			categoryLibs.add(name);

			ArduinoLibrary current = latestLibs.get(name);
			if (current != null) {
				if (ArduinoManager.compareVersions(library.getVersion(), current.getVersion()) > 0) {
					latestLibs.put(name, library);
				}
			} else {
				latestLibs.put(name, library);
			}
		}
	}

	public ArduinoLibrary getLibrary(String name) {
		return latestLibs.get(name);
	}

	public Collection<String> getCategories() {
		return Collections.unmodifiableCollection(categories.keySet());
	}

	public Collection<ArduinoLibrary> getLibraries(String category) {
		Set<String> categoryLibs = categories.get(category);
		if (categoryLibs == null) {
			return new ArrayList<>(0);
		}

		List<ArduinoLibrary> libs = new ArrayList<>(categoryLibs.size());
		for (String name : categoryLibs) {
			libs.add(latestLibs.get(name));
		}
		return libs;
	}

}

Back to the top