View Javadoc
1   /*
2    * Copyright 2021 TiKV Project Authors.
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   */
17  
18  package org.tikv.common.expression;
19  
20  import static com.google.common.base.Preconditions.checkNotNull;
21  
22  import java.io.Serializable;
23  import org.tikv.common.expression.visitor.ProtoConverter;
24  
25  public class ByItem implements Serializable {
26    private final Expression expr;
27    private final boolean desc;
28  
29    private ByItem(Expression expr, boolean desc) {
30      checkNotNull(expr, "Expr cannot be null for ByItem");
31  
32      this.expr = expr;
33      this.desc = desc;
34    }
35  
36    public static ByItem create(Expression expr, boolean desc) {
37      return new ByItem(expr, desc);
38    }
39  
40    public com.pingcap.tidb.tipb.ByItem toProto(Object context) {
41      com.pingcap.tidb.tipb.ByItem.Builder builder = com.pingcap.tidb.tipb.ByItem.newBuilder();
42      return builder.setExpr(ProtoConverter.toProto(expr, context)).setDesc(desc).build();
43    }
44  
45    public Expression getExpr() {
46      return expr;
47    }
48  
49    public boolean isDesc() {
50      return desc;
51    }
52  
53    @Override
54    public String toString() {
55      return String.format("[%s %s]", expr.toString(), desc ? "DESC" : "ASC");
56    }
57  }