View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.cdc.core.ack;
19  
20  import com.google.common.base.Splitter;
21  import lombok.Getter;
22  import lombok.RequiredArgsConstructor;
23  import org.apache.commons.lang3.RandomStringUtils;
24  
25  import java.util.List;
26  
27  /**
28   * CDC ack id.
29   */
30  @RequiredArgsConstructor
31  @Getter
32  public final class CDCAckId {
33      
34      private final String importerId;
35      
36      private final String random;
37      
38      /**
39       * Build ack id.
40       *
41       * @param importerId importer id
42       * @return ack id
43       */
44      public static CDCAckId build(final String importerId) {
45          return new CDCAckId(importerId, RandomStringUtils.randomAlphanumeric(16));
46      }
47      
48      /**
49       * Marshal ack id.
50       *
51       * @return ack id
52       */
53      public String marshal() {
54          return importerId + "_" + random;
55      }
56      
57      /**
58       * Unmarshal ack id from text.
59       *
60       * @param text text
61       * @return ack id
62       */
63      public static CDCAckId unmarshal(final String text) {
64          List<String> parts = Splitter.on('_').trimResults().omitEmptyStrings().splitToList(text);
65          return new CDCAckId(parts.get(0), parts.get(1));
66      }
67  }