Immutable URI reference. A URI reference includes a URI and a fragment, the component of the URI following a '#'. Builds and parses URI references which conform to RFC 2396 . In the interest of performance, this class performs little to no valid
| 43 | * rather than throw an exception unless otherwise specified. |
| 44 | */ |
| 45 | public abstract class Uri implements Parcelable, Comparable<Uri> { |
| 46 | /* |
| 47 | This class aims to do as little up front work as possible. To accomplish |
| 48 | that, we vary the implementation depending on what the user passes in. |
| 49 | For example, we have one implementation if the user passes in a |
| 50 | URI string (StringUri) and another if the user passes in the |
| 51 | individual components (OpaqueUri). |
| 52 | *Concurrency notes*: Like any truly immutable object, this class is safe |
| 53 | for concurrent use. This class uses a caching pattern in some places where |
| 54 | it doesn't use volatile or synchronized. This is safe to do with ints |
| 55 | because getting or setting an int is atomic. It's safe to do with a String |
| 56 | because the internal fields are final and the memory model guarantees other |
| 57 | threads won't see a partially initialized instance. We are not guaranteed |
| 58 | that some threads will immediately see changes from other threads on |
| 59 | certain platforms, but we don't mind if those threads reconstruct the |
| 60 | cached result. As a result, we get thread safe caching with no concurrency |
| 61 | overhead, which means the most common case, access from a single thread, |
| 62 | is as fast as possible. |
| 63 | From the Java Language spec.: |
| 64 | "17.5 Final Field Semantics |
| 65 | ... when the object is seen by another thread, that thread will always |
| 66 | see the correctly constructed version of that object's final fields. |
| 67 | It will also see versions of any object or array referenced by |
| 68 | those final fields that are at least as up-to-date as the final fields |
| 69 | are." |
| 70 | In that same vein, all non-transient fields within Uri |
| 71 | implementations should be final and immutable so as to ensure true |
| 72 | immutability for clients even when they don't use proper concurrency |
| 73 | control. |
| 74 | For reference, from RFC 2396: |
| 75 | "4.3. Parsing a URI Reference |
| 76 | A URI reference is typically parsed according to the four main |
| 77 | components and fragment identifier in order to determine what |
| 78 | components are present and whether the reference is relative or |
| 79 | absolute. The individual components are then parsed for their |
| 80 | subparts and, if not opaque, to verify their validity. |
| 81 | Although the BNF defines what is allowed in each component, it is |
| 82 | ambiguous in terms of differentiating between an authority component |
| 83 | and a path component that begins with two slash characters. The |
| 84 | greedy algorithm is used for disambiguation: the left-most matching |
| 85 | rule soaks up as much of the URI reference string as it is capable of |
| 86 | matching. In other words, the authority component wins." |
| 87 | The "four main components" of a hierarchical URI consist of |
| 88 | <scheme>://<authority><path>?<query> |
| 89 | */ |
| 90 | /** Log tag. */ |
| 91 | private static final String LOG = Uri.class.getSimpleName(); |
| 92 | /** |
| 93 | * NOTE: EMPTY accesses this field during its own initialization, so this |
| 94 | * field *must* be initialized first, or else EMPTY will see a null value! |
| 95 | * |
| 96 | * Placeholder for strings which haven't been cached. This enables us |
| 97 | * to cache null. We intentionally create a new String instance so we can |
| 98 | * compare its identity and there is no chance we will confuse it with |
| 99 | * user data. |
| 100 | */ |
| 101 | @SuppressWarnings("RedundantStringConstructorCall") |
| 102 | private static final String NOT_CACHED = new String("NOT CACHED"); |
nothing calls this directly
no outgoing calls
no test coverage detected