/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.master.block;

import javax.annotation.concurrent.ThreadSafe;

/**
 * This class provides a set of methods related to block IDs. Each block ID is a value of long with
 * the following two parts:
 * <ul>
 * <li>The most significant 5 bytes (40 bits) represent the container ID of this block and</li>
 * <li>The least significant 3 bytes (24 bits) represent the sequence number of this block in the
 * container.</li>
 * </ul>
 */
@ThreadSafe
public final class BlockID {

  private static final int CONTAINER_ID_BITS = 40;
  private static final int SEQUENCE_NUMBER_BITS = 64 - CONTAINER_ID_BITS;
  private static final long CONTAINER_ID_MASK = (1L << CONTAINER_ID_BITS) - 1;
  private static final long SEQUENCE_NUMBER_MASK = (1L << SEQUENCE_NUMBER_BITS) - 1;

  private BlockID() {
    // prevent instantiation of a util class
  }

  /**
   * @param containerID the container ID to create the block ID with
   * @param sequenceNumber the sequence number to create the block ID with
   * @return the block ID constructed with the container ID and sequence number
   */
  public static long createBlockID(long containerID, long sequenceNumber) {
    // TODO(gene): Check for valid IDs here?
    return ((containerID & CONTAINER_ID_MASK) << SEQUENCE_NUMBER_BITS)
        | (sequenceNumber & SEQUENCE_NUMBER_MASK);
  }

  /**
   * @param blockID the block ID to get the container ID for
   * @return the container ID of a specified block ID
   */
  public static long getContainerID(long blockID) {
    return (blockID >> SEQUENCE_NUMBER_BITS) & CONTAINER_ID_MASK;
  }

  /**
   * @param blockID the block ID to get the sequence number for
   * @return the sequence number of the specified block ID
   */
  public static long getSequenceNumber(long blockID) {
    return blockID & SEQUENCE_NUMBER_MASK;
  }

  /**
   * @return the maximum possible sequence number for block IDs
   */
  public static long getMaxSequenceNumber() {
    return SEQUENCE_NUMBER_MASK;
  }
}

@@ -989,17 +989,17 @@
 s BlockI
-D
+d
  %7B%0A%0A  pr
@@ -1028,25 +1028,25 @@
  CONTAINER_I
-D
+d
 _BITS = 40;%0A
@@ -1107,25 +1107,25 @@
  CONTAINER_I
-D
+d
 _BITS;%0A  pri
@@ -1154,25 +1154,25 @@
  CONTAINER_I
-D
+d
 _MASK = (1L 
@@ -1185,17 +1185,17 @@
 TAINER_I
-D
+d
 _BITS) -
@@ -1300,17 +1300,17 @@
 e BlockI
-D
+d
 () %7B%0A   
@@ -1625,17 +1625,17 @@
 teBlockI
-D
+d
 (long co
@@ -1642,17 +1642,17 @@
 ntainerI
-D
+d
 , long s
@@ -1736,17 +1736,17 @@
 ntainerI
-D
+d
  & CONTA
@@ -1747,25 +1747,25 @@
  CONTAINER_I
-D
+d
 _MASK) %3C%3C SE
@@ -2001,17 +2001,17 @@
 ntainerI
-D
+d
 (long bl
@@ -2006,33 +2006,33 @@
 erId(long blockI
-D
+d
 ) %7B%0A    return (
@@ -2037,17 +2037,17 @@
  (blockI
-D
+d
  %3E%3E SEQU
@@ -2077,17 +2077,17 @@
 TAINER_I
-D
+d
 _MASK;%0A 
@@ -2275,17 +2275,17 @@
 g blockI
-D
+d
 ) %7B%0A    
@@ -2297,17 +2297,17 @@
 n blockI
-D
+d
  & SEQUE

update ld to ID in javadoc