Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#1663): Implement RepositoryStatistics.Smart #1666

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 118 additions & 11 deletions src/main/java/com/jcabi/github/RepositoryStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package com.jcabi.github;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -44,17 +45,6 @@
* @author Volodya Lombrozo (volodya.lombrozo@gmail.com)
* @version $Id $
* @since 1.8.0
* @todo #1660:90min Add RepositoryStatistics.Smart.
* Implement RepositoryStatistics.Smart and use it to retrieve repository
* statistics with the following methods:
* - RepositoryStatistics.Smart#language()
* - RepositoryStatistics.Smart#forksCount()
* - RepositoryStatistics.Smart#stargazers()
* - RepositoryStatistics.Smart#watchers()
* - RepositoryStatistics.Smart#size()
* - RepositoryStatistics.Smart#openIssues()
* In other words, it would be convenient to have particular methods with
* understandable names instead of using toMap() method.
*/
public final class RepositoryStatistics {

Expand Down Expand Up @@ -162,6 +152,14 @@ private enum KEY {
this.key = json;
}

/**
* Getter for the key.
* @return The key of the JSON object returned by the GitHub API.
*/
public String getKey() {
return this.key;
}

/**
* Extracts the JSON object returned by the GitHub to a map entry.
* @param object The JSON object returned by the GitHub API.
Expand All @@ -188,5 +186,114 @@ Object value(final JsonObject object) {
return result;
}
}

/**
* Smart RepositoryStatistics.
*
* @version $Id $
* @author Volodya Lombrozo (volodya.lombrozo@gmail.com)
* @since 1.8.0
*/
public static final class Smart {

/**
* Repository statistics.
*/
private final transient RepositoryStatistics stats;

/**
* Public ctor.
* @param repo Repository.
*/
public Smart(final Repo repo) {
this(new RepositoryStatistics(repo));
}

/**
* Public ctor.
* @param statistics Repository statistics.
*/
public Smart(final RepositoryStatistics statistics) {
this.stats = statistics;
}

/**
* Number of forks of this repository.
* @return Number of forks
* @throws IOException If there is any I/O problem
*/
public int forks() throws IOException {
return this.integer(KEY.FORKS_COUNT);
}

/**
* Number of users who have starred this repository.
* @return Number of stargazers
* @throws IOException If there is any I/O problem
*/
public int stargazers() throws IOException {
return this.integer(KEY.STARGAZERS_COUNT);
}

/**
* Number of users watching the repository.
* @return Number of watchers
* @throws IOException If there is any I/O problem
*/
public int watchers() throws IOException {
return this.integer(KEY.WATCHERS_COUNT);
}

/**
* The size of the repository.
* @return Size of the repository
* @throws IOException If there is any I/O problem
*/
public int size() throws IOException {
return this.integer(KEY.SIZE);
}

/**
* The number of open issues in this repository.
* @return Number of open issues
* @throws IOException If there is any I/O problem
*/
public int openIssues() throws IOException {
return this.integer(KEY.OPEN_ISSUES_COUNT);
}

/**
* The time the repository was created.
* @return Time the repository was created
* @throws IOException If there is any I/O problem
*/
public ZonedDateTime created() throws IOException {
return this.datetime(KEY.CREATED_AT);
}

/**
* Parses integer from JSON.
* @param key Json key.
* @return Integer value.
* @throws IOException If there is any I/O problem
*/
private int integer(final KEY key) throws IOException {
return Integer.parseInt(
String.valueOf(this.stats.toMap().get(key.getKey()))
);
}

/**
* Parses datetime from JSON.
* @param key Json key.
* @return Datetime value.
* @throws IOException If there is any I/O problem
*/
private ZonedDateTime datetime(final KEY key) throws IOException {
return ZonedDateTime.parse(
String.valueOf(this.stats.toMap().get(key.getKey()))
);
}
}
}

Loading