Skip to content

Commit 5666d3d

Browse files
committed
Update min version to 0.4.10, and make the version parser more general.
1 parent c233640 commit 5666d3d

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class IPFS {
1313

14-
public static final String MIN_VERSION = "0.4.5";
14+
public static final String MIN_VERSION = "0.4.10";
1515
public enum PinType {all, direct, indirect, recursive}
1616
public List<String> ObjectTemplates = Arrays.asList("unixfs-dir");
1717
public List<String> ObjectPatchTypes = Arrays.asList("add-link", "rm-link", "set-data", "append-data");
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.ipfs.api;
2+
3+
public class Version implements Comparable<Version> {
4+
5+
public final int major, minor, patch;
6+
public final String suffix;
7+
8+
public Version(int major, int minor, int patch, String suffix) {
9+
this.major = major;
10+
this.minor = minor;
11+
this.patch = patch;
12+
this.suffix = suffix;
13+
}
14+
15+
public String toString() {
16+
return major + "." + minor + "." + patch + (suffix.length() > 0 ? "-" + suffix : "");
17+
}
18+
19+
public boolean isBefore(Version other) {
20+
return this.compareTo(other) < 0;
21+
}
22+
23+
@Override
24+
public int compareTo(Version other) {
25+
int major = Integer.compare(this.major, other.major);
26+
if (major != 0)
27+
return major;
28+
int minor = Integer.compare(this.minor, other.minor);
29+
if (minor != 0)
30+
return minor;
31+
int patch = Integer.compare(this.patch, other.patch);
32+
if (patch != 0)
33+
return patch;
34+
if (suffix.length() == 0)
35+
return 1;
36+
if (other.suffix.length() == 0)
37+
return -1;
38+
return suffix.compareTo(other.suffix);
39+
}
40+
41+
public static Version parse(String version) {
42+
int first = version.indexOf(".");
43+
int second = version.indexOf(".", first + 1);
44+
int third = version.contains("-") ? version.indexOf("-") : version.length();
45+
46+
int major = Integer.parseInt(version.substring(0, first));
47+
int minor = Integer.parseInt(version.substring(first + 1, second));
48+
int patch = Integer.parseInt(version.substring(second + 1, third));
49+
String suffix = third < version.length() ? version.substring(third + 1) : "";
50+
return new Version(major, minor, patch, suffix);
51+
}
52+
}

src/test/java/io/ipfs/api/APITest.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -237,27 +237,23 @@ public void rawLeafNodePinUpdate() {
237237
try {
238238
MerkleNode child1 = ipfs.block.put("some data".getBytes(), Optional.of("raw"));
239239
Multihash hashChild1 = child1.hash;
240-
System.out.println("child1: " + hashChild1);
240+
System.out.println("child1: " + hashChild1.type);
241241

242242
CborObject.CborMerkleLink root1 = new CborObject.CborMerkleLink(hashChild1);
243243
MerkleNode root1Res = ipfs.block.put(Collections.singletonList(root1.toByteArray()), Optional.of("cbor")).get(0);
244244
System.out.println("root1: " + root1Res.hash);
245245
ipfs.pin.add(root1Res.hash);
246246

247-
CborObject.CborList root2 = new CborObject.CborList(Arrays.asList(new CborObject.CborMerkleLink(hashChild1), new CborObject.CborLong(42)));
248-
MerkleNode root2Res = ipfs.block.put(Collections.singletonList(root2.toByteArray()), Optional.of("cbor")).get(0);
249-
List<MultiAddress> update = ipfs.pin.update(root1Res.hash, root2Res.hash, true);
247+
MerkleNode child2 = ipfs.block.put("G'day new tree".getBytes(), Optional.of("raw"));
248+
Multihash hashChild2 = child2.hash;
250249

251-
Map<Multihash, Object> ls = ipfs.pin.ls(IPFS.PinType.all);
252-
boolean childPresent = ls.containsKey(hashChild1);
253-
if (!childPresent)
254-
throw new IllegalStateException("Child not present!");
255-
256-
ipfs.repo.gc();
257-
Map<Multihash, Object> ls2 = ipfs.pin.ls(IPFS.PinType.all);
258-
boolean childPresentAfterGC = ls2.containsKey(hashChild1);
259-
if (!childPresentAfterGC)
260-
throw new IllegalStateException("Child not present!");
250+
CborObject.CborList root2 = new CborObject.CborList(Arrays.asList(
251+
new CborObject.CborMerkleLink(hashChild1),
252+
new CborObject.CborMerkleLink(hashChild2),
253+
new CborObject.CborLong(42))
254+
);
255+
MerkleNode root2Res = ipfs.block.put(Collections.singletonList(root2.toByteArray()), Optional.of("cbor")).get(0);
256+
List<MultiAddress> update = ipfs.pin.update(root1Res.hash, root2Res.hash, false);
261257
} catch (IOException e) {
262258
throw new RuntimeException(e);
263259
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.ipfs.api;
2+
3+
import org.junit.*;
4+
5+
import java.util.*;
6+
import java.util.stream.*;
7+
8+
public class Versions {
9+
10+
@Test
11+
public void sorting(){
12+
List<String> original = Arrays.asList("1.0.3", "0.4.9", "0.4.10", "0.5.1-rc1", "0.5.1-rc2", "0.5.1-rc2+meta");
13+
List<Version> versions = original.stream().map(Version::parse).collect(Collectors.toList());
14+
Collections.sort(versions);
15+
List<String> sorted = versions.stream().map(Object::toString).collect(Collectors.toList());
16+
List<String> correct = Arrays.asList("0.4.9", "0.4.10", "0.5.1-rc1", "0.5.1-rc2", "0.5.1-rc2+meta", "1.0.3");
17+
Assert.assertTrue("Correct version sorting", sorted.equals(correct));
18+
}
19+
}

0 commit comments

Comments
 (0)