View Javadoc
1   /*
2    * Copyright (C) 2005-2015 Schlichtherle IT Services.
3    * All rights reserved. Use is subject to license terms.
4    */
5   package net.java.truevfs.kernel.spec;
6   
7   import java.beans.*;
8   import java.io.Serializable;
9   import java.net.URISyntaxException;
10  import java.util.Locale;
11  import javax.annotation.concurrent.Immutable;
12  import net.java.truecommons.shed.UriBuilder;
13  
14  /**
15   * Addresses a file system scheme.
16   * This is simply a {@link java.net.URI} scheme according to the syntax
17   * constraints defined in
18   * <a href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform Resource Identifiers (URI): Generic Syntax</i></a>.
19   *
20   * <h3><a name="serialization">Serialization</a></h3>
21   * <p>
22   * This class supports serialization with both
23   * {@link java.io.ObjectOutputStream} and {@link java.beans.XMLEncoder}.
24   *
25   * @see    FsNodePath
26   * @see    FsMountPoint
27   * @see    FsNodeName
28   * @author Christian Schlichtherle
29   */
30  @Immutable
31  public final class FsScheme implements Serializable, Comparable<FsScheme> {
32  
33      private static final long serialVersionUID = 2765230379628276648L;
34  
35      private final String scheme;
36  
37      /**
38       * Constructs a new URI scheme by parsing the given string.
39       * This static factory method calls
40       * {@link #FsScheme(String) new FsScheme(scheme)}
41       * and wraps any thrown {@link URISyntaxException} in an
42       * {@link IllegalArgumentException}.
43       *
44       * @param  scheme the URI scheme.
45       * @throws IllegalArgumentException if {@code scheme} does not conform to
46       *         the syntax constraints for URI schemes.
47       * @return A new scheme.
48       */
49      public static FsScheme create(String scheme) {
50          try {
51              return new FsScheme(scheme);
52          } catch (URISyntaxException ex) {
53              throw new IllegalArgumentException(ex);
54          }
55      }
56  
57      /**
58       * Constructs a new URI scheme by parsing the given string.
59       *
60       * @param  scheme the URI scheme.
61       * @throws URISyntaxException if {@code scheme} does not conform to the
62       *         syntax constraints for URI schemes.
63       */
64      @ConstructorProperties("scheme")
65      public FsScheme(final String scheme) throws URISyntaxException {
66          UriBuilder.validateScheme(scheme);
67          this.scheme = scheme;
68      }
69  
70      /**
71       * Returns the scheme as a string.
72       *
73       * @return The scheme as a string.
74       * @since  TrueVFS 0.10
75       * @deprecated This method is solely provided to support
76       *             {@link XMLEncoder}/{@link XMLDecoder}.
77       *             Applications should call {@link #toString()} instead.
78       */
79      @Deprecated
80      public String getScheme() { return scheme; }
81  
82      @Override
83      public boolean equals(Object that) {
84          return this == that
85                  || that instanceof FsScheme
86                      && this.scheme.equalsIgnoreCase(((FsScheme) that).scheme);
87      }
88  
89      @Override
90      public int compareTo(FsScheme that) {
91          return this.scheme.compareToIgnoreCase(that.scheme);
92      }
93  
94      @Override
95      public int hashCode() {
96          return scheme.toLowerCase(Locale.ROOT).hashCode();
97      }
98  
99      /**
100      * Returns the scheme as a string.
101      *
102      * @return The scheme as a string.
103      */
104     @Override
105     public String toString() { return scheme; }
106 }