1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.dfs;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jgit.annotations.Nullable;
18 import org.eclipse.jgit.internal.storage.pack.PackExt;
19
20
21
22
23 public abstract class DfsStreamKey {
24
25
26
27
28
29
30
31
32
33
34
35 public static DfsStreamKey of(DfsRepositoryDescription repo, String name,
36 @Nullable PackExt ext) {
37 return new ByteArrayDfsStreamKey(repo, name.getBytes(UTF_8), ext);
38 }
39
40 final int hash;
41
42 final int packExtPos;
43
44
45
46
47
48
49
50
51
52 protected DfsStreamKey(int hash, @Nullable PackExt ext) {
53
54
55 this.hash = hash * 31;
56 this.packExtPos = ext == null ? 0 : ext.getPosition();
57 }
58
59
60 @Override
61 public int hashCode() {
62 return hash;
63 }
64
65
66 @Override
67 public abstract boolean equals(Object o);
68
69
70 @SuppressWarnings("boxing")
71 @Override
72 public String toString() {
73 return String.format("DfsStreamKey[hash=%08x]", hash);
74 }
75
76 private static final class ByteArrayDfsStreamKey extends DfsStreamKey {
77 private final DfsRepositoryDescription repo;
78
79 private final byte[] name;
80
81 ByteArrayDfsStreamKey(DfsRepositoryDescription repo, byte[] name,
82 @Nullable PackExt ext) {
83 super(repo.hashCode() * 31 + Arrays.hashCode(name), ext);
84 this.repo = repo;
85 this.name = name;
86 }
87
88 @Override
89 public boolean equals(Object o) {
90 if (o instanceof ByteArrayDfsStreamKey) {
91 ByteArrayDfsStreamKey k = (ByteArrayDfsStreamKey) o;
92 return hash == k.hash && repo.equals(k.repo)
93 && Arrays.equals(name, k.name);
94 }
95 return false;
96 }
97 }
98
99 static final class ForReverseIndex extends DfsStreamKey {
100 private final DfsStreamKey idxKey;
101
102 ForReverseIndex(DfsStreamKey idxKey) {
103 super(idxKey.hash + 1, null);
104 this.idxKey = idxKey;
105 }
106
107 @Override
108 public boolean equals(Object o) {
109 return o instanceof ForReverseIndex
110 && idxKey.equals(((ForReverseIndex) o).idxKey);
111 }
112 }
113 }