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 com.google.common.base.Converter;
20  
21  /**
22   * Provides a function converting back and forth between {@link Revision} and
23   * {@link com.linecorp.centraldogma.common.Revision}.
24   */
25  public final class RevisionConverter extends Converter<com.linecorp.centraldogma.common.Revision, Revision> {
26      public static final Converter<com.linecorp.centraldogma.common.Revision, Revision> TO_DATA =
27              new RevisionConverter();
28  
29      public static final Converter<Revision, com.linecorp.centraldogma.common.Revision> TO_MODEL =
30              TO_DATA.reverse();
31  
32      private RevisionConverter() {}
33  
34      @Override
35      protected Revision doForward(com.linecorp.centraldogma.common.Revision rev) {
36          return new Revision(rev.major(), 0);
37      }
38  
39      @Override
40      protected com.linecorp.centraldogma.common.Revision doBackward(Revision rev) {
41          if (rev.getMinor() != 0) {
42              throw new IllegalArgumentException("minor: " + rev.getMinor() + " (expected: 0)");
43          }
44          return new com.linecorp.centraldogma.common.Revision(rev.getMajor());
45      }
46  }