View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.concurrent.atomic.AtomicInteger;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  
27  /**
28   * This class is used to manage the identifiers for
29   * {@link CacheableDeserializer}
30   */
31  @InterfaceAudience.Private
32  public class CacheableDeserializerIdManager {
33    private static final Map<Integer, CacheableDeserializer<Cacheable>> registeredDeserializers =
34      new HashMap<Integer, CacheableDeserializer<Cacheable>>();
35    private static final AtomicInteger identifier = new AtomicInteger(0);
36  
37    /**
38     * Register the given cacheable deserializer and generate an unique identifier
39     * id for it
40     * @param cd
41     * @return the identifier of given cacheable deserializer
42     */
43    public static int registerDeserializer(CacheableDeserializer<Cacheable> cd) {
44      int idx = identifier.incrementAndGet();
45      synchronized (registeredDeserializers) {
46        registeredDeserializers.put(idx, cd);
47      }
48      return idx;
49    }
50  
51    /**
52     * Get the cacheable deserializer as the given identifier Id
53     * @param id
54     * @return CacheableDeserializer
55     */
56    public static CacheableDeserializer<Cacheable> getDeserializer(int id) {
57      return registeredDeserializers.get(id);
58    }
59  }