/
RevObjects.java
97 lines (83 loc) · 3.23 KB
/
RevObjects.java
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
/* Copyright (c) 2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.model;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Comparator;
import java.util.Iterator;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
/**
* A set of utility methods to work with revision objects
*
*
* @since 1.0
*/
public class RevObjects {
/**
* An identifier for a null coordinate reference system.
*/
public static final String NULL_CRS_IDENTIFIER = "urn:ogc:def:crs:EPSG::0";
private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
/**
* Creates a hexadecimal encoding representation of the first {@code numBytes} bytes of the
* SHA-1 hashcode represented by {@code id} and appends it to {@code target}.
*
* @pre {@code 0 < numBytes <= 20 }
* @param id the {@code ObjectId} representing the SHA-1 hash to encode as an hex string
* @param numBytes
* @param target the {@code StringBuilder} where to append the string representation of the
* {@link ObjectId}
* @return {@code target}
*/
public static StringBuilder toString(final ObjectId id, final int numBytes,
StringBuilder target) {
Preconditions.checkNotNull(id);
Preconditions.checkArgument(numBytes > 0 && numBytes <= ObjectId.NUM_BYTES);
StringBuilder sb = target == null ? new StringBuilder(2 * numBytes) : target;
int b;
for (int i = 0; i < numBytes; i++) {
b = id.byteN(i);
sb.append(HEX_DIGITS[(b >> 4) & 0xf]).append(HEX_DIGITS[b & 0xf]);
}
return sb;
}
/**
* Creates and returns an iterator of the joint lists of {@link RevTree#trees() trees} and
* {@link RevTree#features() features} of the given {@code RevTree} whose iteration order is
* given by the provided {@code comparator}.
*
* @return an iterator over the <b>direct</b> {@link RevTree#trees() trees} and
* {@link RevTree#features() feature} children collections, in the order mandated by the
* provided {@code comparator}
*/
public static Iterator<Node> children(RevTree tree, Comparator<Node> comparator) {
checkNotNull(comparator);
ImmutableList<Node> trees = tree.trees();
ImmutableList<Node> features = tree.features();
if (trees.isEmpty()) {
return features.iterator();
}
if (features.isEmpty()) {
return trees.iterator();
}
return Iterators.mergeSorted(ImmutableList.of(trees.iterator(), features.iterator()),
comparator);
}
public static int h1(ObjectId id) {
return id.h1;
}
public static long h2(ObjectId id) {
return id.h2;
}
public static long h3(ObjectId id) {
return id.h3;
}
}