Skip to content

Commit

Permalink
Support for Guava Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
arteam committed Aug 8, 2014
1 parent b4afcf1 commit 9af11fb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,14 @@ private Object[] convertToMethodParams(@NotNull ContainerNode<?> params,

@Nullable
private Object getDefaultValue(@NotNull Class<?> type) {
return Defaults.defaultValue(type);
if (type == Optional.class) {
// If it's Guava optional then handle it as an absent value
return Optional.absent();
} else if (type.isPrimitive()) {
// If parameter is a primitive set the appropriate default value
return Defaults.defaultValue(type);
}
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -79,21 +80,21 @@ public boolean apply(Player player) {
@JsonRpcMethod
public List<Player> find(@JsonRpcOptional @JsonRpcParam("position") @Nullable final Position position,
@JsonRpcOptional @JsonRpcParam("number") @Nullable final int number,
@JsonRpcOptional @JsonRpcParam("team") @Nullable final Team team,
@JsonRpcOptional @JsonRpcParam("team") @NotNull final Optional<Team> team,
@JsonRpcOptional @JsonRpcParam("firstName") @Nullable final String firstName,
@JsonRpcOptional @JsonRpcParam("lastName") @Nullable final String lastName,
@JsonRpcOptional @JsonRpcParam("birthDate") @Nullable final Date birthDate,
@JsonRpcOptional @JsonRpcParam("capHit") @Nullable final Double capHit) {
@JsonRpcOptional @JsonRpcParam("capHit") @NotNull final Optional<Double> capHit) {
return Lists.newArrayList(Iterables.filter(players, new Predicate<Player>() {
@Override
public boolean apply(Player player) {
if (position != null && !player.getPosition().equals(position)) return false;
if (number != 0 && player.getNumber() != number) return false;
if (team != null && !player.getTeam().equals(team)) return false;
if (team.isPresent() && !player.getTeam().equals(team.get())) return false;
if (firstName != null && !player.getFirstName().equals(firstName)) return false;
if (lastName != null && !player.getLastName().equals(lastName)) return false;
if (birthDate != null && !player.getBirthDate().equals(birthDate)) return false;
if (capHit!=null && player.getCapHit() != capHit) return false;
if (capHit.isPresent() && player.getCapHit() != capHit.get()) return false;
return true;
}
}));
Expand Down

0 comments on commit 9af11fb

Please sign in to comment.