1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2;
18
19 import java.util.Arrays;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.SortedMap;
23 import java.util.TreeMap;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public final class FileSystemOptions implements Cloneable {
48
49
50 private final Map<FileSystemOptionKey, Object> options;
51
52
53
54
55 public FileSystemOptions() {
56 this(new TreeMap<>());
57 }
58
59 protected FileSystemOptions(final Map<FileSystemOptionKey, Object> options) {
60 this.options = options;
61 }
62
63
64
65
66 private static final class FileSystemOptionKey implements Comparable<FileSystemOptionKey> {
67
68 private static final int HASH = 29;
69
70
71 private final Class<? extends FileSystem> fileSystemClass;
72
73
74 private final String name;
75
76
77
78 private FileSystemOptionKey(final Class<? extends FileSystem> fileSystemClass, final String name) {
79 this.fileSystemClass = fileSystemClass;
80 this.name = name;
81 }
82
83 @Override
84 public int compareTo(final FileSystemOptionKey o) {
85 final int ret = fileSystemClass.getName().compareTo(o.fileSystemClass.getName());
86 if (ret != 0) {
87 return ret;
88 }
89 return name.compareTo(o.name);
90 }
91
92 @Override
93 public boolean equals(final Object o) {
94 if (this == o) {
95 return true;
96 }
97 if (o == null || getClass() != o.getClass()) {
98 return false;
99 }
100
101 final FileSystemOptionKey that = (FileSystemOptionKey) o;
102
103 if (!fileSystemClass.equals(that.fileSystemClass)) {
104 return false;
105 }
106 return name.equals(that.name);
107 }
108
109 @Override
110 public int hashCode() {
111 int result;
112 result = fileSystemClass.hashCode();
113 result = HASH * result + name.hashCode();
114 return result;
115 }
116
117 @Override
118 public String toString() {
119 return fileSystemClass.getName() + "." + name;
120 }
121 }
122
123 void setOption(final Class<? extends FileSystem> fileSystemClass, final String name, final Object value) {
124 options.put(new FileSystemOptionKey(fileSystemClass, name), value);
125 }
126
127 <T> T getOption(final Class<? extends FileSystem> fileSystemClass, final String name) {
128 final FileSystemOptionKey key = new FileSystemOptionKey(fileSystemClass, name);
129 return (T) options.get(key);
130 }
131
132 boolean hasOption(final Class<? extends FileSystem> fileSystemClass, final String name) {
133 final FileSystemOptionKey key = new FileSystemOptionKey(fileSystemClass, name);
134 return options.containsKey(key);
135 }
136
137 public int compareTo(final FileSystemOptions other) {
138 if (this == other) {
139
140 return 0;
141 }
142
143 final int propsSz = options == null ? 0 : options.size();
144 final int propsFkSz = other.options == null ? 0 : other.options.size();
145 if (propsSz < propsFkSz) {
146 return -1;
147 }
148 if (propsSz > propsFkSz) {
149 return 1;
150 }
151 if (propsSz == 0) {
152
153 return 0;
154 }
155
156
157 final SortedMap<FileSystemOptionKey, Object> myOptions = options instanceof SortedMap
158 ? (SortedMap<FileSystemOptionKey, Object>) options
159 : new TreeMap<>(options);
160 final SortedMap<FileSystemOptionKey, Object> theirOptions = other.options instanceof SortedMap
161 ? (SortedMap<FileSystemOptionKey, Object>) other.options
162 : new TreeMap<>(other.options);
163 final Iterator<FileSystemOptionKey> optKeysIter = myOptions.keySet().iterator();
164 final Iterator<FileSystemOptionKey> otherKeysIter = theirOptions.keySet().iterator();
165 while (optKeysIter.hasNext()) {
166 final int comp = optKeysIter.next().compareTo(otherKeysIter.next());
167 if (comp != 0) {
168 return comp;
169 }
170 }
171
172 final int hash = Arrays.deepHashCode(myOptions.values().toArray());
173 final int hashFk = Arrays.deepHashCode(theirOptions.values().toArray());
174 return Integer.compare(hash, hashFk);
175
176
177 }
178
179 @Override
180 public int hashCode() {
181 final int prime = 31;
182 int result = 1;
183 if (options == null) {
184 result = prime * result;
185 } else {
186 final SortedMap<FileSystemOptionKey, Object> myOptions = options instanceof SortedMap
187 ? (SortedMap<FileSystemOptionKey, Object>) options
188 : new TreeMap<>(options);
189 result = prime * result + myOptions.keySet().hashCode();
190 result = prime * result + Arrays.deepHashCode(myOptions.values().toArray());
191 }
192 return result;
193 }
194
195 @Override
196 public boolean equals(final Object obj) {
197 if (this == obj) {
198 return true;
199 }
200 if (obj == null) {
201 return false;
202 }
203 if (getClass() != obj.getClass()) {
204 return false;
205 }
206 final FileSystemOptions./org/apache/commons/vfs2/FileSystemOptions.html#FileSystemOptions">FileSystemOptions other = (FileSystemOptions) obj;
207 return compareTo(other) == 0;
208 }
209
210
211
212
213
214
215 @Override
216 public Object clone() {
217 return new FileSystemOptions(new TreeMap<>(options));
218 }
219
220 @Override
221 public String toString() {
222 return options.toString();
223 }
224 }