forked from nickbutcher/plaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd993ef
commit 71971a5
Showing
11 changed files
with
293 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
app/src/main/java/io/plaidapp/data/PlaidItemComparator.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2015 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.plaidapp.data; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
/** | ||
* Classes related to sorting {@link PlaidItem}s. | ||
*/ | ||
public class PlaidItemSorting { | ||
|
||
/** | ||
* A comparator that compares {@link PlaidItem}s based on their {@code weight} attribute. | ||
*/ | ||
public static class PlaidItemComparator implements Comparator<PlaidItem> { | ||
|
||
@Override | ||
public int compare(PlaidItem lhs, PlaidItem rhs) { | ||
return Float.compare(lhs.weight, rhs.weight); | ||
} | ||
} | ||
|
||
/** | ||
* Interface for weighing a group of {@link PlaidItem}s | ||
*/ | ||
public interface PlaidItemGroupWeigher<T extends PlaidItem> { | ||
void weigh(List<T> items); | ||
} | ||
|
||
/** | ||
* Applies a weight to a group of {@link PlaidItem}s according to their natural order. | ||
*/ | ||
public static class NaturalOrderWeigher implements PlaidItemGroupWeigher<PlaidItem> { | ||
|
||
@Override | ||
public void weigh(List<PlaidItem> items) { | ||
final float step = 1f / (float) items.size(); | ||
for (int i = 0; i < items.size(); i++) { | ||
PlaidItem item = items.get(i); | ||
item.weight = item.page + ((float) i) * step; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/io/plaidapp/data/api/designernews/StoryWeigher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2015 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.plaidapp.data.api.designernews; | ||
|
||
import java.util.List; | ||
|
||
import io.plaidapp.data.PlaidItemSorting; | ||
import io.plaidapp.data.api.designernews.model.Story; | ||
|
||
/** | ||
* Utility class for applying weights to a group of {@link Story}s for sorting. Weighs stories | ||
* relative to the most upvoted & commented stories in the group. | ||
*/ | ||
public class StoryWeigher implements PlaidItemSorting.PlaidItemGroupWeigher<Story> { | ||
|
||
@Override | ||
public void weigh(List<Story> stories) { | ||
float maxVotes = 0f; | ||
float maxComments = 0f; | ||
for (Story story : stories) { | ||
maxVotes = Math.max(maxVotes, story.vote_count); | ||
maxComments = Math.max(maxComments, story.comment_count); | ||
} | ||
for (Story story : stories) { | ||
float weight = 1f - ((((float) story.comment_count) / maxComments) + | ||
((float) story.vote_count / maxVotes)) / 2f; | ||
story.weight = story.page + weight; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/io/plaidapp/data/api/dribbble/ShotWeigher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2015 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.plaidapp.data.api.dribbble; | ||
|
||
import java.util.List; | ||
|
||
import io.plaidapp.data.PlaidItemSorting; | ||
import io.plaidapp.data.api.dribbble.model.Shot; | ||
|
||
/** | ||
* Utility class for applying weights to a group of {@link Shot}s for sorting. Weighs shots relative | ||
* to the most liked shot in the group. | ||
*/ | ||
public class ShotWeigher implements PlaidItemSorting.PlaidItemGroupWeigher<Shot> { | ||
|
||
@Override | ||
public void weigh(List<Shot> shots) { | ||
float maxLikes = 0f; | ||
for (Shot shot : shots) { | ||
maxLikes = Math.max(maxLikes, shot.likes_count); | ||
} | ||
for (Shot shot : shots) { | ||
float weight = 1f - ((float) shot.likes_count / maxLikes); | ||
shot.weight = shot.page + weight; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/io/plaidapp/data/api/producthunt/PostWeigher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2015 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.plaidapp.data.api.producthunt; | ||
|
||
import java.util.List; | ||
|
||
import io.plaidapp.data.PlaidItemSorting; | ||
import io.plaidapp.data.api.producthunt.model.Post; | ||
|
||
/** | ||
* Utility class for applying weights to a group of {@link Post}s for sorting. Weighs posts relative | ||
* to the most upvoted & commented hunts in the group. | ||
*/ | ||
public class PostWeigher implements PlaidItemSorting.PlaidItemGroupWeigher<Post> { | ||
|
||
@Override | ||
public void weigh(List<Post> posts) { | ||
float maxVotes = 0f; | ||
float maxComments = 0f; | ||
for (Post post : posts) { | ||
maxVotes = Math.max(maxVotes, post.votes_count); | ||
maxComments = Math.max(maxComments, post.comments_count); | ||
} | ||
for (Post post : posts) { | ||
float weight = 1f - ((((float) post.comments_count) / maxComments) + | ||
((float) post.votes_count / maxVotes)) / 2f; | ||
post.weight = post.page + weight; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.