View Javadoc

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.vfs;
18  
19  import java.util.Map;
20  import java.util.TreeMap;
21  
22  /***
23   * Container for FileSystemOptions.<br>
24   * You have to use *FileSystemConfigBuilder.getInstance() to fill this container<br>
25   * * = the filesystem provider short name
26   *
27   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
28   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
29   * @see org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder
30   * @see org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder
31   */
32  public class FileSystemOptions
33  {
34      private Map options = new TreeMap();
35  
36      private class FileSystemOptionKey implements Comparable
37      {
38          private final Class fileSystemClass;
39          private final String name;
40  
41          private FileSystemOptionKey(Class fileSystemClass, String name)
42          {
43              this.fileSystemClass = fileSystemClass;
44              this.name = name;
45          }
46  
47          public int compareTo(Object o)
48          {
49              FileSystemOptionKey k = (FileSystemOptionKey) o;
50  
51              int ret = k.fileSystemClass.getName().compareTo(k.fileSystemClass.getName());
52              if (ret != 0)
53              {
54                  return ret;
55              }
56  
57              return name.compareTo(k.name);
58          }
59  
60          public boolean equals(Object o)
61          {
62              if (this == o)
63              {
64                  return true;
65              }
66              if (o == null || getClass() != o.getClass())
67              {
68                  return false;
69              }
70  
71              final FileSystemOptionKey that = (FileSystemOptionKey) o;
72  
73              if (!fileSystemClass.equals(that.fileSystemClass))
74              {
75                  return false;
76              }
77              if (!name.equals(that.name))
78              {
79                  return false;
80              }
81  
82              return true;
83          }
84  
85          public int hashCode()
86          {
87              int result;
88              result = fileSystemClass.hashCode();
89              result = 29 * result + name.hashCode();
90              return result;
91          }
92      }
93  
94      public FileSystemOptions()
95      {
96      }
97  
98      void setOption(Class fileSystemClass, String name, Object value)
99      {
100         options.put(new FileSystemOptionKey(fileSystemClass, name), value);
101     }
102 
103     Object getOption(Class fileSystemClass, String name)
104     {
105         FileSystemOptionKey key = new FileSystemOptionKey(fileSystemClass, name);
106         return options.get(key);
107     }
108 
109     boolean hasOption(Class fileSystemClass, String name)
110     {
111         FileSystemOptionKey key = new FileSystemOptionKey(fileSystemClass, name);
112         return options.containsKey(key);
113     }
114 
115     public int compareTo(FileSystemOptions other)
116     {
117         if (this == other)
118         {
119             // the same instance
120             return 0;
121         }
122 
123         int propsSz = options == null ? 0 : options.size();
124         int propsFkSz = other.options == null ? 0 : other.options.size();
125         if (propsSz < propsFkSz)
126         {
127             return -1;
128         }
129         if (propsSz > propsFkSz)
130         {
131             return 1;
132         }
133         if (propsSz == 0)
134         {
135             // props empty
136             return 0;
137         }
138 
139         int hash = options.hashCode();
140         int hashFk = other.options.hashCode();
141         if (hash < hashFk)
142         {
143             return -1;
144         }
145         if (hash > hashFk)
146         {
147             return 1;
148         }
149 
150         // bad props not the same instance, but looks like the same
151         // TODO: compare Entry by Entry
152         return 0;
153     }
154 }