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.time.Instant;
20  import java.util.Collections;
21  
22  import com.google.common.base.Converter;
23  
24  import com.linecorp.centraldogma.common.Markup;
25  
26  /**
27   * Provides a function converting back and forth between {@link Commit} and
28   * {@link com.linecorp.centraldogma.common.Commit}.
29   */
30  public final class CommitConverter extends Converter<com.linecorp.centraldogma.common.Commit, Commit> {
31      public static final Converter<com.linecorp.centraldogma.common.Commit, Commit> TO_DATA =
32              new CommitConverter();
33  
34      public static final Converter<Commit, com.linecorp.centraldogma.common.Commit> TO_MODEL =
35              TO_DATA.reverse();
36  
37      private CommitConverter() {}
38  
39      @Override
40      protected Commit doForward(com.linecorp.centraldogma.common.Commit commit) {
41          final Comment comment = new Comment();
42          comment.setContent(commit.detail());
43          comment.setMarkup(MarkupConverter.TO_DATA.convert(commit.markup()));
44          return new Commit(RevisionConverter.TO_DATA.convert(commit.revision()),
45                            AuthorConverter.TO_DATA.convert(commit.author()),
46                            commit.whenAsText(),
47                            commit.summary(), comment, Collections.emptyList());
48      }
49  
50      @Override
51      protected com.linecorp.centraldogma.common.Commit doBackward(Commit commit) {
52          final Markup markup = Markup.valueOf(commit.getDetail().getMarkup().name());
53          return new com.linecorp.centraldogma.common.Commit(
54                  RevisionConverter.TO_MODEL.convert(commit.getRevision()),
55                  AuthorConverter.TO_MODEL.convert(commit.getAuthor()),
56                  Instant.parse(commit.getTimestamp()).toEpochMilli(),
57                  commit.getSummary(),
58                  commit.getDetail().getContent(),
59                  markup);
60      }
61  }