Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
67e52ce
Base implementation
Dec 17, 2019
84fcfea
Update with unit tests
Dec 20, 2019
145f7a0
lint
Dec 20, 2019
ac1fd6e
correct copyright date
Jan 7, 2020
2c8724b
lint
Jan 7, 2020
c90dbb0
Revert removal of helper functions
Jan 8, 2020
b0a617f
use auto-value
Jan 22, 2020
9dbfc2d
Merge branch 'master' into conditional-policy
Jan 22, 2020
54ad076
reformat Binding.java and Condition.java
Jan 22, 2020
ff49620
remove unnecessary dep
Jan 22, 2020
298b2be
code format
Jan 22, 2020
aaebba4
add dep on com.google.code.findbugs in google-cloud-core
Jan 22, 2020
a5f63ea
address comments
Feb 6, 2020
4873b73
Merge branch 'master' into conditional-policy
frankyn Feb 11, 2020
c853a84
Clean up
Feb 11, 2020
c64e55a
Merge branch 'conditional-policy' of github.com:frankyn/java-core int…
Feb 11, 2020
d2fab21
respond to comments
Feb 19, 2020
86cd863
Merge branch 'master' into conditional-policy
Feb 19, 2020
085959d
address comments
Feb 20, 2020
174e8c4
format
Feb 20, 2020
14e1aac
address feedback
Feb 21, 2020
fdb040a
remove unnecessary null check
Feb 21, 2020
42199d1
lint
Feb 21, 2020
bbb708a
address feedback
Feb 21, 2020
7f0e33e
remove ImmutableList from Binding AutoValue surface
Feb 21, 2020
108faec
address feedback
Feb 25, 2020
79126b5
split up unit test
Feb 25, 2020
505f9bc
use guava beta annotation
Feb 25, 2020
a89ef0c
surface ImmutableList<> for Binding class.
Feb 25, 2020
f0b5085
use BetaApi from api.core
Feb 25, 2020
9f5e600
return as expected
Feb 26, 2020
8580f5a
partial addressing of feedback
Feb 26, 2020
9fe4358
address feedback pt2
Feb 26, 2020
8f48a15
address remaining feedback
Feb 26, 2020
615ba06
Merge branch 'master' into conditional-policy
Feb 26, 2020
2b56641
address one last feedback
Feb 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Base implementation
  • Loading branch information
Frank Natividad
Frank Natividad committed Dec 17, 2019
commit 67e52cef4c9901d9059b1f9bec6769ca4fa49f11
154 changes: 154 additions & 0 deletions google-cloud-core/src/main/java/com/google/cloud/Binding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2019 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.cloud;

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

import com.google.api.core.InternalApi;
import com.google.common.base.MoreObjects;
import java.util.*;

public class Binding {
private Role role;
private Set<Identity> identities;
private Condition condition;

public static class Builder {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be using auto-value for these classes and builders if they are meant to be value classes?

It might be tricky if we're trying to enforce that the list of members is immutable on the actual value, but it's probably worthwhile to have these classes fully immutable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call I didn't think about using auto-value and I think you're right in that it can be helpful to be fully immutable reducing the number of builders.

Please correct me but I don't think I should update the Policy class but only Binding and Condition to use Auto-Value.

private Role role;
private Set<Identity> identities;
private Condition condition;

@InternalApi("This class should only be extended within google-cloud-java")
protected Builder() {}

@InternalApi("This class should only be extended within google-cloud-java")
protected Builder(Binding binding) {
setRole(binding.role);
setIdentities(binding.identities);
setCondition(binding.condition);
}

public final Binding.Builder setRole(Role role) {
this.role = role;
return this;
}

public final Binding.Builder setIdentities(Set<Identity> identities) {
this.identities = identities;
return this;
}

public final Binding.Builder setCondition(Condition condition) {
this.condition = condition;
return this;
}

/**
* Adds one or more identities to the binding
*
* @throws NullPointerException if any of the identities is null.
*/
public final Builder addIdentity(Identity first, Identity... others) {
String nullIdentityMessage = "Null identities are not permitted.";
checkNotNull(first, nullIdentityMessage);
checkNotNull(others, nullIdentityMessage);
for (Identity identity : others) {
checkNotNull(identity, nullIdentityMessage);
}
Set<Identity> toAdd = new LinkedHashSet<>();
toAdd.add(first);
toAdd.addAll(Arrays.asList(others));
if (identities == null) {
identities = new HashSet<>();
}
identities.addAll(toAdd);
return this;
}

/**
* Removes one or more identities from an existing binding. Does nothing if the binding
* associated with the provided role doesn't exist.
*/
public final Builder removeIdentity(Identity first, Identity... others) {
if (identities != null) {
identities.remove(first);
identities.removeAll(Arrays.asList(others));
}
return this;
}

/** Creates a {@code Policy} object. */
public final Binding build() {
return new Binding(this);
}
}

private Binding(Binding.Builder builder) {
this.role = builder.role;
this.identities = builder.identities;
this.condition = builder.condition;
}

public Binding.Builder toBuilder() {
return new Binding.Builder(this);
}

public Role getRole() {
return this.role;
}

public Set<Identity> getIdentities() {
return this.identities;
}

public Condition getCondition() {
return this.condition;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("role", role)
.add("identities", identities)
.add("condition", condition)
.toString();
}

@Override
public int hashCode() {
return Objects.hash(getClass(), role, identities, condition);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Binding)) {
return false;
}
Binding other = (Binding) obj;
return Objects.equals(role, other.getRole())
&& Objects.equals(identities, other.getIdentities())
&& Objects.equals(condition, other.getCondition());
}

/** Returns a builder for {@code Policy} objects. */
public static Binding.Builder newBuilder() {
return new Binding.Builder();
}
}
118 changes: 118 additions & 0 deletions google-cloud-core/src/main/java/com/google/cloud/Condition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2019 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.cloud;

import com.google.api.core.InternalApi;
import com.google.common.base.MoreObjects;
import java.util.Objects;

public class Condition {
private String title;
private String description;
private String expression;

public static class Builder {
private String title;
private String description;
private String expression;

@InternalApi("This class should only be extended within google-cloud-java")
protected Builder() {}

@InternalApi("This class should only be extended within google-cloud-java")
protected Builder(Condition condition) {
this.title = condition.title;
this.description = condition.description;
this.expression = condition.expression;
}

public final Condition.Builder setTitle(String title) {
this.title = title;
return this;
}

public final Condition.Builder setDescription(String description) {
this.description = description;
return this;
}

public final Condition.Builder setExpression(String expression) {
this.expression = expression;
return this;
}

/** Creates a {@code Condition} object. */
public final Condition build() {
return new Condition(this);
}
}

private Condition(Condition.Builder builder) {
this.title = builder.title;
this.description = builder.description;
this.expression = builder.expression;
}

public Condition.Builder toBuilder() {
return new Condition.Builder(this);
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}

public String getExpression() {
return expression;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("title", title)
.add("description", description)
.add("expression", expression)
.toString();
}

@Override
public int hashCode() {
return Objects.hash(getClass(), title, description, expression);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Condition)) {
return false;
}
Condition other = (Condition) obj;
return Objects.equals(title, other.getTitle())
&& Objects.equals(description, other.getDescription())
&& Objects.equals(expression, other.getExpression());
}

/** Returns a builder for {@code Policy} objects. */
public static Condition.Builder newBuilder() {
return new Condition.Builder();
}
}
Loading