/*
 * 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;
  }
}

@@ -648,17 +648,17 @@
  block I
-d
+D
 s. Each 
@@ -664,17 +664,17 @@
  block I
-d
+D
  is a va
@@ -797,17 +797,17 @@
 tainer I
-d
+D
  of this
@@ -989,17 +989,17 @@
 s BlockI
-d
+D
  %7B%0A%0A  pr
@@ -1300,17 +1300,17 @@
 e BlockI
-d
+D
 () %7B%0A   
@@ -1384,17 +1384,17 @@
 ntainerI
-d
+D
  the con
@@ -1400,18 +1400,18 @@
 ntainer 
-id
+ID
  to crea
@@ -1415,34 +1415,34 @@
 reate the block 
-id
+ID
  with%0A   * @para
@@ -1498,18 +1498,18 @@
 e block 
-id
+ID
  with%0A  
@@ -1529,18 +1529,18 @@
 e block 
-id
+ID
  constru
@@ -1563,18 +1563,18 @@
 ntainer 
-id
+ID
  and seq
@@ -1625,17 +1625,17 @@
 teBlockI
-d
+D
 (long co
@@ -1642,17 +1642,17 @@
 ntainerI
-d
+D
 , long s
@@ -1703,18 +1703,18 @@
 r valid 
-id
+ID
 s here?%0A
@@ -1736,17 +1736,17 @@
 ntainerI
-d
+D
  & CONTA
@@ -1856,33 +1856,33 @@
  * @param blockI
-d
+D
  the block id to
@@ -1868,34 +1868,34 @@
 ockID the block 
-id
+ID
  to get the cont
@@ -1900,18 +1900,18 @@
 ntainer 
-id
+ID
  for%0A   
@@ -1934,18 +1934,18 @@
 ntainer 
-id
+ID
  of a sp
@@ -1950,34 +1950,34 @@
 specified block 
-id
+ID
 %0A   */%0A  public 
@@ -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
@@ -2114,17 +2114,17 @@
 m blockI
-d
+D
  the blo
@@ -2126,18 +2126,18 @@
 e block 
-id
+ID
  to get 
@@ -2216,18 +2216,18 @@
 d block 
-id
+ID
 %0A   */%0A 
@@ -2275,17 +2275,17 @@
 g blockI
-d
+D
 ) %7B%0A    
@@ -2297,17 +2297,17 @@
 n blockI
-d
+D
  & SEQUE
@@ -2394,18 +2394,18 @@
 r block 
-id
+ID
 s%0A   */%0A

update ld to ID in javadoc