use of com.paour.comparator.NaturalOrderComparator in project substitution-schedule-parser by vertretungsplanme.
the class SubstitutionSchedule method addDay.
/**
* Add a day to this substitution schedule
*
* @param newDay the day to add
*/
public void addDay(SubstitutionScheduleDay newDay) {
if (lastChange == null && lastChangeString == null) {
// Read lastChange or lastChangeString from day
if (newDay.getLastChange() != null) {
lastChange = newDay.getLastChange();
} else if (newDay.getLastChangeString() != null) {
lastChangeString = newDay.getLastChangeString();
}
} else if (lastChange != null && newDay.getLastChange() != null && newDay.getLastChange().isAfter(lastChange)) {
// Update lastChange from day
lastChange = newDay.getLastChange();
}
addOrMergeDay(newDay);
Collections.sort(days, new Comparator<SubstitutionScheduleDay>() {
@Override
public int compare(SubstitutionScheduleDay a, SubstitutionScheduleDay b) {
if (a.getDate() != null && b.getDate() != null) {
return a.getDate().compareTo(b.getDate());
} else if (a.getDateString() != null && b.getDateString() != null) {
NaturalOrderComparator comp = new NaturalOrderComparator();
return comp.compare(a.getDateString(), b.getDateString());
} else if (a.getDateString() != null) {
return 1;
} else if (b.getDateString() != null) {
return -1;
} else {
return 0;
}
}
});
}
use of com.paour.comparator.NaturalOrderComparator in project substitution-schedule-parser by vertretungsplanme.
the class ParserUtils method getClassesFromJson.
@Nullable
static List<String> getClassesFromJson(JSONObject data) throws JSONException {
if (data.has("classes")) {
if (data.get("classes") instanceof JSONArray) {
JSONArray classesJson = data.getJSONArray("classes");
List<String> classes = new ArrayList<>();
for (int i = 0; i < classesJson.length(); i++) {
classes.add(classesJson.getString(i));
}
return classes;
} else if (data.get("classes") instanceof String) {
String regex = data.getString("classes");
Generex generex = new Generex(regex);
final List<String> classes = generex.getAllMatchedStrings();
Collections.sort(classes, new NaturalOrderComparator());
return classes;
} else {
return null;
}
} else {
return null;
}
}
use of com.paour.comparator.NaturalOrderComparator in project substitution-schedule-parser by vertretungsplanme.
the class SubstitutionTextUtils method joinClasses.
private static String joinClasses(Set<String> classes) {
List<String> list = new ArrayList<>(classes);
Collections.sort(list, new NaturalOrderComparator());
StringBuilder builder = new StringBuilder();
boolean first = true;
String beginning = null;
Pattern beginningRegex = Pattern.compile("^(.*\\d+)(\\w+)$");
for (String string : list) {
if (first) {
Matcher matcher = beginningRegex.matcher(string);
if (matcher.find()) {
beginning = matcher.group(1);
}
builder.append(string);
first = false;
} else {
Matcher matcher = beginningRegex.matcher(string);
if (matcher.find()) {
String newBeginning = matcher.group(1);
if (newBeginning.equals(beginning)) {
builder.append(matcher.group(2));
} else {
builder.append(", ");
builder.append(string);
beginning = newBeginning;
}
} else {
builder.append(", ");
builder.append(string);
beginning = null;
}
}
}
return builder.toString();
}
Aggregations