Skip to content

Commit e51812a

Browse files
committed
use generics in SearchApi
1 parent 91807f2 commit e51812a

File tree

3 files changed

+148
-168
lines changed

3 files changed

+148
-168
lines changed

src/main/java/org/gitlab4j/api/Constants.java

+105-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
package org.gitlab4j.api;
22

3+
import java.util.Arrays;
34
import java.util.HashMap;
45
import java.util.Map;
5-
6+
import java.util.function.Function;
7+
import java.util.stream.Collectors;
8+
9+
import org.gitlab4j.api.models.Commit;
10+
import org.gitlab4j.api.models.Issue;
11+
import org.gitlab4j.api.models.MergeRequest;
12+
import org.gitlab4j.api.models.Milestone;
13+
import org.gitlab4j.api.models.Note;
14+
import org.gitlab4j.api.models.Project;
15+
import org.gitlab4j.api.models.SearchBlob;
16+
import org.gitlab4j.api.models.Snippet;
17+
import org.gitlab4j.api.models.User;
618
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
719

820
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -730,79 +742,147 @@ public String toString() {
730742
/**
731743
* Enum for the search scope when doing a globalSearch() with the SearchApi.
732744
*/
733-
public enum SearchScope {
745+
public static class SearchScope<T> {
746+
747+
private final String jsonName;
748+
private final Class<T> resultType;
749+
750+
private SearchScope(String jsonName, Class<T> resultType) {
751+
this.jsonName = jsonName;
752+
this.resultType = resultType;
753+
}
734754

735-
PROJECTS, ISSUES, MERGE_REQUESTS, MILESTONES, SNIPPET_TITLES, SNIPPET_BLOBS, USERS,
736-
BLOBS, COMMITS, WIKI_BLOBS;
755+
public Class<T> getResultType() {
756+
return resultType;
757+
}
758+
759+
public static final SearchScope<Project> PROJECTS = new SearchScope<>("projects", Project.class);
760+
public static final SearchScope<Issue> ISSUES = new SearchScope<>("issues", Issue.class);
761+
public static final SearchScope<MergeRequest> MERGE_REQUESTS = new SearchScope<>("merge_requests", MergeRequest.class);
762+
public static final SearchScope<Milestone> MILESTONES = new SearchScope<>("milestones", Milestone.class);
763+
public static final SearchScope<Snippet> SNIPPET_TITLES = new SearchScope<>("snippet_titles", Snippet.class);
764+
public static final SearchScope<Snippet> SNIPPET_BLOBS = new SearchScope<>("snippet_blobs", Snippet.class);
765+
public static final SearchScope<User> USERS = new SearchScope<>("users", User.class);
766+
public static final SearchScope<SearchBlob> BLOBS = new SearchScope<>("blobs", SearchBlob.class);
767+
public static final SearchScope<Commit> COMMITS = new SearchScope<>("commits", Commit.class);
768+
public static final SearchScope<SearchBlob> WIKI_BLOBS = new SearchScope<>("wiki_blobs", SearchBlob.class);
737769

738-
private static JacksonJsonEnumHelper<SearchScope> enumHelper = new JacksonJsonEnumHelper<>(SearchScope.class);
770+
private static final Map<String, SearchScope> jsonLookup = Arrays.stream(new SearchScope[]{PROJECTS, PROJECTS, ISSUES, MERGE_REQUESTS, MILESTONES, SNIPPET_TITLES, SNIPPET_BLOBS, USERS, BLOBS, COMMITS, WIKI_BLOBS})
771+
.collect(Collectors.toMap(searchScope -> searchScope.jsonName, Function.identity()));
739772

740773
@JsonCreator
741-
public static SearchScope forValue(String value) {
742-
return enumHelper.forValue(value);
774+
public static <T> SearchScope<T> forValue(String value) {
775+
return (SearchScope<T>) jsonLookup.get(value);
743776
}
744777

745778
@JsonValue
746779
public String toValue() {
747-
return (enumHelper.toString(this));
780+
return jsonName;
748781
}
749782

750783
@Override
751784
public String toString() {
752-
return (enumHelper.toString(this));
785+
return jsonName;
753786
}
754787
}
755788

756789
/**
757790
* Enum for the search scope when doing a groupSearch() with the SearchApi.
758791
*/
759-
public enum GroupSearchScope {
792+
public static class GroupSearchScope<T> {
793+
794+
private final String jsonName;
795+
private final Class<T> resultType;
760796

761-
PROJECTS, ISSUES, MERGE_REQUESTS, MILESTONES, WIKI_BLOBS, COMMITS, BLOBS, NOTES, USERS;
797+
public GroupSearchScope(String jsonName, Class<T> resultType) {
798+
this.jsonName = jsonName;
799+
this.resultType = resultType;
800+
}
762801

763-
private static JacksonJsonEnumHelper<GroupSearchScope> enumHelper = new JacksonJsonEnumHelper<>(GroupSearchScope.class);
802+
public Class<T> getResultType() {
803+
return resultType;
804+
}
805+
806+
public static final GroupSearchScope<Project> PROJECTS = new GroupSearchScope<>("projects", Project.class);
807+
public static final GroupSearchScope<Issue> ISSUES = new GroupSearchScope<>("issues", Issue.class);
808+
public static final GroupSearchScope<MergeRequest> MERGE_REQUESTS = new GroupSearchScope<>("merge_requests", MergeRequest.class);
809+
public static final GroupSearchScope<Milestone> MILESTONES = new GroupSearchScope<>("milestones", Milestone.class);
810+
public static final GroupSearchScope<SearchBlob> WIKI_BLOBS = new GroupSearchScope<>("wiki_blobs", SearchBlob.class);
811+
public static final GroupSearchScope<Commit> COMMITS = new GroupSearchScope<>("commits", Commit.class);
812+
public static final GroupSearchScope<SearchBlob> BLOBS = new GroupSearchScope<>("blobs", SearchBlob.class);
813+
public static final GroupSearchScope<Note> NOTES = new GroupSearchScope<>("notes", Note.class);
814+
public static final GroupSearchScope<User> USERS = new GroupSearchScope<>("users", User.class);
815+
816+
private static final Map<String, GroupSearchScope> jsonLookup = Arrays.stream(new GroupSearchScope[]{
817+
PROJECTS, ISSUES, MERGE_REQUESTS, MILESTONES, WIKI_BLOBS, COMMITS, BLOBS, NOTES, USERS,
818+
})
819+
.collect(Collectors.toMap(searchScope -> searchScope.jsonName, Function.identity()));
764820

765821
@JsonCreator
766-
public static GroupSearchScope forValue(String value) {
767-
return enumHelper.forValue(value);
822+
public static <T> GroupSearchScope<T> forValue(String value) {
823+
return (GroupSearchScope<T>) jsonLookup.get(value);
768824
}
769825

770826
@JsonValue
771827
public String toValue() {
772-
return (enumHelper.toString(this));
828+
return jsonName;
773829
}
774830

775831
@Override
776832
public String toString() {
777-
return (enumHelper.toString(this));
833+
return jsonName;
778834
}
779835
}
780836

781837
/**
782838
* Enum for the search scope when doing a projectSearch() with the SearchApi.
783839
*/
784-
public enum ProjectSearchScope {
840+
public static class ProjectSearchScope<T> {
841+
842+
private final String jsonName;
843+
private final Class<T> resultType;
844+
845+
public ProjectSearchScope(String jsonName, Class<T> resultType) {
846+
this.jsonName = jsonName;
847+
this.resultType = resultType;
848+
}
849+
850+
public Class<T> getResultType() {
851+
return resultType;
852+
}
785853

786-
BLOBS, COMMITS, ISSUES, MERGE_REQUESTS, MILESTONES, NOTES, WIKI_BLOBS, USERS;
854+
public static final ProjectSearchScope<SearchBlob> BLOBS = new ProjectSearchScope<>("blobs", SearchBlob.class);
855+
public static final ProjectSearchScope<Commit> COMMITS = new ProjectSearchScope<>("commits", Commit.class);
856+
public static final ProjectSearchScope<Issue> ISSUES = new ProjectSearchScope<>("issues", Issue.class);
857+
public static final ProjectSearchScope<MergeRequest> MERGE_REQUESTS = new ProjectSearchScope<>("merge_requests", MergeRequest.class);
858+
public static final ProjectSearchScope<Milestone> MILESTONES = new ProjectSearchScope<>("milestones", Milestone.class);
859+
public static final ProjectSearchScope<Note> NOTES = new ProjectSearchScope<>("notes", Note.class);
860+
public static final ProjectSearchScope<SearchBlob> WIKI_BLOBS = new ProjectSearchScope<>("wiki_blobs", SearchBlob.class);
861+
public static final ProjectSearchScope<User> USERS = new ProjectSearchScope<>("users", User.class);
787862

788-
private static JacksonJsonEnumHelper<ProjectSearchScope> enumHelper = new JacksonJsonEnumHelper<>(ProjectSearchScope.class);
863+
864+
private static final Map<String, ProjectSearchScope> jsonLookup = Arrays.stream(new ProjectSearchScope[]{
865+
BLOBS, COMMITS, ISSUES, MERGE_REQUESTS, MILESTONES, NOTES, WIKI_BLOBS, USERS,
866+
})
867+
.collect(Collectors.toMap(searchScope -> searchScope.jsonName, Function.identity()));
789868

790869
@JsonCreator
791-
public static ProjectSearchScope forValue(String value) {
792-
return enumHelper.forValue(value);
870+
public static <T> ProjectSearchScope<T> forValue(String value) {
871+
return (ProjectSearchScope<T>) jsonLookup.get(value);
793872
}
794873

795874
@JsonValue
796875
public String toValue() {
797-
return (enumHelper.toString(this));
876+
return jsonName;
798877
}
799878

800879
@Override
801880
public String toString() {
802-
return (enumHelper.toString(this));
881+
return jsonName;
803882
}
804883
}
805884

885+
806886
/** Enum to use for specifying the action when doing a getTodos() with the TodosApi. */
807887
public enum TodoAction {
808888

@@ -1084,10 +1164,10 @@ public enum DefaultBranchProtectionLevel {
10841164
FULLY_PROTECTED(2),
10851165
PROTECTED_AGAINST_PUSHES(3),
10861166
FULL_PROTECTION_AFTER_INITIAL_PUSH(4);
1087-
1167+
10881168
@JsonValue
10891169
private final int value;
1090-
1170+
10911171
private DefaultBranchProtectionLevel(int value) {
10921172
this.value = value;
10931173
}

0 commit comments

Comments
 (0)