KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > FileSystemOptions


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs;
17
18 import java.util.Map JavaDoc;
19 import java.util.TreeMap JavaDoc;
20
21 /**
22  * Container for FileSystemOptions.<br>
23  * You have to use *FileSystemConfigBuilder.getInstance() to fill this container<br>
24  * * = the filesystem provider short name
25  *
26  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
27  * @see org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder
28  * @see org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder
29  */

30 public class FileSystemOptions
31 {
32     private Map JavaDoc options = new TreeMap JavaDoc();
33
34     private class FileSystemOptionKey implements Comparable JavaDoc
35     {
36         private final Class JavaDoc fileSystemClass;
37         private final String JavaDoc name;
38
39         private FileSystemOptionKey(Class JavaDoc fileSystemClass, String JavaDoc name)
40         {
41             this.fileSystemClass = fileSystemClass;
42             this.name = name;
43         }
44
45         public int compareTo(Object JavaDoc o)
46         {
47             FileSystemOptionKey k = (FileSystemOptionKey) o;
48
49             int ret = k.fileSystemClass.getName().compareTo(k.fileSystemClass.getName());
50             if (ret != 0)
51             {
52                 return ret;
53             }
54
55             return name.compareTo(k.name);
56         }
57
58         public boolean equals(Object JavaDoc o)
59         {
60             if (this == o)
61             {
62                 return true;
63             }
64             if (o == null || getClass() != o.getClass())
65             {
66                 return false;
67             }
68
69             final FileSystemOptionKey that = (FileSystemOptionKey) o;
70
71             if (!fileSystemClass.equals(that.fileSystemClass))
72             {
73                 return false;
74             }
75             if (!name.equals(that.name))
76             {
77                 return false;
78             }
79
80             return true;
81         }
82
83         public int hashCode()
84         {
85             int result;
86             result = fileSystemClass.hashCode();
87             result = 29 * result + name.hashCode();
88             return result;
89         }
90     }
91
92     public FileSystemOptions()
93     {
94     }
95
96     void setOption(Class JavaDoc fileSystemClass, String JavaDoc name, Object JavaDoc value)
97     {
98         options.put(new FileSystemOptionKey(fileSystemClass, name), value);
99     }
100
101     Object JavaDoc getOption(Class JavaDoc fileSystemClass, String JavaDoc name)
102     {
103         FileSystemOptionKey key = new FileSystemOptionKey(fileSystemClass, name);
104         return options.get(key);
105     }
106
107     boolean hasOption(Class JavaDoc fileSystemClass, String JavaDoc name)
108     {
109         FileSystemOptionKey key = new FileSystemOptionKey(fileSystemClass, name);
110         return options.containsKey(key);
111     }
112
113     public int compareTo(FileSystemOptions other)
114     {
115         if (this == other)
116         {
117             // the same instance
118
return 0;
119         }
120
121         int propsSz = options == null ? 0 : options.size();
122         int propsFkSz = other.options == null ? 0 : other.options.size();
123         if (propsSz < propsFkSz)
124         {
125             return -1;
126         }
127         if (propsSz > propsFkSz)
128         {
129             return 1;
130         }
131         if (propsSz == 0)
132         {
133             // props empty
134
return 0;
135         }
136
137         int hash = options.hashCode();
138         int hashFk = other.options.hashCode();
139         if (hash < hashFk)
140         {
141             return -1;
142         }
143         if (hash > hashFk)
144         {
145             return 1;
146         }
147
148         // bad props not the same instance, but looks like the same
149
// TODO: compare Entry by Entry
150
return 0;
151     }
152 }
153
Popular Tags