From 7fead8171d76496981eb61c21bc3b8ee62580c17 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sun, 1 Jan 2012 11:13:09 -0800 Subject: [PATCH] improving the usability by introducing a set with lookup-by-ID --- pom.xml | 23 ++++++++++++ .../java/org/kohsuke/github/GHPersonSet.java | 36 +++++++++++++++++++ .../java/org/kohsuke/github/GHRepository.java | 8 +++-- src/main/java/org/kohsuke/github/GHUser.java | 14 +++++--- .../java/org/kohsuke/github/JsonUsers.java | 4 +-- 5 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/kohsuke/github/GHPersonSet.java diff --git a/pom.xml b/pom.xml index 2198f4e65..73f3d90bd 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,23 @@ + + + + com.infradna.tool + bridge-method-injector + 1.2 + + + + process + + + + + + + org.jvnet.hudson @@ -54,6 +71,12 @@ commons-io 1.4 + + com.infradna.tool + bridge-method-annotation + 1.4 + true + diff --git a/src/main/java/org/kohsuke/github/GHPersonSet.java b/src/main/java/org/kohsuke/github/GHPersonSet.java new file mode 100644 index 000000000..5b98f036d --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHPersonSet.java @@ -0,0 +1,36 @@ +package org.kohsuke.github; + +import java.util.Collection; +import java.util.HashSet; + +/** + * Set of {@link GHPerson} with helper lookup methods. + * + * @author Kohsuke Kawaguchi + */ +public final class GHPersonSet extends HashSet { + public GHPersonSet() { + } + + public GHPersonSet(Collection c) { + super(c); + } + + public GHPersonSet(int initialCapacity, float loadFactor) { + super(initialCapacity, loadFactor); + } + + public GHPersonSet(int initialCapacity) { + super(initialCapacity); + } + + /** + * Finds the item by its login. + */ + public T byLogin(String login) { + for (T t : this) + if (t.getLogin().equals(login)) + return t; + return null; + } +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 44abaca1c..2091d1ed3 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -30,6 +30,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlInput; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.infradna.tool.bridge_method_injector.WithBridgeMethods; import java.io.IOException; import java.lang.annotation.RetentionPolicy; @@ -143,11 +144,12 @@ public class GHRepository { * Gets the collaborators on this repository. * This set always appear to include the owner. */ - public Set getCollaborators() throws IOException { - Set r = new HashSet(); + @WithBridgeMethods(Set.class) + public GHPersonSet getCollaborators() throws IOException { + GHPersonSet r = new GHPersonSet(); for (String u : root.retrieve("/repos/show/"+owner.login+"/"+name+"/collaborators",JsonCollaborators.class).collaborators) r.add(root.getUser(u)); - return Collections.unmodifiableSet(r); + return r; } /** diff --git a/src/main/java/org/kohsuke/github/GHUser.java b/src/main/java/org/kohsuke/github/GHUser.java index e8a682321..7e6689788 100644 --- a/src/main/java/org/kohsuke/github/GHUser.java +++ b/src/main/java/org/kohsuke/github/GHUser.java @@ -23,6 +23,9 @@ */ package org.kohsuke.github; +import com.infradna.tool.bridge_method_injector.BridgeMethodsAdded; +import com.infradna.tool.bridge_method_injector.WithBridgeMethods; + import java.io.IOException; import java.util.HashSet; import java.util.Map; @@ -123,22 +126,25 @@ public class GHUser extends GHPerson { /** * Lists the users that this user is following */ - public Set getFollows() throws IOException { + @WithBridgeMethods(Set.class) + public GHPersonSet getFollows() throws IOException { return root.retrieve("/user/show/"+login+"/following",JsonUsers.class).toSet(root); } /** * Lists the users who are following this user. */ - public Set getFollowers() throws IOException { + @WithBridgeMethods(Set.class) + public GHPersonSet getFollowers() throws IOException { return root.retrieve("/user/show/"+login+"/followers",JsonUsers.class).toSet(root); } /** * Gets the organization that this user belongs to publicly. */ - public Set getOrganizations() throws IOException { - Set orgs = new HashSet(); + @WithBridgeMethods(Set.class) + public GHPersonSet getOrganizations() throws IOException { + GHPersonSet orgs = new GHPersonSet(); Set names = new HashSet(); for (GHOrganization o : root.retrieve3("/users/"+login+"/orgs",GHOrganization[].class)) { if (names.add(o.getLogin())) // I've seen some duplicates in the data diff --git a/src/main/java/org/kohsuke/github/JsonUsers.java b/src/main/java/org/kohsuke/github/JsonUsers.java index 257f6001f..60c123e0e 100644 --- a/src/main/java/org/kohsuke/github/JsonUsers.java +++ b/src/main/java/org/kohsuke/github/JsonUsers.java @@ -34,8 +34,8 @@ import java.util.Set; class JsonUsers { public List users; - public Set toSet(GitHub root) throws IOException { - Set r = new HashSet(); + public GHPersonSet toSet(GitHub root) throws IOException { + GHPersonSet r = new GHPersonSet(); for (String u : users) r.add(root.getUser(u)); return r;