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     */
018    package org.apache.hadoop.fs;
019    
020    import org.apache.commons.codec.binary.Base64;
021    import org.apache.commons.lang.builder.EqualsBuilder;
022    import org.apache.commons.lang.builder.HashCodeBuilder;
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    
026    /**
027     * HDFS-specific volume identifier which implements {@link VolumeId}. Can be
028     * used to differentiate between the data directories on a single datanode. This
029     * identifier is only unique on a per-datanode basis.
030     */
031    @InterfaceStability.Unstable
032    @InterfaceAudience.Public
033    public class HdfsVolumeId implements VolumeId {
034    
035      private final byte[] id;
036      private final boolean isValid;
037    
038      public HdfsVolumeId(byte[] id, boolean isValid) {
039        this.id = id;
040        this.isValid = isValid;
041      }
042    
043      @Override
044      public boolean isValid() {
045        return isValid;
046      }
047    
048      @Override
049      public int compareTo(VolumeId arg0) {
050        return hashCode() - arg0.hashCode();
051      }
052    
053      @Override
054      public int hashCode() {
055        return new HashCodeBuilder().append(id).toHashCode();
056      }
057    
058      @Override
059      public boolean equals(Object obj) {
060        if (obj == null || obj.getClass() != getClass()) {
061          return false;
062        }
063        if (obj == this) {
064          return true;
065        }
066    
067        HdfsVolumeId that = (HdfsVolumeId) obj;
068        return new EqualsBuilder().append(this.id, that.id).isEquals();
069      }
070    
071      @Override
072      public String toString() {
073        return Base64.encodeBase64String(id);
074      }
075    }