public MutableConditionals() { this(new Conditionals()); }
/** * Adds tags to the If-Match header. * * @param tag the tag to add, may be null. This means the same as adding {@link Tag#ALL} * @throws IllegalArgumentException if ALL is supplied more than once, or you add a null tag more than once. * @return a new Conditionals object with the If-Match tag added. */ public Conditionals addIfMatch(Tag tag) { Preconditions.checkArgument(!modifiedSince.isPresent(), String.format(ERROR_MESSAGE, HeaderConstants.IF_MATCH, HeaderConstants.IF_MODIFIED_SINCE)); Preconditions.checkArgument(noneMatch.isEmpty(), String.format(ERROR_MESSAGE, HeaderConstants.IF_MATCH, HeaderConstants.IF_NONE_MATCH)); List<Tag> match = new ArrayList<>(this.match); if (tag == null) { tag = Tag.ALL; } if (Tag.ALL.equals(tag)) { match.clear(); } if (!match.contains(Tag.ALL)) { if (!match.contains(tag)) { match.add(tag); } } else { throw new IllegalArgumentException("Tag ALL already in the list"); } return new Conditionals(Collections.unmodifiableList(match), empty(), Optional.empty(), unModifiedSince); }
/** * You should use the server's time here. Otherwise you might get unexpected results. * The typical use case is: * * * <pre> * HTTPResponse response = .... * HTTPRequest request = createRequest(); * request = request.conditionals(new Conditionals().ifUnModifiedSince(response.getLastModified()); * </pre> * * @param time the time to check. * @return the conditionals with the If-Unmodified-Since date set. */ public Conditionals ifUnModifiedSince(LocalDateTime time) { Preconditions.checkArgument(noneMatch.isEmpty(), String.format(ERROR_MESSAGE, HeaderConstants.IF_UNMODIFIED_SINCE, HeaderConstants.IF_NONE_MATCH)); Preconditions.checkArgument(!modifiedSince.isPresent(), String.format(ERROR_MESSAGE, HeaderConstants.IF_UNMODIFIED_SINCE, HeaderConstants.IF_MODIFIED_SINCE)); time = time.withNano(0); return new Conditionals(match, empty(), Optional.empty(), Optional.of(time)); }
/** * You should use the server's time here. Otherwise you might get unexpected results. * * The typical use case is: * * * <pre> * HTTPResponse response = .... * HTTPRequest request = createRequest(); * request = request.conditionals(new Conditionals().ifModifiedSince(response.getLastModified()); * </pre> * * @param time the time to check. * @return the conditionals with the If-Modified-Since date set. */ public Conditionals ifModifiedSince(LocalDateTime time) { Preconditions.checkArgument(match.isEmpty(), String.format(ERROR_MESSAGE, HeaderConstants.IF_MODIFIED_SINCE, HeaderConstants.IF_MATCH)); Preconditions.checkArgument(!unModifiedSince.isPresent(), String.format(ERROR_MESSAGE, HeaderConstants.IF_MODIFIED_SINCE, HeaderConstants.IF_UNMODIFIED_SINCE)); time = time.withNano(0); return new Conditionals(empty(), noneMatch, Optional.of(time), Optional.empty()); }
throw new IllegalArgumentException("Tag ALL already in the list"); return new Conditionals(empty(), Collections.unmodifiableList(noneMatch), modifiedSince, Optional.empty());
public static Conditionals valueOf(Headers headers) { List<Tag> ifMatch = makeTags(headers.getFirstHeaderValue(HeaderConstants.IF_MATCH).orElse(null)); List<Tag> ifNoneMatch = makeTags(headers.getFirstHeaderValue(HeaderConstants.IF_NONE_MATCH).orElse(null)); Optional<LocalDateTime> modifiedSince = headers.getFirstHeader(HeaderConstants.IF_MODIFIED_SINCE).flatMap(HeaderUtils::fromHttpDate); Optional<LocalDateTime> unModifiedSince = headers.getFirstHeader(HeaderConstants.IF_UNMODIFIED_SINCE).flatMap(HeaderUtils::fromHttpDate); return new Conditionals(ifMatch, ifNoneMatch, modifiedSince, unModifiedSince); }