001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.datamining;
018
019import java.io.Serializable;
020import java.util.HashSet;
021import java.util.Set;
022
023import org.conqat.lib.commons.collections.CollectionUtils;
024import org.conqat.lib.commons.collections.ListMap;
025import org.conqat.lib.commons.collections.UnmodifiableSet;
026
027/**
028 * Binary rating database. A rating is either 'like' or 'unrated'.
029 */
030public class RecommenderRatingDatabase<T> implements Serializable {
031
032        /** Serial ID */
033        private static final long serialVersionUID = 1L;
034
035        /** The data */
036        private final ListMap<IRecommenderUser, T> data = new ListMap<IRecommenderUser, T>();
037
038        /** Returns all users contained in this database. */
039        public UnmodifiableSet<IRecommenderUser> getUsers() {
040                return CollectionUtils.asUnmodifiable(data.getKeys());
041        }
042
043        /** Adds the given users to this rating database. */
044        public void add(IRecommenderUser user, Set<T> likedItems) {
045                data.addAll(user, likedItems);
046        }
047
048        /** Removes the user completely */
049        public void remove(IRecommenderUser user) {
050                data.removeCollection(user);
051        }
052
053        /**
054         * Returns the items liked by the given user. If no entry for this user is found
055         * an IllegalArgumentException is thrown.
056         */
057        public Set<T> getLikedItems(IRecommenderUser user) {
058                if (!data.containsCollection(user)) {
059                        throw new IllegalArgumentException("No such user: " + user);
060                }
061                return new HashSet<T>(data.getCollection(user));
062        }
063
064        /**
065         * Constructs a new {@link RecommenderRatingDatabase} from the given shopping
066         * baskets
067         */
068        public static <T> RecommenderRatingDatabase<T> fromShoppingBaskets(Set<Set<T>> shoppingBaskets) {
069                RecommenderRatingDatabase<T> ratingDatabase = new RecommenderRatingDatabase<T>();
070                for (Set<T> basket : shoppingBaskets) {
071                        ratingDatabase.add(new ShoppingBasketUser<T>(basket), basket);
072                }
073                return ratingDatabase;
074        }
075
076        /** {@inheritDoc} */
077        @Override
078        public String toString() {
079                return data.toString();
080        }
081
082}