|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.gravitino.dto.function; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 22 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 23 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 24 | +import javax.annotation.Nullable; |
| 25 | +import lombok.EqualsAndHashCode; |
| 26 | +import lombok.Getter; |
| 27 | +import org.apache.gravitino.function.FunctionColumn; |
| 28 | +import org.apache.gravitino.json.JsonUtils; |
| 29 | +import org.apache.gravitino.rel.types.Type; |
| 30 | + |
| 31 | +/** DTO for function column. */ |
| 32 | +@Getter |
| 33 | +@EqualsAndHashCode |
| 34 | +public class FunctionColumnDTO { |
| 35 | + |
| 36 | + @JsonProperty("name") |
| 37 | + private String name; |
| 38 | + |
| 39 | + @JsonProperty("dataType") |
| 40 | + @JsonSerialize(using = JsonUtils.TypeSerializer.class) |
| 41 | + @JsonDeserialize(using = JsonUtils.TypeDeserializer.class) |
| 42 | + private Type dataType; |
| 43 | + |
| 44 | + @Nullable |
| 45 | + @JsonProperty("comment") |
| 46 | + private String comment; |
| 47 | + |
| 48 | + private FunctionColumnDTO() {} |
| 49 | + |
| 50 | + private FunctionColumnDTO(String name, Type dataType, String comment) { |
| 51 | + this.name = name; |
| 52 | + this.dataType = dataType; |
| 53 | + this.comment = comment; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Convert this DTO to a {@link FunctionColumn} instance. |
| 58 | + * |
| 59 | + * @return The function column. |
| 60 | + */ |
| 61 | + public FunctionColumn toFunctionColumn() { |
| 62 | + return FunctionColumn.of(name, dataType, comment); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a {@link FunctionColumnDTO} from a {@link FunctionColumn} instance. |
| 67 | + * |
| 68 | + * @param column The function column. |
| 69 | + * @return The function column DTO. |
| 70 | + */ |
| 71 | + public static FunctionColumnDTO fromFunctionColumn(FunctionColumn column) { |
| 72 | + return new FunctionColumnDTO(column.name(), column.dataType(), column.comment()); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String toString() { |
| 77 | + return "FunctionColumnDTO{" |
| 78 | + + "name='" |
| 79 | + + name |
| 80 | + + '\'' |
| 81 | + + ", dataType=" |
| 82 | + + dataType |
| 83 | + + ", comment='" |
| 84 | + + comment |
| 85 | + + '\'' |
| 86 | + + '}'; |
| 87 | + } |
| 88 | + |
| 89 | + /** Builder for {@link FunctionColumnDTO}. */ |
| 90 | + public static class Builder { |
| 91 | + private String name; |
| 92 | + private Type dataType; |
| 93 | + private String comment; |
| 94 | + |
| 95 | + /** |
| 96 | + * Set the column name. |
| 97 | + * |
| 98 | + * @param name The column name. |
| 99 | + * @return This builder. |
| 100 | + */ |
| 101 | + public Builder withName(String name) { |
| 102 | + this.name = name; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Set the column data type. |
| 108 | + * |
| 109 | + * @param dataType The column data type. |
| 110 | + * @return This builder. |
| 111 | + */ |
| 112 | + public Builder withDataType(Type dataType) { |
| 113 | + this.dataType = dataType; |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Set the column comment. |
| 119 | + * |
| 120 | + * @param comment The column comment. |
| 121 | + * @return This builder. |
| 122 | + */ |
| 123 | + public Builder withComment(String comment) { |
| 124 | + this.comment = comment; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Build the {@link FunctionColumnDTO}. |
| 130 | + * |
| 131 | + * @return The function column DTO. |
| 132 | + */ |
| 133 | + public FunctionColumnDTO build() { |
| 134 | + return new FunctionColumnDTO(name, dataType, comment); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Create a new builder. |
| 140 | + * |
| 141 | + * @return A new builder. |
| 142 | + */ |
| 143 | + public static Builder builder() { |
| 144 | + return new Builder(); |
| 145 | + } |
| 146 | +} |
0 commit comments