|
| 1 | +package io.ipfs.api; |
| 2 | + |
| 3 | +import java.net.URLEncoder; |
| 4 | +import java.util.*; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +/* |
| 8 | +Example usage: |
| 9 | + AddArgs args = AddArgs.Builder.newInstance() |
| 10 | + .setInline() |
| 11 | + .setCidVersion(1) |
| 12 | + .build(); |
| 13 | + */ |
| 14 | +final class AddArgs { |
| 15 | + |
| 16 | + private final Map<String, String> args = new HashMap<>(); |
| 17 | + |
| 18 | + public AddArgs(Builder builder) |
| 19 | + { |
| 20 | + args.putAll(builder.args); |
| 21 | + } |
| 22 | + @Override |
| 23 | + public String toString() |
| 24 | + { |
| 25 | + List<String> asList = args.entrySet() |
| 26 | + .stream() |
| 27 | + .sorted(Comparator.comparing(Map.Entry::getKey)) |
| 28 | + .map(e -> e.getKey() + " = " + e.getValue()).collect(Collectors.toList()); |
| 29 | + return Arrays.toString(asList.toArray()); |
| 30 | + } |
| 31 | + public String toQueryString() |
| 32 | + { |
| 33 | + StringBuilder sb = new StringBuilder(); |
| 34 | + for (Map.Entry<String, String> entry: args.entrySet()) { |
| 35 | + sb.append("&").append(entry.getKey()) |
| 36 | + .append("=") |
| 37 | + .append(URLEncoder.encode(entry.getValue())); |
| 38 | + } |
| 39 | + return sb.length() > 0 ? sb.toString().substring(1) : sb.toString(); |
| 40 | + } |
| 41 | + public static class Builder { |
| 42 | + private static final String TRUE = "true"; |
| 43 | + private final Map<String, String> args = new HashMap<>(); |
| 44 | + private Builder() {} |
| 45 | + public static Builder newInstance() |
| 46 | + { |
| 47 | + return new Builder(); |
| 48 | + } |
| 49 | + public Builder setQuiet() { |
| 50 | + args.put("quiet", TRUE); |
| 51 | + return this; |
| 52 | + } |
| 53 | + public Builder setQuieter() { |
| 54 | + args.put("quieter", TRUE); |
| 55 | + return this; |
| 56 | + } |
| 57 | + public Builder setSilent() { |
| 58 | + args.put("silent", TRUE); |
| 59 | + return this; |
| 60 | + } |
| 61 | + public Builder setTrickle() { |
| 62 | + args.put("trickle", TRUE); |
| 63 | + return this; |
| 64 | + } |
| 65 | + public Builder setOnlyHash() { |
| 66 | + args.put("only-hash", TRUE); |
| 67 | + return this; |
| 68 | + } |
| 69 | + public Builder setWrapWithDirectory() { |
| 70 | + args.put("wrap-with-directory", TRUE); |
| 71 | + return this; |
| 72 | + } |
| 73 | + public Builder setChunker(String chunker) { |
| 74 | + args.put("chunker", chunker); |
| 75 | + return this; |
| 76 | + } |
| 77 | + public Builder setRawLeaves() { |
| 78 | + args.put("raw-leaves", TRUE); |
| 79 | + return this; |
| 80 | + } |
| 81 | + public Builder setNocopy() { |
| 82 | + args.put("nocopy", TRUE); |
| 83 | + return this; |
| 84 | + } |
| 85 | + public Builder setFscache() { |
| 86 | + args.put("fscache", TRUE); |
| 87 | + return this; |
| 88 | + } |
| 89 | + public Builder setCidVersion(int version) { |
| 90 | + args.put("cid-version", String.valueOf(version)); |
| 91 | + return this; |
| 92 | + } |
| 93 | + public Builder setHash(String hashFunction) { |
| 94 | + args.put("hash", hashFunction); |
| 95 | + return this; |
| 96 | + } |
| 97 | + public Builder setInline() { |
| 98 | + args.put("inline", TRUE); |
| 99 | + return this; |
| 100 | + } |
| 101 | + public Builder setInlineLimit(int maxBlockSize) { |
| 102 | + args.put("inline-limit", String.valueOf(maxBlockSize)); |
| 103 | + return this; |
| 104 | + } |
| 105 | + public Builder setPin() { |
| 106 | + args.put("pin", TRUE); |
| 107 | + return this; |
| 108 | + } |
| 109 | + public Builder setToFiles(String path) { |
| 110 | + args.put("to-files", path); |
| 111 | + return this; |
| 112 | + } |
| 113 | + public AddArgs build() |
| 114 | + { |
| 115 | + return new AddArgs(this); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments