1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.internal.thrift;
18  
19  import java.util.Collections;
20  
21  import com.google.common.base.Converter;
22  
23  /**
24   * Provides a function converting back and forth between {@link Query} and
25   * {@link com.linecorp.centraldogma.common.Query}.
26   */
27  public final class QueryConverter extends Converter<com.linecorp.centraldogma.common.Query<?>, Query> {
28      public static final Converter<com.linecorp.centraldogma.common.Query<?>, Query> TO_DATA =
29              new QueryConverter();
30  
31      public static final Converter<Query, com.linecorp.centraldogma.common.Query<?>> TO_MODEL =
32              TO_DATA.reverse();
33  
34      private QueryConverter() {}
35  
36      @Override
37      protected Query doForward(com.linecorp.centraldogma.common.Query<?> query) {
38          switch (query.type()) {
39              case IDENTITY:
40                  return new Query(query.path(), QueryType.IDENTITY, Collections.emptyList());
41              case IDENTITY_TEXT:
42                  return new Query(query.path(), QueryType.IDENTITY_TEXT, Collections.emptyList());
43              case IDENTITY_JSON:
44                  return new Query(query.path(), QueryType.IDENTITY_JSON, Collections.emptyList());
45              case JSON_PATH:
46                  return new Query(query.path(), QueryType.JSON_PATH, query.expressions());
47          }
48  
49          throw new Error();
50      }
51  
52      @Override
53      protected com.linecorp.centraldogma.common.Query<?> doBackward(Query query) {
54          switch (query.getType()) {
55              case IDENTITY:
56                  return com.linecorp.centraldogma.common.Query.of(
57                          com.linecorp.centraldogma.common.QueryType.IDENTITY, query.getPath());
58              case IDENTITY_TEXT:
59                  return com.linecorp.centraldogma.common.Query.ofText(query.getPath());
60              case IDENTITY_JSON:
61                  return com.linecorp.centraldogma.common.Query.ofJson(query.getPath());
62              case JSON_PATH:
63                  return com.linecorp.centraldogma.common.Query.ofJsonPath(query.getPath(),
64                                                                           query.getExpressions());
65          }
66  
67          throw new Error();
68      }
69  }