Пример #1
0
  /**
   * Gets recommendations for the currently logged in user.
   *
   * @return An array containing the recommendations
   */
  public Medium[] getRecommendedMedia() {
    User u = Database.getInstance().getLoggedInUser();
    if (u == null) return null;
    LinkedList<Medium> recommendedMedia = new LinkedList<>();

    // Get favorite genre
    HashMap<Genre, Integer> mediumCounts = new HashMap<>();
    Integer count;
    int maxCount = -1;
    Genre favoriteGenre = null;

    for (Medium m : u.getFavoriteMediums()) {
      count = mediumCounts.get(m.getGenre());
      if (count == null) {
        count = 1;
      } else {
        count++;
      }
      mediumCounts.put(m.getGenre(), count);
      if (count > maxCount) {
        maxCount = count;
        favoriteGenre = m.getGenre();
      }
    }
    // Use topgenre if no favorite genre was found
    if (favoriteGenre == null) {
      favoriteGenre = Database.getInstance().getTopGenre();
    }

    // Get a song out of the favorite genre that is not rated yet
    for (Medium m : favoriteGenre.getMedia()) {
      if (m.getRatingList().get(u.hashCode()) == null) {
        recommendedMedia.add(m);
      }
    }

    // Return random top 10
    Collections.shuffle(recommendedMedia);
    recommendedMedia =
        new LinkedList(recommendedMedia.subList(0, Math.min(recommendedMedia.size(), 10)));
    Medium[] result = new Medium[recommendedMedia.size()];
    recommendedMedia.toArray(result);
    return result;
  }