1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.vfs2.util;
18
19 import java.nio.file.AccessMode;
20 import java.util.Arrays;
21 import java.util.Objects;
22
23 import org.apache.commons.lang3.ArraySorter;
24
25 /**
26 * An enumerated type representing the modes of a random access content.
27 * <p>
28 * TODO Replace with {@link AccessMode}.
29 * </p>
30 */
31 public enum RandomAccessMode {
32
33 /**
34 * Read access mode.
35 */
36 READ(true, false) {
37
38 /**
39 * Returns a defensive copy of an internal constant array.
40 */
41 @Override
42 public AccessMode[] toAccessModes() {
43 return ACCESS_MODE_READ.clone();
44 }
45 },
46
47 /**
48 * Read-write access mode.
49 */
50 READWRITE(true, true) {
51
52 /**
53 * Returns a defensive copy of an internal constant array.
54 */
55 @Override
56 public AccessMode[] toAccessModes() {
57 return ACCESS_MODE_READ_WRITE.clone();
58 }
59 };
60
61 private static final AccessMode[] ACCESS_MODE_READ = {AccessMode.READ};
62 private static final AccessMode[] ACCESS_MODE_READ_WRITE = {AccessMode.READ, AccessMode.WRITE};
63
64 /**
65 * Converts an array of {@link AccessMode} into a RandomAccessMode.
66 *
67 * @param accessModes AccessMode array, only {@link AccessMode#READ} and {@link AccessMode#WRITE} are supported.
68 * @return A RandomAccessMode.
69 * @since 2.10.0
70 */
71 public static RandomAccessMode from(final AccessMode... accessModes) {
72 Objects.requireNonNull(accessModes, "accessModes");
73 if (accessModes.length == 0) {
74 throw new IllegalArgumentException("Empty AccessMode[].");
75 }
76 final AccessMode[] modes = ArraySorter.sort(accessModes.clone());
77 if (Arrays.binarySearch(modes, AccessMode.WRITE) >= 0) {
78 return READWRITE;
79 }
80 if (Arrays.binarySearch(modes, AccessMode.READ) >= 0) {
81 return READ;
82 }
83 throw new IllegalArgumentException(Arrays.toString(accessModes));
84 }
85 private final boolean read;
86
87 private final boolean write;
88
89 RandomAccessMode(final boolean read, final boolean write) {
90 this.read = read;
91 this.write = write;
92 }
93
94 /**
95 * Gets this instance as an access mode string suitable for other APIs, like {@code "r"} for {@link #READ} and
96 * {@code "rw"} for {@link #READWRITE}.
97 *
98 * @return An access mode String, {@code "r"} for {@link #READ} and {@code "rw"} for {@link #READWRITE}.
99 * @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 }