Skip to content

Commit

Permalink
Refactor HivePrivilegeInfo
Browse files Browse the repository at this point in the history
Extracted-From: prestodb/presto#10904
  • Loading branch information
Andrii Rosa authored and sopel39 committed Jan 29, 2019
1 parent c603474 commit de9890c
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@
import static io.prestosql.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.SELECT;
import static io.prestosql.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.UPDATE;
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;
Expand All @@ -51,7 +52,7 @@ public HivePrivilegeInfo(
@JsonProperty("hivePrivilege") HivePrivilege hivePrivilege,
@JsonProperty("grantOption") boolean grantOption)
{
this.hivePrivilege = hivePrivilege;
this.hivePrivilege = requireNonNull(hivePrivilege, "hivePrivilege is null");
this.grantOption = grantOption;
}

Expand All @@ -67,20 +68,15 @@ public boolean isGrantOption()
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":
Expand All @@ -91,8 +87,9 @@ public static Set<HivePrivilegeInfo> parsePrivilege(PrivilegeGrantInfo userGrant
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)
Expand Down Expand Up @@ -120,7 +117,7 @@ public boolean isContainedIn(HivePrivilegeInfo hivePrivilegeInfo)

public Set<PrivilegeInfo> toPrivilegeInfo()
{
switch (getHivePrivilege()) {
switch (hivePrivilege) {
case SELECT:
return ImmutableSet.of(new PrivilegeInfo(Privilege.SELECT, isGrantOption()));
case INSERT:
Expand All @@ -130,11 +127,12 @@ public Set<PrivilegeInfo> toPrivilegeInfo()
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
Expand Down

0 comments on commit de9890c

Please sign in to comment.