Refactor HivePrivilegeInfo
@@ -34,13 +34,14 @@ import static com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege
import static com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.UPDATE;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Locale.ENGLISH;
+import static java.util.Objects.requireNonNull;
@Immutable
public class HivePrivilegeInfo
{
public enum HivePrivilege
{
- SELECT, INSERT, UPDATE, DELETE, OWNERSHIP;
+ SELECT, INSERT, UPDATE, DELETE, OWNERSHIP
}
private final HivePrivilege hivePrivilege;
@@ -51,7 +52,7 @@ public class HivePrivilegeInfo
@JsonProperty("hivePrivilege") HivePrivilege hivePrivilege,
@JsonProperty("grantOption") boolean grantOption)
{
- this.hivePrivilege = hivePrivilege;
+ this.hivePrivilege = requireNonNull(hivePrivilege, "hivePrivilege is null");
this.grantOption = grantOption;
}
@@ -67,20 +68,15 @@ public class HivePrivilegeInfo
return grantOption;
}
- public HivePrivilegeInfo withGrantOption(boolean grantOption)
- {
- return new HivePrivilegeInfo(hivePrivilege, grantOption);
- }
-
public static Set<HivePrivilegeInfo> parsePrivilege(PrivilegeGrantInfo userGrant)
{
boolean withGrantOption = userGrant.isGrantOption();
String name = userGrant.getPrivilege().toUpperCase(ENGLISH);
switch (name) {
case "ALL":
- return Arrays.asList(HivePrivilege.values()).stream()
+ return ImmutableSet.copyOf(Arrays.stream(HivePrivilege.values())
.map(hivePrivilege -> new HivePrivilegeInfo(hivePrivilege, withGrantOption))
- .collect(Collectors.toSet());
+ .collect(Collectors.toSet()));
case "SELECT":
return ImmutableSet.of(new HivePrivilegeInfo(SELECT, withGrantOption));
case "INSERT":
@@ -91,8 +87,9 @@ public class HivePrivilegeInfo
return ImmutableSet.of(new HivePrivilegeInfo(DELETE, withGrantOption));
case "OWNERSHIP":
return ImmutableSet.of(new HivePrivilegeInfo(OWNERSHIP, withGrantOption));
+ default:
+ throw new IllegalArgumentException("Unsupported privilege name: " + name);
}
- return ImmutableSet.of();
}
public static HivePrivilege toHivePrivilege(Privilege privilege)
@@ -120,7 +117,7 @@ public class HivePrivilegeInfo
public Set<PrivilegeInfo> toPrivilegeInfo()
{
- switch (getHivePrivilege()) {
+ switch (hivePrivilege) {
case SELECT:
return ImmutableSet.of(new PrivilegeInfo(Privilege.SELECT, isGrantOption()));
case INSERT:
@@ -130,11 +127,12 @@ public class HivePrivilegeInfo
case UPDATE:
return ImmutableSet.of(new PrivilegeInfo(Privilege.UPDATE, isGrantOption()));
case OWNERSHIP:
- return Arrays.asList(Privilege.values()).stream()
+ return ImmutableSet.copyOf(Arrays.stream(Privilege.values())
.map(privilege -> new PrivilegeInfo(privilege, Boolean.TRUE))
- .collect(Collectors.toSet());
+ .collect(Collectors.toSet()));
+ default:
+ throw new IllegalArgumentException("Unsupported hivePrivilege: " + hivePrivilege);
}
- return null;
}
@Override
