Skip to content
63 changes: 63 additions & 0 deletions src/main/java/com/google/firebase/remoteconfig/AndCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

/*
* Copyright 2020 Google LLC
*
* 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.google.firebase.remoteconfig;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableList;
import com.google.firebase.internal.NonNull;
import com.google.firebase.remoteconfig.internal.ServerTemplateResponse.AndConditionResponse;
import com.google.firebase.remoteconfig.internal.ServerTemplateResponse.OneOfConditionResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

final class AndCondition {
private final ImmutableList<OneOfCondition> conditions;

AndCondition(@NonNull List<OneOfCondition> conditions) {
checkNotNull(conditions, "List of conditions for AND operation must not be null.");
checkArgument(!conditions.isEmpty(),
"List of conditions for AND operation must not be empty.");
this.conditions = ImmutableList.copyOf(conditions);
}

AndCondition(AndConditionResponse andConditionResponse) {
List<OneOfConditionResponse> conditionList = andConditionResponse.getConditions();
checkNotNull(conditionList, "List of conditions for AND operation must not be null.");
checkArgument(!conditionList.isEmpty(),
"List of conditions for AND operation must not be empty");
this.conditions = conditionList.stream()
.map(OneOfCondition::new)
.collect(ImmutableList.toImmutableList());
}

@NonNull
List<OneOfCondition> getConditions() {
return new ArrayList<>(conditions);
}

AndConditionResponse toAndConditionResponse() {
return new AndConditionResponse()
.setConditions(this.conditions.stream()
.map(OneOfCondition::toOneOfConditionResponse)
.collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,73 @@ protected Template execute() throws FirebaseRemoteConfigException {
};
}

/**
* Alternative to {@link #getServerTemplate} where developers can initialize with a pre-cached
* template or config.
*/
public ServerTemplateImpl.Builder serverTemplateBuilder() {
return new ServerTemplateImpl.Builder(this.remoteConfigClient);
}

/**
* Initializes a template instance and loads the latest template data.
*
* @param defaultConfig Default parameter values to use if a getter references a parameter not
* found in the template.
* @return A {@link Template} instance with the latest template data.
*/
public ServerTemplate getServerTemplate(KeysAndValues defaultConfig)
throws FirebaseRemoteConfigException {
return getServerTemplateOp(defaultConfig).call();
}

/**
* Initializes a template instance without any defaults and loads the latest template data.
*
* @return A {@link Template} instance with the latest template data.
*/
public ServerTemplate getServerTemplate() throws FirebaseRemoteConfigException {
return getServerTemplate(null);
}

/**
* Initializes a template instance and asynchronously loads the latest template data.
*
* @param defaultConfig Default parameter values to use if a getter references a parameter not
* found in the template.
* @return A {@link Template} instance with the latest template data.
*/
public ApiFuture<ServerTemplate> getServerTemplateAsync(KeysAndValues defaultConfig) {
return getServerTemplateOp(defaultConfig).callAsync(app);
}

/**
* Initializes a template instance without any defaults and asynchronously loads the latest
* template data.
*
* @return A {@link Template} instance with the latest template data.
*/
public ApiFuture<ServerTemplate> getServerTemplateAsync() {
return getServerTemplateAsync(null);
}

private CallableOperation<ServerTemplate, FirebaseRemoteConfigException> getServerTemplateOp(
KeysAndValues defaultConfig) {
return new CallableOperation<ServerTemplate, FirebaseRemoteConfigException>() {
@Override
protected ServerTemplate execute() throws FirebaseRemoteConfigException {
String serverTemplateData = remoteConfigClient.getServerTemplate();
ServerTemplate template =
serverTemplateBuilder()
.defaultConfig(defaultConfig)
.cachedTemplate(serverTemplateData)
.build();

return template;
}
};
}

/**
* Gets the requested version of the of the Remote Config template.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ Template publishTemplate(Template template, boolean validateOnly,

ListVersionsResponse listVersions(
ListVersionsOptions options) throws FirebaseRemoteConfigException;
}

String getServerTemplate() throws FirebaseRemoteConfigException;

}
Loading