Skip to content

Commit d704182

Browse files
committed
starting to implement library manager as a tree
1 parent ef9090e commit d704182

File tree

2 files changed

+480
-138
lines changed

2 files changed

+480
-138
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package it.baeyens.arduino.managers;
2+
3+
import java.util.Collection;
4+
import java.util.Set;
5+
import java.util.TreeMap;
6+
import java.util.TreeSet;
7+
8+
public class LibraryTree {
9+
private TreeMap<String, Category> categories = new TreeMap<>();
10+
11+
public static interface Node {
12+
boolean hasChildren();
13+
Object[] getChildren();
14+
Object getParent();
15+
String getName();
16+
}
17+
18+
public class Category implements Comparable<Category>, Node {
19+
private String name;
20+
private TreeMap<String, Library> libraries = new TreeMap<>();
21+
22+
public Category(String name) {
23+
this.name = name;
24+
}
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public Collection<Library> getLibraries() {
30+
return libraries.values();
31+
}
32+
33+
@Override
34+
public int compareTo(Category other) {
35+
return name.compareTo(other.name);
36+
}
37+
@Override
38+
public boolean hasChildren() {
39+
return !libraries.isEmpty();
40+
}
41+
@Override
42+
public Object[] getChildren() {
43+
return libraries.values().toArray();
44+
}
45+
@Override
46+
public Object getParent() {
47+
return LibraryTree.this;
48+
}
49+
}
50+
51+
public class Library implements Comparable<Library>, Node {
52+
private String name;
53+
private Category category;
54+
private TreeSet<Version> versions = new TreeSet<>();
55+
private String installed;
56+
57+
public Library(Category category, String name) {
58+
this.category = category;
59+
this.name = name;
60+
}
61+
62+
public Collection<Version> getVersions() {
63+
return versions;
64+
}
65+
66+
public String getName() {
67+
return name;
68+
}
69+
70+
public Category getCategory() {
71+
return category;
72+
}
73+
74+
public String getInstalled() {
75+
return installed;
76+
}
77+
78+
public String getLatest() {
79+
return versions.last().toString();
80+
}
81+
82+
@Override
83+
public int compareTo(Library other) {
84+
return name.compareTo(other.name);
85+
}
86+
87+
@Override
88+
public boolean hasChildren() {
89+
return false;
90+
}
91+
92+
@Override
93+
public Object[] getChildren() {
94+
return null;
95+
}
96+
97+
@Override
98+
public Object getParent() {
99+
return category;
100+
}
101+
}
102+
103+
public class Version implements Comparable<Object> {
104+
private String[] parts;
105+
public Version(String version) {
106+
parts = version.split("\\.");
107+
}
108+
109+
@Override
110+
public int compareTo(Object other) {
111+
if (other instanceof String) {
112+
return this.compareTo(new Version((String)other));
113+
} else if (other instanceof Version) {
114+
return this.compareParts(((Version)other).parts, 0);
115+
} else {
116+
throw new UnsupportedOperationException();
117+
}
118+
}
119+
120+
private int compareParts(String[] other, int level) {
121+
if (parts.length > level && other.length > level) {
122+
if (parts[level].compareTo(other[level]) == 0) {
123+
return this.compareParts(other, ++level);
124+
} else {
125+
try {
126+
return new Integer(parts[level]).compareTo(Integer.parseInt(other[level]));
127+
} catch (Exception e) {
128+
return parts[level].compareTo(other[level]);
129+
}
130+
}
131+
} else {
132+
return parts.length > other.length ? 1 : -1;
133+
}
134+
}
135+
136+
public String toString() {
137+
return String.join(".", parts);
138+
}
139+
}
140+
141+
public LibraryTree() {
142+
LibraryIndex libraryIndex = Manager.getLibraryIndex();
143+
144+
for (String categoryName : libraryIndex.getCategories()) {
145+
Category category = new Category(categoryName);
146+
for (it.baeyens.arduino.managers.Library library : libraryIndex.getLibraries(categoryName)) {
147+
Library lib = category.libraries.get(library.getName());
148+
if (lib == null) {
149+
lib = new Library(category, library.getName());
150+
category.libraries.put(lib.getName(), lib);
151+
}
152+
lib.versions.add(new Version(library.getVersion()));
153+
if (library.isInstalled()) {
154+
lib.installed = library.getVersion();
155+
}
156+
}
157+
158+
categories.put(category.getName(), category);
159+
}
160+
}
161+
162+
public Collection<Category> getCategories() {
163+
return categories.values();
164+
}
165+
166+
public Collection<Library> getAllLibraries() {
167+
Set<Library> all = new TreeSet<>();
168+
for (Category category : categories.values()) {
169+
all.addAll(category.getLibraries());
170+
}
171+
return all;
172+
}
173+
}

0 commit comments

Comments
 (0)