com.spotify.apollo.ResponseImpl Maven / Gradle / Ivy
/*
* -\-\-
* Spotify Apollo API Interfaces
* --
* Copyright (C) 2013 - 2015 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.apollo;
import com.google.auto.value.AutoValue;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
/**
* An immutable response. Each invocation of a 'withXXX' method returns a new instance,
* making it safe to share instances between threads, at the cost of some efficiency.
*/
@AutoValue
abstract class ResponseImpl implements Response {
static final Response> OK = createInternal(Status.OK, Optional.empty());
// TODO Make it @Memoized once we upgrade to auto-value 1.4+
@Override
@Deprecated
public Map headers() {
return internalHeadersImpl().asMap();
}
@Override
public List> headerEntries() {
return internalHeadersImpl().entries();
}
@Override
public Optional header(String name) {
return internalHeadersImpl().get(name);
}
abstract Headers internalHeadersImpl();
@Override
public Response withHeader(String header, String value) {
// Allow overriding values
final Map newHeaders = new LinkedHashMap<>();
newHeaders.putAll(headers());
newHeaders.put(header, value);
return createInternal(status(), Headers.create(newHeaders), payload());
}
@Override
public Response withHeaders(Map headers) {
// Allow overriding values
final Map newHeaders = new LinkedHashMap<>();
newHeaders.putAll(headers());
newHeaders.putAll(headers);
return createInternal(status(), Headers.create(newHeaders), payload());
}
@Override
public Response
withPayload(@Nullable P newPayload) {
return createInternal(status(), internalHeadersImpl(), Optional.ofNullable(newPayload));
}
static Response create(StatusType statusCode) {
Objects.requireNonNull(statusCode);
//noinspection unchecked
return statusCode == Status.OK
? (Response) OK
: createInternal(statusCode, Optional.empty());
}
static Response create(StatusType statusCode, T payload) {
Objects.requireNonNull(statusCode);
return createInternal(statusCode, Optional.of(payload));
}
static Response create(T payload) {
return createInternal(Status.OK, Optional.of(payload));
}
private static Response createInternal(StatusType statusCode, Optional payload) {
return createInternal(
statusCode,
Headers.EMPTY,
payload);
}
private static Response createInternal(
StatusType statusCode,
Headers headers,
Optional payload) {
return new AutoValue_ResponseImpl<>(
statusCode,
payload,
headers);
}
}