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    
019    package org.apache.hadoop.fs;
020    
021    import java.io.IOException;
022    import java.util.EnumSet;
023    
024    import org.apache.hadoop.HadoopIllegalArgumentException;
025    import org.apache.hadoop.classification.InterfaceAudience;
026    import org.apache.hadoop.classification.InterfaceStability;
027    
028    @InterfaceAudience.Public
029    @InterfaceStability.Stable
030    public enum XAttrSetFlag {
031      /**
032       * Create a new xattr.
033       * If the xattr exists already, exception will be thrown.
034       */
035      CREATE((short) 0x01),
036    
037      /**
038       * Replace a existing xattr.
039       * If the xattr does not exist, exception will be thrown.
040       */
041      REPLACE((short) 0x02);
042    
043      private final short flag;
044    
045      private XAttrSetFlag(short flag) {
046        this.flag = flag;
047      }
048    
049      short getFlag() {
050        return flag;
051      }
052    
053      public static void validate(String xAttrName, boolean xAttrExists,
054          EnumSet<XAttrSetFlag> flag) throws IOException {
055        if (flag == null || flag.isEmpty()) {
056          throw new HadoopIllegalArgumentException("A flag must be specified.");
057        }
058    
059        if (xAttrExists) {
060          if (!flag.contains(REPLACE)) {
061            throw new IOException("XAttr: " + xAttrName +
062                " already exists. The REPLACE flag must be specified.");
063          }
064        } else {
065          if (!flag.contains(CREATE)) {
066            throw new IOException("XAttr: " + xAttrName +
067                " does not exist. The CREATE flag must be specified.");
068          }
069        }
070      }
071    }