Skip to content

Commit 6db00d3

Browse files
Creating an expandable enum type, converting StorageClass
1 parent 455f739 commit 6db00d3

3 files changed

Lines changed: 188 additions & 7 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2017 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+
package com.google.cloud;
17+
18+
import com.google.api.core.ApiFunction;
19+
import com.google.api.core.InternalApi;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
/**
24+
* This represents a concept having a known set of acceptable String values, which can expand later
25+
* due to new API features.
26+
*/
27+
@InternalApi
28+
public class StringEnumType<EnumT> {
29+
30+
private final String enumName;
31+
private final ApiFunction<String, EnumT> constructor;
32+
private Map<String, EnumT> knownValues = new HashMap<>();
33+
34+
public StringEnumType(String enumName, ApiFunction<String, EnumT> constructor) {
35+
this.enumName = enumName;
36+
this.constructor = constructor;
37+
}
38+
39+
/**
40+
* Create a new constant and register it in the known values.
41+
*/
42+
public EnumT createAndRegister(String constant) {
43+
EnumT instance = constructor.apply(constant);
44+
knownValues.put(constant, instance);
45+
return instance;
46+
}
47+
48+
/**
49+
* Get the enum object for the given String constant, and throw an exception if the constant is
50+
* not recognized.
51+
*/
52+
public EnumT valueOfStrict(String constant) {
53+
EnumT value = knownValues.get(constant);
54+
if (value != null) {
55+
return value;
56+
} else {
57+
throw new IllegalArgumentException(
58+
"Constant \"" + constant + "\" not found for enum \"" + enumName + "\"");
59+
}
60+
}
61+
62+
/**
63+
* Get the enum object for the given String constant, and allow unrecognized values.
64+
*/
65+
public EnumT valueOf(String constant) {
66+
EnumT value = knownValues.get(constant);
67+
if (value != null) {
68+
return value;
69+
} else {
70+
return constructor.apply(constant);
71+
}
72+
}
73+
74+
/**
75+
* Return the known values of this enum type.
76+
*/
77+
public EnumT[] values() {
78+
return (EnumT[]) knownValues.values().toArray();
79+
}
80+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2017 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+
package com.google.cloud;
17+
18+
import com.google.api.core.InternalApi;
19+
import com.google.common.base.Preconditions;
20+
21+
/**
22+
* This represents a specific instance of a concept having a known set of acceptable String values,
23+
* which can expand later due to new API features. Standard Java enums can't be used in such
24+
* a context.
25+
*/
26+
public abstract class StringEnumValue {
27+
private String constant;
28+
29+
@InternalApi
30+
protected StringEnumValue(String constant) {
31+
this.constant = Preconditions.checkNotNull(constant);
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return constant;
37+
}
38+
39+
@Override
40+
public boolean equals(Object that) {
41+
if (that == null) {
42+
return false;
43+
}
44+
if (this == that) {
45+
return true;
46+
}
47+
if (!(that instanceof StringEnumValue)) {
48+
return false;
49+
}
50+
StringEnumValue thatEnumValue = (StringEnumValue) that;
51+
return this.constant.equals(thatEnumValue.constant);
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return constant.hashCode();
57+
}
58+
}

google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClass.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,82 @@
1515
*/
1616
package com.google.cloud.storage;
1717

18+
import com.google.api.core.ApiFunction;
19+
import com.google.cloud.StringEnumType;
20+
import com.google.cloud.StringEnumValue;
21+
1822
/**
1923
* Enums for the storage classes.
2024
* See https://cloud.google.com/storage/docs/storage-classes for details.
2125
*/
22-
public enum StorageClass {
26+
public class StorageClass extends StringEnumValue {
27+
28+
private StorageClass(String constant) {
29+
super(constant);
30+
}
31+
32+
private static ApiFunction<String, StorageClass> CONSTRUCTOR =
33+
new ApiFunction<String, StorageClass>() {
34+
@Override
35+
public StorageClass apply(String constant) {
36+
return new StorageClass(constant);
37+
}
38+
};
39+
40+
private static StringEnumType<StorageClass> type = new StringEnumType(
41+
StorageClass.class.getName(),
42+
CONSTRUCTOR);
2343

2444
/**
2545
* Regional storage class.
2646
*/
27-
REGIONAL,
47+
public static StorageClass REGIONAL = type.createAndRegister("REGIONAL");
2848

2949
/**
3050
* Multi-regional storage class.
3151
*/
32-
MULTI_REGIONAL,
52+
public static StorageClass MULTI_REGIONAL = type.createAndRegister("MULTI_REGIONAL");
3353

3454
/**
3555
* Nearline storage class.
3656
*/
37-
NEARLINE,
57+
public static StorageClass NEARLINE = type.createAndRegister("NEARLINE");
3858

3959
/**
4060
* Coldline storage class.
4161
*/
42-
COLDLINE,
62+
public static StorageClass COLDLINE = type.createAndRegister("COLDLINE");
4363

4464
/**
4565
* Standard storage class.
4666
*/
47-
STANDARD,
67+
public static StorageClass STANDARD = type.createAndRegister("STANDARD");
4868

4969
/**
5070
* Durable Reduced Availability (deprecated)
5171
*/
52-
DURABLE_REDUCED_AVAILABILITY,
72+
public static StorageClass DURABLE_REDUCED_AVAILABILITY = type.createAndRegister(
73+
"DURABLE_REDUCED_AVAILABILITY");
74+
75+
/**
76+
* Get the StorageClass for the given String constant, and throw an exception if the constant is
77+
* not recognized.
78+
*/
79+
public static StorageClass valueOfStrict(String constant) {
80+
return type.valueOfStrict(constant);
81+
}
82+
83+
/**
84+
* Get the StorageClass for the given String constant, and allow unrecognized values.
85+
*/
86+
public static StorageClass valueOf(String constant) {
87+
return type.valueOf(constant);
88+
}
89+
90+
/**
91+
* Return the known values for StorageClass.
92+
*/
93+
public static StorageClass[] values() {
94+
return type.values();
95+
}
5396
}

0 commit comments

Comments
 (0)