Skip to content

Commit

Permalink
#1567 lambdas and diamonds
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 26, 2022
1 parent e8742a7 commit 6c9dd40
Show file tree
Hide file tree
Showing 106 changed files with 462 additions and 930 deletions.
58 changes: 24 additions & 34 deletions src/main/java/com/jcabi/github/Bulk.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
*/
package com.jcabi.github;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Iterator;
import javax.json.JsonObject;
Expand Down Expand Up @@ -93,41 +91,33 @@ public Bulk(final Iterable<T> items) {
final RtPagination<T> page = RtPagination.class.cast(items);
final RtValuePagination.Mapping<T, JsonObject> mapping =
page.mapping();
this.origin = new RtPagination<T>(
this.origin = new RtPagination<>(
page.request(),
new RtValuePagination.Mapping<T, JsonObject>() {
@Override
public T map(final JsonObject object) {
final T item = mapping.map(object);
return (T) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
item.getClass().getInterfaces(),
new InvocationHandler() {
@Override
@SuppressWarnings("PMD.UseVarargs")
public Object invoke(final Object proxy,
final Method method, final Object[] args) {
final Object result;
if ("json".equals(method.getName())) {
result = object;
} else {
try {
result = method.invoke(item, args);
} catch (
final IllegalAccessException ex
) {
throw new IllegalStateException(ex);
} catch (
final InvocationTargetException ex
) {
throw new IllegalStateException(ex);
}
}
return result;
object -> {
final T item = mapping.map(object);
return (T) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
item.getClass().getInterfaces(),
(proxy, method, args) -> {
final Object result;
if ("json".equals(method.getName())) {
result = object;
} else {
try {
result = method.invoke(item, args);
} catch (
final IllegalAccessException ex
) {
throw new IllegalStateException(ex);
} catch (
final InvocationTargetException ex
) {
throw new IllegalStateException(ex);
}
}
);
}
return result;
}
);
}
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/github/CommitsComparison.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Iterable<RepoCommit> commits() throws IOException {
final JsonArray array = this.comparison.json()
.getJsonArray("commits");
final Collection<RepoCommit> commits =
new ArrayList<RepoCommit>(array.size());
new ArrayList<>(array.size());
final RepoCommits repo = this.comparison.repo().commits();
for (final JsonValue value : array) {
commits.add(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/github/Gist.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public String identifier() {
public Iterable<String> files() throws IOException {
final JsonObject array = this.gist.json().getJsonObject("files");
final Collection<String> files =
new ArrayList<String>(array.size());
new ArrayList<>(array.size());
for (final JsonValue value : array.values()) {
files.add(JsonObject.class.cast(value).getString("filename"));
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/jcabi/github/Issue.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public Pull pull() throws IOException {
* @throws IOException If there is any I/O problem
*/
public Event latestEvent(final String type) throws IOException {
final Iterable<Event.Smart> events = new Smarts<Event.Smart>(
final Iterable<Event.Smart> events = new Smarts<>(
this.issue.events()
);
Event found = null;
Expand Down Expand Up @@ -436,11 +436,11 @@ public IssueLabels roLabels() throws IOException {
final Collection<JsonObject> array =
this.jsn.value("labels", JsonArray.class)
.getValuesAs(JsonObject.class);
final Collection<Label> labels = new ArrayList<Label>(array.size());
final Collection<Label> labels = new ArrayList<>(array.size());
for (final JsonObject obj : array) {
labels.add(
new Label.Unmodified(
Issue.Smart.this.repo(),
this.repo(),
obj.toString()
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jcabi/github/IssueLabels.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public boolean addIfAbsent(
final String name, final String color
) throws IOException {
Label label = null;
for (final Label opt : new Bulk<Label>(this.labels.iterate())) {
for (final Label opt : new Bulk<>(this.labels.iterate())) {
if (opt.name().equals(name)) {
label = opt;
break;
Expand Down Expand Up @@ -213,7 +213,7 @@ public boolean addIfAbsent(
*/
public Collection<Label> findByColor(final String color)
throws IOException {
final Collection<Label> found = new LinkedList<Label>();
final Collection<Label> found = new LinkedList<>();
for (final Label label : this.labels.iterate()) {
if (new Label.Smart(label).color().equals(color)) {
found.add(label);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jcabi/github/Releases.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void remove(final int number) throws IOException {
*/
public boolean exists(final String tag) throws IOException {
boolean exists = false;
final Iterable<Release.Smart> rels = new Smarts<Release.Smart>(
final Iterable<Release.Smart> rels = new Smarts<>(
this.iterate()
);
for (final Release.Smart rel : rels) {
Expand All @@ -155,7 +155,7 @@ public boolean exists(final String tag) throws IOException {
*/
public Release find(final String tag) throws IOException {
Release found = null;
final Iterable<Release.Smart> rels = new Smarts<Release.Smart>(
final Iterable<Release.Smart> rels = new Smarts<>(
this.iterate()
);
for (final Release.Smart rel : rels) {
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/com/jcabi/github/RtAssignees.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.jcabi.http.response.RestResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.json.JsonObject;
import lombok.EqualsAndHashCode;
import org.hamcrest.Matchers;

Expand Down Expand Up @@ -86,18 +85,13 @@ final class RtAssignees implements Assignees {

@Override
public Iterable<User> iterate() {
return new RtPagination<User>(
return new RtPagination<>(
this.request,
new RtValuePagination.Mapping<User, JsonObject>() {
@Override
public User map(final JsonObject object) {
return new RtUser(
RtAssignees.this.owner.github(),
RtAssignees.this.entry,
object.getString("login")
);
}
}
object -> new RtUser(
this.owner.github(),
this.entry,
object.getString("login")
)
);
}

Expand Down
20 changes: 7 additions & 13 deletions src/main/java/com/jcabi/github/RtBranches.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.jcabi.aspects.Loggable;
import com.jcabi.http.Request;
import java.util.stream.StreamSupport;
import javax.json.JsonObject;
import lombok.EqualsAndHashCode;

/**
Expand Down Expand Up @@ -86,19 +85,14 @@ public Repo repo() {

@Override
public Iterable<Branch> iterate() {
return new RtPagination<Branch>(
return new RtPagination<>(
this.request,
new RtValuePagination.Mapping<Branch, JsonObject>() {
@Override
public Branch map(final JsonObject object) {
return new RtBranch(
RtBranches.this.entry,
RtBranches.this.owner,
object.getString("name"),
object.getJsonObject("commit").getString("sha")
);
}
}
object -> new RtBranch(
this.entry,
this.owner,
object.getString("name"),
object.getJsonObject("commit").getString("sha")
)
);
}

Expand Down
11 changes: 3 additions & 8 deletions src/main/java/com/jcabi/github/RtCollaborators.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,10 @@ public void remove(

@Override
public Iterable<User> iterate() {
return new RtPagination<User>(
return new RtPagination<>(
this.request,
new RtValuePagination.Mapping<User, JsonObject>() {
@Override
public User map(final JsonObject object) {
return RtCollaborators.this.owner.github().users()
.get(object.getString("login"));
}
}
object -> this.owner.github().users()
.get(object.getString("login"))
);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/github/RtComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void react(final Reaction reaction) throws IOException {
public Iterable<Reaction> reactions() {
return new RtPagination<>(
this.request.uri().path("/reactions").back(),
(object) -> new Reaction.Simple(object.getString(RtComment.CONTENT))
object -> new Reaction.Simple(object.getString(RtComment.CONTENT))
);
}
}
10 changes: 2 additions & 8 deletions src/main/java/com/jcabi/github/RtComments.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.net.HttpURLConnection;
import java.util.Date;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import lombok.EqualsAndHashCode;

Expand Down Expand Up @@ -122,16 +121,11 @@ public Comment post(final String text) throws IOException {

@Override
public Iterable<Comment> iterate(final Date since) {
return new RtPagination<Comment>(
return new RtPagination<>(
this.request.uri()
.queryParam("since", new Github.Time(since))
.back(),
new RtValuePagination.Mapping<Comment, JsonObject>() {
@Override
public Comment map(final JsonObject object) {
return RtComments.this.get(object.getInt("id"));
}
}
object -> this.get(object.getInt("id"))
);
}

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/com/jcabi/github/RtContents.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,13 @@ public Iterable<Content> iterate(
final String path,
final String ref
) {
return new RtPagination<Content>(
return new RtPagination<>(
this.request.method(Request.GET)
.uri().path(path).queryParam("ref", ref).back(),
new RtValuePagination.Mapping<Content, JsonObject>() {
@Override
public Content map(final JsonObject object) {
return new RtContent(
RtContents.this.entry, RtContents.this.owner,
object.getString("path")
);
};
}
object -> new RtContent(
this.entry, this.owner,
object.getString("path")
)
);
}

Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/jcabi/github/RtDeployKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.json.Json;
import javax.json.JsonObject;
import lombok.EqualsAndHashCode;

/**
Expand Down Expand Up @@ -89,14 +88,11 @@ public Repo repo() {

@Override
public Iterable<DeployKey> iterate() {
return new RtPagination<DeployKey>(
return new RtPagination<>(
this.request,
new RtValuePagination.Mapping<DeployKey, JsonObject>() {
@Override
public DeployKey map(final JsonObject object) {
//@checkstyle MultipleStringLiteralsCheck (1 line)
return RtDeployKeys.this.get(object.getInt("id"));
}
object -> {
//@checkstyle MultipleStringLiteralsCheck (1 line)
return this.get(object.getInt("id"));
}
);
}
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/jcabi/github/RtForks.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import lombok.EqualsAndHashCode;

Expand Down Expand Up @@ -91,14 +90,9 @@ public Repo repo() {
@Override
public Iterable<Fork> iterate(
final String sort) {
return new RtPagination<Fork>(
return new RtPagination<>(
this.request.uri().queryParam("sort", sort).back(),
new RtValuePagination.Mapping<Fork, JsonObject>() {
@Override
public Fork map(final JsonObject object) {
return RtForks.this.get(object.getInt(ID));
}
}
object -> this.get(object.getInt(ID))
);
}

Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/jcabi/github/RtGistComments.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import lombok.EqualsAndHashCode;

Expand Down Expand Up @@ -118,14 +117,9 @@ public GistComment post(

@Override
public Iterable<GistComment> iterate() {
return new RtPagination<GistComment>(
return new RtPagination<>(
this.request,
new RtValuePagination.Mapping<GistComment, JsonObject>() {
@Override
public GistComment map(final JsonObject object) {
return RtGistComments.this.get(object.getInt("id"));
}
}
object -> this.get(object.getInt("id"))
);
}
}
Loading

0 comments on commit 6c9dd40

Please sign in to comment.