Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Code migrated to Java7, ready for pull-request
Browse files Browse the repository at this point in the history
  • Loading branch information
hudsonmendes committed Jan 7, 2017
1 parent 64228bf commit d5b2cfc
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 70 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ configure(allprojects - docProjects) {
group = 'org.springframework.social'

compileJava {
sourceCompatibility=1.8
targetCompatibility=1.8
sourceCompatibility=1.7
targetCompatibility=1.7
}
compileTestJava {
sourceCompatibility=1.8
targetCompatibility=1.8
sourceCompatibility=1.7
targetCompatibility=1.7
}

[compileJava, compileTestJava]*.options*.compilerArgs = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public Tweet(
*
* @deprecated Use other constructor with String ID instead.
*/
@Deprecated
public Tweet(
long id,
String idStr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public String getDescription() {
@SuppressWarnings("unchecked")
public <TEntry> List<TEntry> entries() {
List<TEntry> casted = new ArrayList<>();
this.entries.forEach(i -> casted.add((TEntry) i));
for (Object o : this.entries)
casted.add((TEntry) o);
return casted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public List<Place> deserialize(JsonParser jp, DeserializationContext ctxt) throw
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new TwitterModule());
jp.setCodec(mapper);
JsonNode treeNode = (JsonNode) jp.readValueAs(JsonNode.class).get("places");
JsonNode treeNode = jp.readValueAs(JsonNode.class).get("places");
return (List<Place>) mapper.reader(new TypeReference<List<Place>>() {}).readValue(treeNode);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ private MultiValueMap<String, String> makeCompatbileQueryParameters() {
MultiValueMap<String, String> output = new LinkedMultiValueMap<String, String>();
for (Iterator<String> i = this.parameters.keySet().iterator(); i.hasNext();) {
String key = i.next();
this.parameters.get(key).forEach(value -> {
output.add(key, value);
});
final List<String> values = this.parameters.get(key);
for (String value : values)
output.add(key, value);
}
return output;
}
Expand All @@ -138,7 +138,9 @@ private String replaceImplicitArguments(String path) {
}
}

toRemove.forEach(key -> this.parameters.remove(key));
for (String key : toRemove)
this.parameters.remove(key);

return finalPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.MultiValueMap;

Expand All @@ -38,7 +39,7 @@ protected static <TValue> void appendParameter(
appendParameter(params, name, value, false);
}

@SuppressWarnings({"rawtypes", "unchecked"})
@SuppressWarnings({"rawtypes"})
protected static <TValue> void appendParameter(
MultiValueMap<String, String> params,
String name,
Expand All @@ -58,12 +59,11 @@ protected static <TValue> void appendParameter(
return;
if (value instanceof ArrayList) {
final ArrayList valueAsList = (ArrayList) value;
final String[] valueArray = (String[]) valueAsList.stream()
.filter(i -> i != null)
.map(i -> i.toString())
.toArray(size -> new String[size]);

params.add(name, String.join(",", valueArray));
final List<String> valueAsStringList = new ArrayList<>();
for (Object o : valueAsList)
if (o != null)
valueAsStringList.add(o.toString());
params.add(name, String.join(",", valueAsStringList));
}
else
params.set(name, value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.social.twitter.api.advertising.Campaign;
Expand Down Expand Up @@ -99,9 +98,8 @@ public CampaignForm activeBetween(LocalDateTime startTime, LocalDateTime endTime
@Override
public CampaignForm thatCantBeServedDueTo(ReasonNotServable... reasons) {
if (reasons != null)
Arrays.stream(reasons).forEach(reason -> {
this.reasonsNotServable.add(reason);
});
for (ReasonNotServable reason : reasons)
this.reasonsNotServable.add(reason);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Spliterator;
import java.util.function.Consumer;

import org.springframework.social.twitter.api.advertising.StatisticsGranularity;
import org.springframework.social.twitter.api.advertising.StatisticsMetric;
Expand Down Expand Up @@ -126,32 +127,44 @@ else if (metricType == Object.class)
dumpEntriesAsHashes(iterator, entries);
}

private void dumpEntriesAsDecimals(Spliterator<JsonNode> iterator, List<Object> entries) {
iterator.forEachRemaining(i -> {
String entryValue = i.asText();
entries.add(BigDecimalMicroAmountDeserializer.parse(entryValue));
});
private void dumpEntriesAsDecimals(Spliterator<JsonNode> iterator, final List<Object> entries) {
iterator.forEachRemaining(new Consumer<JsonNode>() {
@Override
public void accept(JsonNode t) {
String entryValue = t.asText();
entries.add(BigDecimalMicroAmountDeserializer.parse(entryValue));
}
});
}

private void dumpEntriesAsNumbers(Spliterator<JsonNode> iterator, List<Object> entries) {
iterator.forEachRemaining(i -> {
String entryValue = i.asText();
if (!entryValue.contains(".")) {
entries.add(new Long(entryValue));
}
private void dumpEntriesAsNumbers(Spliterator<JsonNode> iterator, final List<Object> entries) {
iterator.forEachRemaining(new Consumer<JsonNode>() {
@Override
public void accept(JsonNode t) {
String entryValue = t.asText();
if (!entryValue.contains(".")) {
entries.add(new Long(entryValue));
}
else {
entries.add(new Float(entryValue));
}
});
}
});
}

private void dumpEntriesAsHashes(Spliterator<JsonNode> iterator, List<Object> entries) {
iterator.forEachRemaining(i -> {
i.fields().forEachRemaining(j -> {
String key = j.getKey();
String value = j.getValue().asText();
entries.add(new AbstractMap.SimpleEntry<String, String>(key, value));
});
private void dumpEntriesAsHashes(Spliterator<JsonNode> iterator, final List<Object> entries) {
iterator.forEachRemaining(new Consumer<JsonNode>() {
@Override
public void accept(JsonNode t) {
t.fields().forEachRemaining(new Consumer<Map.Entry<String, JsonNode>>() {
@Override
public void accept(Map.Entry<String, JsonNode> j) {
String key = j.getKey();
String value = j.getValue().asText();
entries.add(new AbstractMap.SimpleEntry<String, String>(key, value));
}
});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,11 @@ protected void assertTimelineTweets(List<Tweet> tweets, boolean isSearchResult)
final Entities entities = tweet2.getEntities();
assertEquals(0, entities.getHashTags().size());
}

protected List<Integer> intArrayToIntegerList(int[] values) {
List<Integer> list = new ArrayList<>();
for (int value : values)
list.add(value);
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import static org.junit.Assert.assertThat;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

/**
* @author Hudson Mendes
Expand Down Expand Up @@ -156,19 +159,13 @@ public void build_implicitArguments_array() {
}

private String versionFreePath(String path) {
String splitter = "/";
StringBuilder newPath = new StringBuilder();
Arrays.stream(path.split(splitter))
.filter(frag -> !frag.isEmpty())
.skip(1)
.forEach(frag -> {
if (newPath.length() != 0) {
newPath.append(splitter);
}

newPath.append(frag);
});

return newPath.toString();
final String splitter = "/";
final String[] frags = path.split(splitter);
final List<String> newPath = new ArrayList<>();
for (final String frag : frags)
if (frag != null && !StringUtils.isEmpty(frag.trim()))
newPath.add(frag);
newPath.remove(0);
return String.join(splitter, newPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.social.twitter.api.HashTagEntity;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.advertising.ApprovalStatus;
import org.springframework.social.twitter.api.advertising.PromotableUser;
Expand Down Expand Up @@ -111,14 +112,11 @@ private void assertTweetsContents(List<Tweet> tweets) {
Assert.assertEquals("ift.tt/1tLavU9", tweets.get(0).getEntities().getUrls().get(0).getDisplayUrl());
Assert.assertEquals(new Date("Thu Dec 04 17:56:40 GMT 2014").toInstant(), tweets.get(0).getCreatedAt().toInstant());
Assert.assertThat(
Arrays.asList(Arrays
.stream(tweets.get(0).getEntities().getUrls().get(0).getIndices())
.mapToObj(i -> Integer.valueOf(i))
.toArray(size -> new Integer[size])),
intArrayToIntegerList(tweets.get(0).getEntities().getUrls().get(0).getIndices()),
CoreMatchers.hasItems(Integer.valueOf(60), Integer.valueOf(82)));
}

@Test
@Test
public void createSponsoredTweet() {
final String mockedAccountId = "hkk5";
final String mockedTweetText = "Hey, here is a sponsored tweet that we are creating via #ads API.";
Expand All @@ -140,9 +138,11 @@ public void createSponsoredTweet() {

Assert.assertEquals(mockedUserId, tweet.getFromUserId());
Assert.assertEquals(mockedTweetText, tweet.getText());
Assert.assertThat(
Arrays.asList(tweet.getEntities().getHashTags().stream().map(i -> i.getText()).toArray(size -> new String[size])),
CoreMatchers.hasItems("ads"));

final List<String> hashtags = new ArrayList<>();
for (HashTagEntity hashtag : tweet.getEntities().getHashTags())
hashtags.add(hashtag.getText());
Assert.assertThat(hashtags, CoreMatchers.hasItems("ads"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
Expand Down Expand Up @@ -414,11 +413,10 @@ private void assertSnapshotContents(List<StatisticsSnapshot> snapshots) {
assertEquals(StatisticsGranularity.DAY, snapshot.getGranularity());
assertEquals("2015-05-01T07:00", snapshot.getStartTime().toString());
assertEquals("2015-05-04T07:00", snapshot.getEndTime().toString());

List<StatisticsMetric> metrics = Arrays.asList(Arrays
.stream(snapshot.getMetrics())
.map(i -> i.getName())
.toArray(size -> new StatisticsMetric[size]));

List<StatisticsMetric> metrics = new ArrayList<>();
for (StatisticsSnapshotMetric metric : snapshot.getMetrics())
metrics.add(metric.getName());

assertThat(metrics, hasItem(StatisticsMetric.billed_engagements));
assertThat(metrics, hasItem(StatisticsMetric.billed_follows));
Expand Down Expand Up @@ -457,6 +455,6 @@ private void assertSnapshotSingleContents(List<StatisticsSnapshot> snapshots) {
assertNotNull(snapshot.getMetric(StatisticsMetric.billed_follows));
assertThat(
snapshot.getMetric(StatisticsMetric.billed_follows).entries(),
hasItems(new Long[] {0L}));
hasItems(new Object[] {0L}));
}
}

0 comments on commit d5b2cfc

Please sign in to comment.