|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.gcloud.compute; |
| 18 | + |
| 19 | +import com.google.common.base.Function; |
| 20 | +import com.google.common.base.MoreObjects; |
| 21 | + |
| 22 | +import java.io.Serializable; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +/** |
| 26 | + * The deprecation status associated to a Google Compute Engine resource. |
| 27 | + */ |
| 28 | +public final class DeprecationStatus<T extends ResourceId> implements Serializable { |
| 29 | + |
| 30 | + private static final long serialVersionUID = -2695077634793679794L; |
| 31 | + |
| 32 | + private final String deleted; |
| 33 | + private final String deprecated; |
| 34 | + private final String obsolete; |
| 35 | + private final T replacement; |
| 36 | + private final Status status; |
| 37 | + |
| 38 | + /** |
| 39 | + * The deprecation status of a Google Compute Engine resource. |
| 40 | + */ |
| 41 | + public enum Status { |
| 42 | + /** |
| 43 | + * Operations that create Google Compute Engine entity using a deprecated resource will return |
| 44 | + * successfully but with a warning indicating the deprecation and suggesting a replacement. |
| 45 | + */ |
| 46 | + DEPRECATED, |
| 47 | + /** |
| 48 | + * Operations that create Google Compute Engine entity using an obsolete resource will be |
| 49 | + * rejected and result in an error. |
| 50 | + */ |
| 51 | + OBSOLETE, |
| 52 | + /** |
| 53 | + * Operations that create Google Compute Engine entity using a deleted resource will be |
| 54 | + * rejected and result in an error. |
| 55 | + */ |
| 56 | + DELETED |
| 57 | + } |
| 58 | + |
| 59 | + DeprecationStatus(String deleted, String deprecated, String obsolete, T replacement, |
| 60 | + Status status) { |
| 61 | + this.deleted = deleted; |
| 62 | + this.deprecated = deprecated; |
| 63 | + this.obsolete = obsolete; |
| 64 | + this.replacement = replacement; |
| 65 | + this.status = status; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource |
| 70 | + * will be changed to {@link Status#DELETED}. |
| 71 | + * |
| 72 | + * @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a> |
| 73 | + */ |
| 74 | + public String deleted() { |
| 75 | + return deleted; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource |
| 80 | + * will be changed to {@link Status#DEPRECATED}. |
| 81 | + * |
| 82 | + * @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a> |
| 83 | + */ |
| 84 | + public String deprecated() { |
| 85 | + return deprecated; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource |
| 90 | + * will be changed to {@link Status#OBSOLETE}. |
| 91 | + * |
| 92 | + * @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC3339</a> |
| 93 | + */ |
| 94 | + public String obsolete() { |
| 95 | + return obsolete; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns the identity of the suggested replacement for a deprecated resource. The suggested |
| 100 | + * replacement resource must be the same kind of resource as the deprecated resource. |
| 101 | + */ |
| 102 | + public T replacement() { |
| 103 | + return replacement; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the deprecation state of this resource. |
| 108 | + */ |
| 109 | + public Status status() { |
| 110 | + return status; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() { |
| 115 | + return MoreObjects.toStringHelper(this) |
| 116 | + .add("deleted", deleted) |
| 117 | + .add("deprecated", deprecated) |
| 118 | + .add("obsolete", obsolete) |
| 119 | + .add("replacement", replacement) |
| 120 | + .add("status", status) |
| 121 | + .toString(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public int hashCode() { |
| 126 | + return Objects.hash(deleted, deprecated, obsolete, replacement, status); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean equals(Object obj) { |
| 131 | + return obj instanceof DeprecationStatus |
| 132 | + && Objects.equals(toPb(), ((DeprecationStatus) obj).toPb()); |
| 133 | + } |
| 134 | + |
| 135 | + com.google.api.services.compute.model.DeprecationStatus toPb() { |
| 136 | + com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb = |
| 137 | + new com.google.api.services.compute.model.DeprecationStatus(); |
| 138 | + deprecationStatusPb.setDeleted(deleted); |
| 139 | + deprecationStatusPb.setDeprecated(deprecated); |
| 140 | + deprecationStatusPb.setObsolete(obsolete); |
| 141 | + if (replacement != null) { |
| 142 | + deprecationStatusPb.setReplacement(replacement.toUrl()); |
| 143 | + } |
| 144 | + if (status() != null) { |
| 145 | + deprecationStatusPb.setState(status.name()); |
| 146 | + } |
| 147 | + return deprecationStatusPb; |
| 148 | + } |
| 149 | + |
| 150 | + static <T extends ResourceId> DeprecationStatus<T> fromPb( |
| 151 | + com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb, |
| 152 | + Function<String, T> fromUrl) { |
| 153 | + return new DeprecationStatus<T>( |
| 154 | + deprecationStatusPb.getDeleted(), |
| 155 | + deprecationStatusPb.getDeprecated(), |
| 156 | + deprecationStatusPb.getObsolete(), |
| 157 | + deprecationStatusPb.getReplacement() != null |
| 158 | + ? fromUrl.apply(deprecationStatusPb.getReplacement()) : null, |
| 159 | + deprecationStatusPb.getState() != null |
| 160 | + ? Status.valueOf(deprecationStatusPb.getState()) : null); |
| 161 | + } |
| 162 | +} |
0 commit comments