Reformatted SortedModuleList to SortedListModule
This commit is contained in:
@@ -14,64 +14,24 @@ import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
import de.sebse.fuplanner.tools.Compare;
|
||||
import de.sebse.fuplanner.tools.Regex;
|
||||
|
||||
/**
|
||||
* Created by sebastian on 29.10.17.
|
||||
*/
|
||||
|
||||
public class Modules implements Iterable<Modules.Module>, Serializable {
|
||||
private SortedModuleList list;
|
||||
private String latestSemester = null;
|
||||
private SortedListModule list;
|
||||
private LoginToken token;
|
||||
//private transient Logger log = new Logger(this);
|
||||
private static final String FILE_NAME = "ModuleListSaving";
|
||||
|
||||
public Modules(LoginToken loginToken) {
|
||||
this.token = loginToken;
|
||||
this.list = new SortedModuleList();
|
||||
this.list = new SortedListModule();
|
||||
}
|
||||
|
||||
public Module addModule(String semester, HashSet<String> lvNumber, String title, HashSet<Lecturer> lecturer, String type, String description, String ID) {
|
||||
public void addModule(String semester, HashSet<String> lvNumber, String title, HashSet<Lecturer> lecturer, String type, String description, String ID) {
|
||||
Module m = new Module(semester, lvNumber, title, lecturer, type, description, ID);
|
||||
this.list.add(m);
|
||||
try {
|
||||
setLatestSemester(m.semester);
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
private void setLatestSemester(String semester) throws NoSuchFieldException {
|
||||
if (compareSemester(this.latestSemester, semester) == Compare.LARGER)
|
||||
this.latestSemester = semester;
|
||||
}
|
||||
|
||||
private Compare compareSemester(String a, String b) throws NoSuchFieldException {
|
||||
if (a == null && b == null)
|
||||
return Compare.EQUAL;
|
||||
if (a == null)
|
||||
return Compare.LARGER;
|
||||
if (b == null)
|
||||
return Compare.SMALLER;
|
||||
|
||||
String s1type = Regex.regex("(SS|WS)", a);
|
||||
int s1year = Integer.parseInt(Regex.regex("(SS|WS) ([0-9]{2})", a, 2));
|
||||
String s2type = Regex.regex("(SS|WS)", b);
|
||||
int s2year = Integer.parseInt(Regex.regex("(SS|WS) ([0-9]{2})", b, 2));
|
||||
|
||||
if (s1year == s2year) {
|
||||
if (s1type.equals(s2type))
|
||||
return Compare.EQUAL;
|
||||
if (s1type.equals("SS"))
|
||||
return Compare.SMALLER;
|
||||
return Compare.LARGER;
|
||||
}
|
||||
if (s1year < s2year)
|
||||
return Compare.LARGER;
|
||||
return Compare.SMALLER;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,7 +46,7 @@ public class Modules implements Iterable<Modules.Module>, Serializable {
|
||||
}
|
||||
|
||||
public Iterator<Module> latestSemesterIterator() {
|
||||
return this.list.semesterIterator(latestSemester);
|
||||
return this.list.filteredIterator(this.list.getLatestSemester());
|
||||
}
|
||||
|
||||
public int size() {
|
||||
@@ -94,7 +54,7 @@ public class Modules implements Iterable<Modules.Module>, Serializable {
|
||||
}
|
||||
|
||||
public Module get(String id) {
|
||||
return this.list.get(id);
|
||||
return this.list.getById(id);
|
||||
}
|
||||
|
||||
public Module getByIndex(int index) {
|
||||
@@ -127,11 +87,10 @@ public class Modules implements Iterable<Modules.Module>, Serializable {
|
||||
}
|
||||
|
||||
public void updateList(Modules modules) {
|
||||
this.latestSemester = modules.latestSemester;
|
||||
SortedModuleList old = this.list;
|
||||
SortedListModule old = this.list;
|
||||
this.list = modules.list;
|
||||
for (Module oldModule : old) {
|
||||
Module newModule = this.list.get(oldModule.getID());
|
||||
Module newModule = this.list.getById(oldModule.getID());
|
||||
if (newModule != null) {
|
||||
newModule.announcements = oldModule.announcements;
|
||||
newModule.assignments = oldModule.assignments;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package de.sebse.fuplanner.services.KVV.types;
|
||||
|
||||
import de.sebse.fuplanner.tools.Regex;
|
||||
import de.sebse.fuplanner.tools.SortedList;
|
||||
|
||||
public class SortedListModule extends SortedList<Modules.Module, String, String> {
|
||||
private static final int LARGER = 1;
|
||||
private static final int EQUAL = 0;
|
||||
private static final int SMALLER = -1;
|
||||
|
||||
@Override
|
||||
public int compare(Modules.Module o1, Modules.Module o2) {
|
||||
int semester;
|
||||
try {
|
||||
semester = -compareSemester(o1.semester, o2.semester);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
semester = EQUAL;
|
||||
}
|
||||
if (semester != EQUAL)
|
||||
return semester;
|
||||
return o1.title.compareToIgnoreCase(o2.title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Modules.Module e) {
|
||||
super.add(e);
|
||||
}
|
||||
|
||||
public String getLatestSemester() {
|
||||
if (size() > 0)
|
||||
//noinspection ConstantConditions
|
||||
return this.get(0).semester;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int compareSemester(String a, String b) throws NoSuchFieldException {
|
||||
//Logger log = new Logger("SortedListModule");
|
||||
//log.d("compare", a, b);
|
||||
if (a == null && b == null)
|
||||
return EQUAL;
|
||||
if (a == null)
|
||||
return SMALLER;
|
||||
if (b == null)
|
||||
return LARGER;
|
||||
|
||||
String s1type = Regex.regex("(S|WS)", a);
|
||||
int s1year = Integer.parseInt(Regex.regex("(S|WS) ([0-9]{2})", a, 2));
|
||||
String s2type = Regex.regex("(S|WS)", b);
|
||||
int s2year = Integer.parseInt(Regex.regex("(S|WS) ([0-9]{2})", b, 2));
|
||||
|
||||
if (s1year == s2year) {
|
||||
if (s1type.equals(s2type))
|
||||
return EQUAL;
|
||||
return s1type.equals("SS") ? SMALLER : LARGER;
|
||||
}
|
||||
return s1year < s2year ? SMALLER : LARGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasIdentifier(Modules.Module o1, String id) {
|
||||
return o1.getID().equals(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasFilter(Modules.Module o1, String filter) {
|
||||
return o1.semester.equals(filter);
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package de.sebse.fuplanner.services.KVV.types;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.AbstractList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class SortedModuleList extends AbstractList<Modules.Module> implements Serializable {
|
||||
|
||||
private ArrayList<Modules.Module> internalList = new ArrayList<>();
|
||||
|
||||
// Note that add(E e) in AbstractList is calling this one
|
||||
@Override
|
||||
public void add(int position, Modules.Module e) {
|
||||
internalList.add(e);
|
||||
Collections.sort(internalList, (o1, o2) -> {
|
||||
int semester = o1.semester.compareTo(o2.semester);
|
||||
if (semester != 0)
|
||||
return semester;
|
||||
return o1.title.compareToIgnoreCase(o2.title);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Modules.Module get(int i) {
|
||||
return internalList.get(i);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Modules.Module get(String id) {
|
||||
for (Modules.Module module : this.internalList) {
|
||||
if (module.getID().equals(id))
|
||||
return module;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return internalList.size();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Iterator<Modules.Module> iterator() {
|
||||
return new Iterator<Modules.Module>() {
|
||||
private int pos = -1;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return pos+1 < internalList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Modules.Module next() {
|
||||
pos++;
|
||||
if (pos < internalList.size())
|
||||
return internalList.get(pos);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("no changes allowed");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Iterator<Modules.Module> semesterIterator(String semester) {
|
||||
return new Iterator<Modules.Module>() {
|
||||
private int index = -1;
|
||||
private int next = -1;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (index==next)
|
||||
predict();
|
||||
return next != -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Modules.Module next() {
|
||||
if (index == next)
|
||||
predict();
|
||||
if (next == -1)
|
||||
return null;
|
||||
index = next;
|
||||
return internalList.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("no changes allowed");
|
||||
}
|
||||
|
||||
private void predict() {
|
||||
int size = internalList.size();
|
||||
do {
|
||||
next++;
|
||||
} while (next < size && decline(internalList.get(next)));
|
||||
if (next == size)
|
||||
next = -1;
|
||||
}
|
||||
|
||||
private boolean decline(Modules.Module ob){
|
||||
return !ob.semester.equals(semester);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class Regex {
|
||||
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
|
||||
Matcher matcher = pattern.matcher(match);
|
||||
if (!matcher.find()) {
|
||||
throw new NoSuchFieldException();
|
||||
throw new NoSuchFieldException(String.format("Pattern: %s - String: %s", regex, match));
|
||||
}
|
||||
return matcher.group(group);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ import android.util.Log;
|
||||
public class Logger {
|
||||
private final String tag;
|
||||
|
||||
public static Logger n(Object object) {
|
||||
return new Logger(object);
|
||||
}
|
||||
|
||||
public Logger(Object object) {
|
||||
if (object instanceof String)
|
||||
this.tag = (String) object;
|
||||
|
||||
Reference in New Issue
Block a user