Search in sources :

Example 1 with NaturalOrderComparator

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;
            }
        }
    });
}
Also used : NaturalOrderComparator(com.paour.comparator.NaturalOrderComparator)

Example 2 with NaturalOrderComparator

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;
    }
}
Also used : Generex(com.mifmif.common.regex.Generex) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NaturalOrderComparator(com.paour.comparator.NaturalOrderComparator) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with NaturalOrderComparator

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();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NaturalOrderComparator(com.paour.comparator.NaturalOrderComparator)

Aggregations

NaturalOrderComparator (com.paour.comparator.NaturalOrderComparator)3 ArrayList (java.util.ArrayList)2 Generex (com.mifmif.common.regex.Generex)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nullable (org.jetbrains.annotations.Nullable)1 JSONArray (org.json.JSONArray)1