Skip to content

Commit 2e63c12

Browse files
committed
Added support for integrated managing of class instances
1 parent 12ef4c0 commit 2e63c12

1 file changed

Lines changed: 61 additions & 2 deletions

File tree

src/de/littlerolf/sav/loader/SorterLoader.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import java.net.URL;
77
import java.net.URLClassLoader;
88
import java.util.ArrayList;
9+
import java.util.Arrays;
910
import java.util.List;
1011

12+
import de.littlerolf.sav.data.BaseSorter;
13+
1114
/**
1215
* The Main Class of the Loader module, used for loading and accessing the
1316
* specific Sorters
@@ -21,6 +24,7 @@ public class SorterLoader {
2124
private boolean debug = false;
2225

2326
private List<Class> classes = new ArrayList<Class>();
27+
private List<BaseSorter> sorters = new ArrayList<BaseSorter>();
2428

2529
/**
2630
* Creates a new SorterLoader looking for classes in the given path
@@ -83,21 +87,76 @@ public void loadAllClasses() {
8387
}
8488
}
8589
}
90+
91+
public void instanciateAllClasses() {
92+
if(classes.size() == 0) {
93+
if(debug) {
94+
System.out.println("You have to call loadAllClasses() first!");
95+
}
96+
return;
97+
}
98+
for(Class c : classes) {
99+
try {
100+
sorters.add((BaseSorter) c.newInstance());
101+
} catch (InstantiationException | IllegalAccessException e) {
102+
e.printStackTrace();
103+
}
104+
}
105+
}
106+
107+
public BaseSorter getSorterByName(String name) {
108+
for (BaseSorter bs : sorters) {
109+
if(bs.getName().equals(name)) {
110+
return bs;
111+
}
112+
}
113+
return null;
114+
}
115+
116+
public String[] getAvailableSorters() {
117+
String[] packages = new String[sorters.size()];
118+
for (int i = 0; i < sorters.size(); i++) {
119+
packages[i] = sorters.get(i).getName();
120+
}
121+
if (debug) {
122+
System.out.println(Arrays.toString(packages));
123+
}
124+
return packages;
125+
}
86126

87-
public Class getSorterByPackageName(String packageName) {
127+
/**
128+
* Gets the Class of a Sorter via the Package name
129+
*
130+
* @param packageName
131+
* package name to search for
132+
* @return The found Class or null
133+
*/
134+
public Class getSorterClassByPackageName(String packageName) {
88135
for (Class c : classes) {
89136
if (c.getPackage().getName().equals(packageName)) {
137+
if (debug) {
138+
System.out.println("Found loaded class for package name: "
139+
+ packageName);
140+
}
90141
return c;
91142
}
92143
}
93144
return null;
94145
}
95146

147+
/**
148+
* Returns a full list of all available package names
149+
*
150+
* @return The list of absolute awesomeness
151+
*/
96152
public String[] getAvailablePackagesList() {
97153
String[] packages = new String[classes.size()];
98-
for (int i = 0; i < classes.size() - 1; i++) {
154+
for (int i = 0; i < classes.size(); i++) {
99155
packages[i] = classes.get(i).getPackage().getName();
100156
}
157+
if (debug) {
158+
System.out.println(Arrays.toString(packages));
159+
}
101160
return packages;
102161
}
103162

0 commit comments

Comments
 (0)