001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.fs;
019
020import org.apache.commons.lang.builder.EqualsBuilder;
021import org.apache.commons.lang.builder.HashCodeBuilder;
022import org.apache.hadoop.classification.InterfaceAudience;
023import org.apache.hadoop.classification.InterfaceStability;
024import org.apache.hadoop.util.StringUtils;
025
026import com.google.common.base.Preconditions;
027
028/**
029 * HDFS-specific volume identifier which implements {@link VolumeId}. Can be
030 * used to differentiate between the data directories on a single datanode. This
031 * identifier is only unique on a per-datanode basis.
032 */
033@InterfaceStability.Unstable
034@InterfaceAudience.Public
035public class HdfsVolumeId implements VolumeId {
036  
037  private final byte[] id;
038
039  public HdfsVolumeId(byte[] id) {
040    Preconditions.checkNotNull(id, "id cannot be null");
041    this.id = id;
042  }
043
044  @Override
045  public int compareTo(VolumeId arg0) {
046    if (arg0 == null) {
047      return 1;
048    }
049    return hashCode() - arg0.hashCode();
050  }
051
052  @Override
053  public int hashCode() {
054    return new HashCodeBuilder().append(id).toHashCode();
055  }
056
057  @Override
058  public boolean equals(Object obj) {
059    if (obj == null || obj.getClass() != getClass()) {
060      return false;
061    }
062    if (obj == this) {
063      return true;
064    }
065    HdfsVolumeId that = (HdfsVolumeId) obj;
066    return new EqualsBuilder().append(this.id, that.id).isEquals();
067  }
068
069  @Override
070  public String toString() {
071    return StringUtils.byteToHexString(id);
072  }
073}