xref: /haiku/src/apps/haikudepot/packagemodel/UserRatingSummary.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2013, Rene Gollent <rene@gollent.com>.
4  * Copyright 2016-2024, Andrew Lindesay <apl@lindesay.co.nz>.
5  * All rights reserved. Distributed under the terms of the MIT License.
6  */
7 
8 
9 #include "UserRatingSummary.h"
10 
11 #include "HaikuDepotConstants.h"
12 #include "Logger.h"
13 
14 
15 UserRatingSummary::UserRatingSummary()
16 	:
17 	fAverageRating(RATING_MISSING),
18 	fRatingCount(0),
19 	fRatingCountNoStar(0)
20 {
21 	for (int i = 0; i < 6; i++)
22 		fRatingCountByStar[i] = 0;
23 }
24 
25 
26 UserRatingSummary::UserRatingSummary(const UserRatingSummary& other)
27 	:
28 	fAverageRating(other.AverageRating()),
29 	fRatingCount(other.RatingCount()),
30 	fRatingCountNoStar(other.RatingCountByStar(RATING_MISSING_STAR))
31 {
32 	for (int i = 0; i < 6; i++)
33 		fRatingCountByStar[i] = other.RatingCountByStar(i);
34 }
35 
36 
37 void
38 UserRatingSummary::Clear()
39 {
40 	fAverageRating = RATING_MISSING;
41 	fRatingCount = 0;
42 	fRatingCountNoStar = 0;
43 
44 	for (int i = 0; i < 6; i++)
45 		fRatingCountByStar[i] = 0;
46 }
47 
48 
49 void
50 UserRatingSummary::SetAverageRating(float value)
51 {
52 	fAverageRating = value;
53 }
54 
55 
56 void
57 UserRatingSummary::SetRatingCount(int value)
58 {
59 	fRatingCount = value;
60 }
61 
62 
63 int
64 UserRatingSummary::RatingCountByStar(int star) const
65 {
66 	if (star == RATING_MISSING_STAR)
67 		return fRatingCountNoStar;
68 
69 	if (star >= 0 && star <= 5)
70 		return fRatingCountByStar[star];
71 
72 	return 0;
73 }
74 
75 
76 void
77 UserRatingSummary::SetRatingByStar(int star, int ratingCount)
78 {
79 	if (star == RATING_MISSING_STAR)
80 		fRatingCountNoStar = ratingCount;
81 
82 	if (ratingCount < 0) {
83 		HDERROR("bad rating count %" B_PRId32, ratingCount);
84 		return;
85 	}
86 
87 	if (star < RATING_MISSING_STAR || star > 5) {
88 		HDERROR("bad star %" B_PRId32, star);
89 		return;
90 	}
91 
92 	fRatingCountByStar[star] = ratingCount;
93 }
94 
95 
96 UserRatingSummary&
97 UserRatingSummary::operator=(const UserRatingSummary& other)
98 {
99 	fAverageRating = other.AverageRating();
100 	fRatingCount = other.RatingCount();
101 	fRatingCountNoStar = other.RatingCountByStar(-1);
102 
103 	for (int i = 0; i < 6; i++)
104 		fRatingCountByStar[i] = other.RatingCountByStar(i);
105 
106 	return *this;
107 }
108 
109 
110 bool
111 UserRatingSummary::operator==(const UserRatingSummary& other) const
112 {
113 	if (fAverageRating != other.AverageRating() || fRatingCount != other.RatingCount()
114 		|| fRatingCountNoStar != other.RatingCountByStar(-1)) {
115 		return false;
116 	}
117 
118 	for (int i = 0; i < 6; i++) {
119 		if (fRatingCountByStar[i] != other.RatingCountByStar(i))
120 			return false;
121 	}
122 
123 	return true;
124 }
125 
126 
127 bool
128 UserRatingSummary::operator!=(const UserRatingSummary& other) const
129 {
130 	return !(*this == other);
131 }
132