Skip to content

Commit 33c41bf

Browse files
committed
Add type-check to PolarisEntity ctors
1 parent 4427dc8 commit 33c41bf

File tree

12 files changed

+47
-16
lines changed

12 files changed

+47
-16
lines changed

polaris-core/src/main/java/org/apache/polaris/core/entity/CatalogEntity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static org.apache.polaris.core.admin.model.StorageConfigInfo.StorageTypeEnum.AZURE;
2222

23+
import com.google.common.base.Preconditions;
2324
import jakarta.annotation.Nonnull;
2425
import jakarta.annotation.Nullable;
2526
import java.util.Collection;
@@ -70,9 +71,11 @@ public class CatalogEntity extends PolarisEntity implements LocationBasedEntity
7071

7172
public CatalogEntity(PolarisBaseEntity sourceEntity) {
7273
super(sourceEntity);
74+
Preconditions.checkState(
75+
getType() == PolarisEntityType.CATALOG, "Invalid entity type: %s", getType());
7376
}
7477

75-
public static CatalogEntity of(PolarisBaseEntity sourceEntity) {
78+
public static @Nullable CatalogEntity of(@Nullable PolarisBaseEntity sourceEntity) {
7679
if (sourceEntity != null) {
7780
return new CatalogEntity(sourceEntity);
7881
}

polaris-core/src/main/java/org/apache/polaris/core/entity/CatalogRoleEntity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
*/
1919
package org.apache.polaris.core.entity;
2020

21+
import com.google.common.base.Preconditions;
22+
import jakarta.annotation.Nullable;
2123
import org.apache.polaris.core.admin.model.CatalogRole;
2224

2325
/** Wrapper for translating between the REST CatalogRole object and the base PolarisEntity type. */
2426
public class CatalogRoleEntity extends PolarisEntity {
2527
public CatalogRoleEntity(PolarisBaseEntity sourceEntity) {
2628
super(sourceEntity);
29+
Preconditions.checkState(
30+
getType() == PolarisEntityType.CATALOG_ROLE, "Invalid entity type: %s", getType());
2731
}
2832

29-
public static CatalogRoleEntity of(PolarisBaseEntity sourceEntity) {
33+
public static @Nullable CatalogRoleEntity of(@Nullable PolarisBaseEntity sourceEntity) {
3034
if (sourceEntity != null) {
3135
return new CatalogRoleEntity(sourceEntity);
3236
}

polaris-core/src/main/java/org/apache/polaris/core/entity/NamespaceEntity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.polaris.core.entity;
2020

2121
import com.fasterxml.jackson.annotation.JsonIgnore;
22+
import com.google.common.base.Preconditions;
23+
import jakarta.annotation.Nullable;
2224
import org.apache.iceberg.catalog.Namespace;
2325
import org.apache.iceberg.rest.RESTUtil;
2426
import org.apache.polaris.core.catalog.PolarisCatalogHelpers;
@@ -33,9 +35,11 @@ public class NamespaceEntity extends PolarisEntity implements LocationBasedEntit
3335

3436
public NamespaceEntity(PolarisBaseEntity sourceEntity) {
3537
super(sourceEntity);
38+
Preconditions.checkState(
39+
getType() == PolarisEntityType.NAMESPACE, "Invalid entity type: %s", getType());
3640
}
3741

38-
public static NamespaceEntity of(PolarisBaseEntity sourceEntity) {
42+
public static @Nullable NamespaceEntity of(@Nullable PolarisBaseEntity sourceEntity) {
3943
if (sourceEntity != null) {
4044
return new NamespaceEntity(sourceEntity);
4145
}

polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisEntity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.fasterxml.jackson.annotation.JsonIgnore;
2323
import com.fasterxml.jackson.annotation.JsonProperty;
2424
import jakarta.annotation.Nonnull;
25+
import jakarta.annotation.Nullable;
2526
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
@@ -151,14 +152,14 @@ public PolarisEntity(
151152
entityVersion);
152153
}
153154

154-
public static PolarisEntity of(PolarisBaseEntity sourceEntity) {
155+
public static @Nullable PolarisEntity of(@Nullable PolarisBaseEntity sourceEntity) {
155156
if (sourceEntity != null) {
156157
return new PolarisEntity(sourceEntity);
157158
}
158159
return null;
159160
}
160161

161-
public static PolarisEntity of(EntityResult result) {
162+
public static @Nullable PolarisEntity of(EntityResult result) {
162163
if (result.isSuccess()) {
163164
return new PolarisEntity(result.getEntity());
164165
}

polaris-core/src/main/java/org/apache/polaris/core/entity/PrincipalEntity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
*/
1919
package org.apache.polaris.core.entity;
2020

21+
import com.google.common.base.Preconditions;
22+
import jakarta.annotation.Nullable;
2123
import org.apache.polaris.core.admin.model.Principal;
2224

2325
/** Wrapper for translating between the REST Principal object and the base PolarisEntity type. */
2426
public class PrincipalEntity extends PolarisEntity {
2527
public PrincipalEntity(PolarisBaseEntity sourceEntity) {
2628
super(sourceEntity);
29+
Preconditions.checkState(
30+
getType() == PolarisEntityType.PRINCIPAL, "Invalid entity type: %s", getType());
2731
}
2832

29-
public static PrincipalEntity of(PolarisBaseEntity sourceEntity) {
33+
public static @Nullable PrincipalEntity of(@Nullable PolarisBaseEntity sourceEntity) {
3034
if (sourceEntity != null) {
3135
return new PrincipalEntity(sourceEntity);
3236
}

polaris-core/src/main/java/org/apache/polaris/core/entity/PrincipalRoleEntity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.polaris.core.entity;
2020

21+
import com.google.common.base.Preconditions;
22+
import jakarta.annotation.Nullable;
2123
import org.apache.polaris.core.admin.model.PrincipalRole;
2224
import org.apache.polaris.core.entity.table.federated.FederatedEntities;
2325

@@ -27,9 +29,11 @@
2729
public class PrincipalRoleEntity extends PolarisEntity {
2830
public PrincipalRoleEntity(PolarisBaseEntity sourceEntity) {
2931
super(sourceEntity);
32+
Preconditions.checkState(
33+
getType() == PolarisEntityType.PRINCIPAL_ROLE, "Invalid entity type: %s", getType());
3034
}
3135

32-
public static PrincipalRoleEntity of(PolarisBaseEntity sourceEntity) {
36+
public static @Nullable PrincipalRoleEntity of(@Nullable PolarisBaseEntity sourceEntity) {
3337
if (sourceEntity != null) {
3438
return new PrincipalRoleEntity(sourceEntity);
3539
}

polaris-core/src/main/java/org/apache/polaris/core/entity/TaskEntity.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.polaris.core.entity;
2020

21+
import com.google.common.base.Preconditions;
22+
import jakarta.annotation.Nullable;
2123
import org.apache.polaris.core.persistence.PolarisObjectMapperUtil;
2224

2325
/**
@@ -27,14 +29,15 @@
2729
public class TaskEntity extends PolarisEntity {
2830
public TaskEntity(PolarisBaseEntity sourceEntity) {
2931
super(sourceEntity);
32+
Preconditions.checkState(
33+
getType() == PolarisEntityType.TASK, "Invalid entity type: %s", getType());
3034
}
3135

32-
public static TaskEntity of(PolarisBaseEntity polarisEntity) {
33-
if (polarisEntity != null) {
34-
return new TaskEntity(polarisEntity);
35-
} else {
36-
return null;
36+
public static @Nullable TaskEntity of(@Nullable PolarisBaseEntity sourceEntity) {
37+
if (sourceEntity != null) {
38+
return new TaskEntity(sourceEntity);
3739
}
40+
return null;
3841
}
3942

4043
public <T> T readData(Class<T> klass) {

polaris-core/src/main/java/org/apache/polaris/core/entity/table/GenericTableEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.polaris.core.entity.table;
2020

2121
import com.fasterxml.jackson.annotation.JsonIgnore;
22+
import jakarta.annotation.Nullable;
2223
import org.apache.iceberg.catalog.Namespace;
2324
import org.apache.iceberg.catalog.TableIdentifier;
2425
import org.apache.iceberg.rest.RESTUtil;
@@ -42,7 +43,7 @@ public GenericTableEntity(PolarisBaseEntity sourceEntity) {
4243
super(sourceEntity);
4344
}
4445

45-
public static GenericTableEntity of(PolarisBaseEntity sourceEntity) {
46+
public static @Nullable GenericTableEntity of(@Nullable PolarisBaseEntity sourceEntity) {
4647
if (sourceEntity != null) {
4748
return new GenericTableEntity(sourceEntity);
4849
}

polaris-core/src/main/java/org/apache/polaris/core/entity/table/IcebergTableLikeEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.polaris.core.entity.table;
2020

2121
import com.fasterxml.jackson.annotation.JsonIgnore;
22+
import jakarta.annotation.Nullable;
2223
import java.util.Optional;
2324
import org.apache.iceberg.catalog.Namespace;
2425
import org.apache.iceberg.catalog.TableIdentifier;
@@ -48,7 +49,7 @@ public IcebergTableLikeEntity(PolarisBaseEntity sourceEntity) {
4849
super(sourceEntity);
4950
}
5051

51-
public static IcebergTableLikeEntity of(PolarisBaseEntity sourceEntity) {
52+
public static @Nullable IcebergTableLikeEntity of(@Nullable PolarisBaseEntity sourceEntity) {
5253
if (sourceEntity != null) {
5354
return new IcebergTableLikeEntity(sourceEntity);
5455
}

polaris-core/src/main/java/org/apache/polaris/core/entity/table/TableLikeEntity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.polaris.core.entity.table;
2020

2121
import com.fasterxml.jackson.annotation.JsonIgnore;
22+
import com.google.common.base.Preconditions;
2223
import jakarta.annotation.Nonnull;
2324
import org.apache.iceberg.catalog.Namespace;
2425
import org.apache.iceberg.catalog.TableIdentifier;
@@ -37,6 +38,8 @@ public abstract class TableLikeEntity extends PolarisEntity implements LocationB
3738

3839
public TableLikeEntity(@Nonnull PolarisBaseEntity sourceEntity) {
3940
super(sourceEntity);
41+
Preconditions.checkState(
42+
getType() == PolarisEntityType.TABLE_LIKE, "Invalid entity type: %s", getType());
4043
}
4144

4245
@JsonIgnore

0 commit comments

Comments
 (0)