001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.util; 018 019import java.nio.file.AccessMode; 020import java.util.Arrays; 021import java.util.Objects; 022 023import org.apache.commons.lang3.ArraySorter; 024 025/** 026 * An enumerated type representing the modes of a random access content. 027 * <p> 028 * TODO Replace with {@link AccessMode}. 029 * </p> 030 */ 031public enum RandomAccessMode { 032 033 /** 034 * Read access mode. 035 */ 036 READ(true, false) { 037 038 /** 039 * Returns a defensive copy of an internal constant array. 040 */ 041 @Override 042 public AccessMode[] toAccessModes() { 043 return ACCESS_MODE_READ.clone(); 044 } 045 }, 046 047 /** 048 * Read-write access mode. 049 */ 050 READWRITE(true, true) { 051 052 /** 053 * Returns a defensive copy of an internal constant array. 054 */ 055 @Override 056 public AccessMode[] toAccessModes() { 057 return ACCESS_MODE_READ_WRITE.clone(); 058 } 059 }; 060 061 private static final AccessMode[] ACCESS_MODE_READ = {AccessMode.READ}; 062 private static final AccessMode[] ACCESS_MODE_READ_WRITE = {AccessMode.READ, AccessMode.WRITE}; 063 064 /** 065 * Converts an array of {@link AccessMode} into a RandomAccessMode. 066 * 067 * @param accessModes AccessMode array, only {@link AccessMode#READ} and {@link AccessMode#WRITE} are supported. 068 * @return A RandomAccessMode. 069 * @since 2.10.0 070 */ 071 public static RandomAccessMode from(final AccessMode... accessModes) { 072 Objects.requireNonNull(accessModes, "accessModes"); 073 if (accessModes.length == 0) { 074 throw new IllegalArgumentException("Empty AccessMode[]."); 075 } 076 final AccessMode[] modes = ArraySorter.sort(accessModes.clone()); 077 if (Arrays.binarySearch(modes, AccessMode.WRITE) >= 0) { 078 return READWRITE; 079 } 080 if (Arrays.binarySearch(modes, AccessMode.READ) >= 0) { 081 return READ; 082 } 083 throw new IllegalArgumentException(Arrays.toString(accessModes)); 084 } 085 private final boolean read; 086 087 private final boolean write; 088 089 RandomAccessMode(final boolean read, final boolean write) { 090 this.read = read; 091 this.write = write; 092 } 093 094 /** 095 * Gets this instance as an access mode string suitable for other APIs, like {@code "r"} for {@link #READ} and 096 * {@code "rw"} for {@link #READWRITE}. 097 * 098 * @return An access mode String, {@code "r"} for {@link #READ} and {@code "rw"} for {@link #READWRITE}. 099 * @since 2.0 100 */ 101 public String getModeString() { 102 if (requestRead()) { 103 if (requestWrite()) { 104 return "rw"; // NON-NLS 105 } 106 return "r"; // NON-NLS 107 } 108 if (requestWrite()) { 109 return "w"; // NON-NLS 110 } 111 112 return ""; 113 } 114 115 /** 116 * Tests the read flag. 117 * 118 * @return true for read. 119 */ 120 public boolean requestRead() { 121 return read; 122 } 123 124 /** 125 * Tests the write flag. 126 * 127 * @return true for write. 128 */ 129 public boolean requestWrite() { 130 return write; 131 } 132 133 /** 134 * Converts this instance to an array of {@link AccessMode}. 135 * 136 * @return an array of {@link AccessMode}. 137 * @since 2.10.0 138 */ 139 public AccessMode[] toAccessModes() { 140 // TODO If this method is abstract, JApiCmp reports: 141 // METHOD_ABSTRACT_ADDED_TO_CLASS,org.apache.commons.vfs2.util.RandomAccessMode:CLASS_NOW_ABSTRACT 142 return null; 143 } 144 145}