aboutsummaryrefslogtreecommitdiff
path: root/jimfs/src/main/java/com/google/common/jimfs/Name.java
blob: 327be751d47fc7d047cf24609d57e003dca252b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.jimfs;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.Ordering;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
 * Immutable representation of a file name. Used both for the name components of paths and as the
 * keys for directory entries.
 *
 * <p>A name has both a display string (used in the {@code toString()} form of a {@code Path} as
 * well as for {@code Path} equality and sort ordering) and a canonical string, which is used for
 * determining equality of the name during file lookup.
 *
 * <p>Note: all factory methods return a constant name instance when given the original string "."
 * or "..", ensuring that those names can be accessed statically elsewhere in the code while still
 * being equal to any names created for those values, regardless of normalization settings.
 *
 * @author Colin Decker
 */
final class Name {

  /** The empty name. */
  static final Name EMPTY = new Name("", "");

  /** The name to use for a link from a directory to itself. */
  public static final Name SELF = new Name(".", ".");

  /** The name to use for a link from a directory to its parent directory. */
  public static final Name PARENT = new Name("..", "..");

  /** Creates a new name with no normalization done on the given string. */
  @VisibleForTesting
  static Name simple(String name) {
    switch (name) {
      case ".":
        return SELF;
      case "..":
        return PARENT;
      default:
        return new Name(name, name);
    }
  }

  /**
   * Creates a name with the given display representation and the given canonical representation.
   */
  public static Name create(String display, String canonical) {
    return new Name(display, canonical);
  }

  private final String display;
  private final String canonical;

  private Name(String display, String canonical) {
    this.display = checkNotNull(display);
    this.canonical = checkNotNull(canonical);
  }

  @Override
  public boolean equals(@NullableDecl Object obj) {
    if (obj instanceof Name) {
      Name other = (Name) obj;
      return canonical.equals(other.canonical);
    }
    return false;
  }

  @Override
  public int hashCode() {
    return Util.smearHash(canonical.hashCode());
  }

  @Override
  public String toString() {
    return display;
  }

  /** Returns an ordering that orders names by their display representation. */
  public static Ordering<Name> displayOrdering() {
    return DISPLAY_ORDERING;
  }

  /** Returns an ordering that orders names by their canonical representation. */
  public static Ordering<Name> canonicalOrdering() {
    return CANONICAL_ORDERING;
  }

  private static final Ordering<Name> DISPLAY_ORDERING =
      Ordering.natural()
          .onResultOf(
              new Function<Name, String>() {
                @Override
                public String apply(Name name) {
                  return name.display;
                }
              });

  private static final Ordering<Name> CANONICAL_ORDERING =
      Ordering.natural()
          .onResultOf(
              new Function<Name, String>() {
                @Override
                public String apply(Name name) {
                  return name.canonical;
                }
              });
}