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: */
017: package org.apache.commons.vfs;
018:
019: import java.util.Map;
020: import java.util.TreeMap;
021:
022: /**
023: * Container for FileSystemOptions.<br>
024: * You have to use *FileSystemConfigBuilder.getInstance() to fill this container<br>
025: * * = the filesystem provider short name
026: *
027: * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
028: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
029: * @see org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder
030: * @see org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder
031: */
032: public class FileSystemOptions {
033: private Map options = new TreeMap();
034:
035: private class FileSystemOptionKey implements Comparable {
036: private final Class fileSystemClass;
037: private final String name;
038:
039: private FileSystemOptionKey(Class fileSystemClass, String name) {
040: this .fileSystemClass = fileSystemClass;
041: this .name = name;
042: }
043:
044: public int compareTo(Object o) {
045: FileSystemOptionKey k = (FileSystemOptionKey) o;
046:
047: int ret = k.fileSystemClass.getName().compareTo(
048: k.fileSystemClass.getName());
049: if (ret != 0) {
050: return ret;
051: }
052:
053: return name.compareTo(k.name);
054: }
055:
056: public boolean equals(Object o) {
057: if (this == o) {
058: return true;
059: }
060: if (o == null || getClass() != o.getClass()) {
061: return false;
062: }
063:
064: final FileSystemOptionKey that = (FileSystemOptionKey) o;
065:
066: if (!fileSystemClass.equals(that.fileSystemClass)) {
067: return false;
068: }
069: if (!name.equals(that.name)) {
070: return false;
071: }
072:
073: return true;
074: }
075:
076: public int hashCode() {
077: int result;
078: result = fileSystemClass.hashCode();
079: result = 29 * result + name.hashCode();
080: return result;
081: }
082: }
083:
084: public FileSystemOptions() {
085: }
086:
087: void setOption(Class fileSystemClass, String name, Object value) {
088: options.put(new FileSystemOptionKey(fileSystemClass, name),
089: value);
090: }
091:
092: Object getOption(Class fileSystemClass, String name) {
093: FileSystemOptionKey key = new FileSystemOptionKey(
094: fileSystemClass, name);
095: return options.get(key);
096: }
097:
098: boolean hasOption(Class fileSystemClass, String name) {
099: FileSystemOptionKey key = new FileSystemOptionKey(
100: fileSystemClass, name);
101: return options.containsKey(key);
102: }
103:
104: public int compareTo(FileSystemOptions other) {
105: if (this == other) {
106: // the same instance
107: return 0;
108: }
109:
110: int propsSz = options == null ? 0 : options.size();
111: int propsFkSz = other.options == null ? 0 : other.options
112: .size();
113: if (propsSz < propsFkSz) {
114: return -1;
115: }
116: if (propsSz > propsFkSz) {
117: return 1;
118: }
119: if (propsSz == 0) {
120: // props empty
121: return 0;
122: }
123:
124: int hash = options.hashCode();
125: int hashFk = other.options.hashCode();
126: if (hash < hashFk) {
127: return -1;
128: }
129: if (hash > hashFk) {
130: return 1;
131: }
132:
133: // bad props not the same instance, but looks like the same
134: // TODO: compare Entry by Entry
135: return 0;
136: }
137: }
|