Skip to content

Commit

Permalink
Merge pull request #65 from AlexKrupa/alexkrupa/nullability-annotations
Browse files Browse the repository at this point in the history
Add JetBrains nullability annotations for public APIs
  • Loading branch information
burnoo authored Aug 6, 2018
2 parents 80d9e86 + dd5a886 commit 77bdc2c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions jspoon/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {

dependencies {
api "org.jsoup:jsoup:$versions.jsoup"
compileOnly "org.jetbrains:annotations:$versions.annotations"
testImplementation "junit:junit:$versions.junit"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.droidsonroids.jspoon;

import org.jetbrains.annotations.NotNull;
import org.jsoup.nodes.Element;

import pl.droidsonroids.jspoon.annotation.Selector;
Expand All @@ -19,5 +20,5 @@ public interface ElementConverter<T> {
* @param selector The annotation where this converter is registered
* @return the value to be assigned to the field
*/
T convert(Element node, Selector selector);
T convert(@NotNull Element node, @NotNull Selector selector);
}
28 changes: 13 additions & 15 deletions jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package pl.droidsonroids.jspoon;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
Expand All @@ -9,9 +14,6 @@
import java.util.LinkedHashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import pl.droidsonroids.jspoon.annotation.Selector;
import pl.droidsonroids.jspoon.exception.EmptySelectorException;

Expand Down Expand Up @@ -87,9 +89,8 @@ public T fromHtml(String htmlContent, T instance) {
* @param htmlContent String with HTML content
* @return Created object
*/
public T fromHtml(String htmlContent) {
Element pageRoot = Jsoup.parse(htmlContent);
return loadFromNode(pageRoot, null);
public T fromHtml(@NotNull String htmlContent) {
return fromHtml(htmlContent, null);
}

/**
Expand All @@ -101,7 +102,7 @@ public T fromHtml(String htmlContent) {
* @return Created object of type {@code T}
* @throws IOException If I/O error occurs while reading the {@code InputStream}
*/
public T fromInputStream(InputStream inputStream) throws IOException {
public T fromInputStream(@NotNull InputStream inputStream) throws IOException {
return fromInputStream(inputStream, null);
}

Expand All @@ -115,7 +116,7 @@ public T fromInputStream(InputStream inputStream) throws IOException {
* @return Created object of type {@code T}
* @throws IOException If I/O error occurs while reading the {@code InputStream}
*/
public T fromInputStream(InputStream inputStream, URL baseUrl) throws IOException {
public T fromInputStream(@NotNull InputStream inputStream, @Nullable URL baseUrl) throws IOException {
return fromInputStream(inputStream, null, baseUrl);
}

Expand All @@ -131,11 +132,11 @@ public T fromInputStream(InputStream inputStream, URL baseUrl) throws IOExceptio
* @return Created object of type {@code T}
* @throws IOException If I/O error occurs while reading the {@code InputStream}
*/
public T fromInputStream(InputStream inputStream, Charset charset, URL baseUrl, T instance) throws IOException {
public T fromInputStream(@NotNull InputStream inputStream, @Nullable Charset charset, @Nullable URL baseUrl, @Nullable T instance) throws IOException {
String urlToUse = baseUrl != null ? baseUrl.toString() : null;
String charsetToUse = charset != null ? charset.name() : null;
Element root = Jsoup.parse(inputStream, charsetToUse, urlToUse);
return loadFromNode(root, null);
return loadFromNode(root, instance);
}

/**
Expand All @@ -149,11 +150,8 @@ public T fromInputStream(InputStream inputStream, Charset charset, URL baseUrl,
* @return Created object of type {@code T}
* @throws IOException If I/O error occurs while reading the {@code InputStream}
*/
public T fromInputStream(InputStream inputStream, Charset charset, URL baseUrl) throws IOException {
String urlToUse = baseUrl != null ? baseUrl.toString() : null;
String charsetToUse = charset != null ? charset.name() : null;
Element root = Jsoup.parse(inputStream, charsetToUse, urlToUse);
return loadFromNode(root, null);
public T fromInputStream(@NotNull InputStream inputStream, @Nullable Charset charset, @Nullable URL baseUrl) throws IOException {
return fromInputStream(inputStream, charset, baseUrl, null);
}

private void addCachedHtmlField(FieldType field, Selector selector) {
Expand Down
6 changes: 4 additions & 2 deletions jspoon/src/main/java/pl/droidsonroids/jspoon/Jspoon.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pl.droidsonroids.jspoon;

import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -15,7 +17,7 @@ public class Jspoon {
*
* @return a new Jspoon instance
*/
public static Jspoon create() {
public static @NotNull Jspoon create() {
return new Jspoon();
}

Expand All @@ -32,7 +34,7 @@ private Jspoon() {
*/

@SuppressWarnings("unchecked")
public <T> HtmlAdapter<T> adapter(Class<T> clazz) {
public @NotNull <T> HtmlAdapter<T> adapter(Class<T> clazz) {
if (!adapterCache.containsKey(clazz)) {
adapterCache.put(clazz, new HtmlAdapter<>(this, clazz));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package pl.droidsonroids.jspoon;

import static org.junit.Assert.assertEquals;

import org.jetbrains.annotations.NotNull;
import org.jsoup.nodes.Element;
import org.junit.Before;
import org.junit.Test;

import pl.droidsonroids.jspoon.annotation.Selector;

import static org.junit.Assert.assertEquals;

public class ConverterTest {

private Jspoon jspoon;
Expand Down Expand Up @@ -41,7 +42,7 @@ private static class Weather {
private static class DayOfWeekConverter implements ElementConverter<DayOfWeek> {

@Override
public DayOfWeek convert(Element node, Selector selector) {
public DayOfWeek convert(@NotNull Element node, @NotNull Selector selector) {
String text = node.text();
text = text.substring("Today is ".length(), text.indexOf("."));
for (DayOfWeek dayOfWeek : DayOfWeek.values())
Expand All @@ -54,7 +55,7 @@ public DayOfWeek convert(Element node, Selector selector) {
private static class WeatherConverter implements ElementConverter<Weather> {

@Override
public Weather convert(Element node, Selector selector) {
public Weather convert(@NotNull Element node, @NotNull Selector selector) {
String text = node.text();
int offset = text.indexOf("Weather");
offset += "Weather is ".length();
Expand Down
1 change: 1 addition & 0 deletions versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ext {
minSdkVersion: 23,
sdkVersion : 27,
androidPlugin: '3.1.3',
annotations : '16.0.2',
buildTools : '27.0.3',
constraint : '1.1.2',
jsoup : '1.11.3',
Expand Down

0 comments on commit 77bdc2c

Please sign in to comment.