Example #1
1
  static void collectionTest() {
    // removeIf
    Collection<String> c = new HashSet<>();
    c.add("Content 1");
    c.add("Content 2");
    c.add("Content 3");
    c.add("Content 4");
    c.removeIf(s -> s.contains("2"));
    System.out.println("removeIf : " + c);

    /// 基本操作
    List<Integer> list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    list.removeIf(a -> a % 3 == 0);
    System.out.println("a % 3 == 0 " + list);

    // OR 操作
    Predicate<Integer> predicate2 = a -> a % 3 == 0;
    Predicate<Integer> predicate3 = a -> a % 5 == 0;
    list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    list.removeIf(predicate2.or(predicate3));
    System.out.println("a % 3 == 0 or a % 5 == 0 " + list);

    // AND 操作
    predicate2 = a -> a % 3 == 0;
    predicate3 = a -> a % 5 == 0;
    list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    list.removeIf(predicate2.and(predicate3));
    System.out.println("a % 3 == 0 and a % 5 == 0 " + list);

    List<String> stringList = Arrays.asList("a", "b");
    stringList.forEach(System.out::println);

    stringList = Arrays.asList("a", "b", "c");
    stringList.replaceAll(String::toUpperCase);
    System.out.println(stringList); // [A, B, C]

    stringList = Arrays.asList("a", "b", "c");
    stringList.sort(String::compareTo);

    Map<String, Integer> map = new HashMap<>();
    map.put("A", 10);
    map.put("B", 20);
    map.put("C", 30);
    map.forEach((k, v) -> System.out.println("Item : " + k + " Count : " + v));
    System.out.println(map.getOrDefault("D", 40)); // => 40
  }
  /**
   * Puts the given lesson plans into classrooms based on the distance the member fo staff will have
   * to travel between each lesson.
   *
   * @param lessonPlans The lessonPlans to be put into classrooms.
   * @param classrooms The classrooms to put lessons plans into.
   * @param distances The distances between the classrooms.
   * @return The list of lessonPlans after they have been put into classrooms.
   */
  private boolean putLessonPlansIntoClassrooms(
      List<LessonPlan> lessonPlans, List<Classroom> classrooms, List<Distance> distances) {

    // Create a week of periods for this subject
    List<List<LessonPlan>> week = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      week.add(new ArrayList<>());
    }
    lessonPlans
        .stream()
        .forEach(lessonPlan -> week.get(lessonPlan.period.day.id - 1).add(lessonPlan));

    List<Period> periods;
    // Iterate through each day
    for (int dayNo = 0; dayNo < week.size(); dayNo++) {
      // Get the periods of this day
      try {
        periods =
            daoManager
                .getPeriodDao()
                .getAllByDay(
                    daoManager
                        .getDayDao()
                        .getById(dayNo + 1)
                        .orElseThrow(
                            () -> new IllegalStateException("Failed to find periods in a day")));
        // The above exception should never be thrown
        periods.sort((o1, o2) -> o1.startTime.getHour() - o2.startTime.getHour());
      } catch (DataAccessException e) {
        DataExceptionHandler.handleJavaFx(e, "period", false);
        return false;
      } catch (DataConnectionException e) {
        DataExceptionHandler.handleJavaFx(e, null, true);
        return false;
      }

      // Get this days lessons and randomly allocate the first period
      List<LessonPlan> day = week.get(dayNo);
      List<LessonPlan> firstPeriod = new ArrayList<>();
      day.forEach(
          lessonPlan -> {
            if (lessonPlan.period.startTime.equals(LocalTime.of(9, 10))) {
              firstPeriod.add(lessonPlan);
            }
          });
      classrooms
          .stream()
          .forEach(
              classroom -> {
                for (LessonPlan lessonPlan : firstPeriod) {
                  if (lessonPlan.classroom.id == -1) {
                    lessonPlan.classroom = classroom;
                    break;
                  }
                }
              });
      // Store the first period in the timetabled list
      firstPeriod.forEach(
          lessonPlan -> {
            int index = lessonPlans.indexOf(lessonPlan);
            lessonPlans.set(index, lessonPlan);
          });

      List<LessonPlan> previousPeriod = new ArrayList<>(firstPeriod);
      // Iterate through the other periods
      for (int periodNo = 1; periodNo < 5; periodNo++) {
        List<LessonPlan> period = new ArrayList<>();
        for (LessonPlan lessonPlan : day) {
          if (lessonPlan.period.equals(periods.get(periodNo - 1))) {
            period.add(lessonPlan);
          }
        }

        // Timetable lessons where the staff member had a lesson previously
        for (LessonPlan lessonPlan : period) {
          Staff staff = lessonPlan.staff;
          Optional<LessonPlan> previousLesson =
              previousPeriod.stream().filter(lesson -> lesson.staff.equals(staff)).findFirst();

          if (previousLesson.isPresent()) {
            Optional<LessonPlan> planOptional =
                period
                    .stream()
                    .filter(plan -> plan.classroom.equals(previousLesson.get().classroom))
                    .findFirst();

            if (planOptional.isPresent()) {
              List<Distance> availableRooms =
                  distances
                      .stream()
                      .filter(
                          distance ->
                              distance.startRoom.equals(planOptional.get().classroom)
                                  || distance.endRoom.equals(planOptional.get().classroom))
                      .collect(Collectors.toList());
              availableRooms.removeIf(
                  distance -> {
                    boolean remove = false;
                    for (LessonPlan plan : period) {
                      if (plan.classroom.equals(distance.startRoom)
                          || plan.classroom.equals(distance.endRoom)) {
                        remove = true;
                      }
                    }

                    return remove;
                  });

              availableRooms.sort((o1, o2) -> o1.distance - o2.distance);
              Distance nextTrip = availableRooms.get(0);

              if (nextTrip.startRoom.equals(planOptional.get().classroom)) {
                lessonPlan.classroom = nextTrip.endRoom;
              } else if (nextTrip.endRoom.equals(planOptional.get().classroom)) {
                lessonPlan.classroom = nextTrip.startRoom;
              } else {
                assert false : "The new classroom should be one from the shortest distance!";
              }
            } else {
              lessonPlan.classroom = previousLesson.get().classroom;
            }
          }
        }

        // Timetable the remaining lessons randomly
        for (LessonPlan lessonPlan : period) {
          if (lessonPlan.classroom != null) {
            List<Classroom> availableRooms = new ArrayList<>();
            availableRooms.addAll(classrooms);

            availableRooms.removeIf(
                classroom -> {
                  boolean remove = false;
                  for (LessonPlan plan : period) {
                    if (classroom.equals(plan.classroom)) {
                      remove = true;
                    }
                  }
                  return remove;
                });
            lessonPlan.classroom = availableRooms.get(0);
          }
        }

        previousPeriod = period;
      }
    }
    return true;
  }