public NestedBaggageContext branch() { NestedBaggageContext copy = new NestedBaggageContext(); copy.didOverflow = didOverflow; copy.dataAtoms.addAll(dataAtoms); for (BagKey childKey : children.keySet()) { copy.children.put(childKey, children.get(childKey).branch()); } return copy; }
@Override public NestedBaggageContext join(NestedBaggageContext left, NestedBaggageContext right) { if (left == null) { return right; } else { left.mergeWith(right); return left; } }
static NestedBaggageContext parse(BaggageReader reader) { if (!reader.hasNext()) { return null; } NestedBaggageContext bag = new NestedBaggageContext(); while (reader.hasData()) { bag.dataAtoms.add(reader.nextData()); } if (reader.didOverflow()) { bag.didOverflow = true; } while (reader.hasChild()) { BagKey childKey = reader.enter(); NestedBaggageContext childBag = parse(reader); if (childBag != null) { bag.children.put(childKey, childBag); } reader.exit(); } return bag; }
void serialize(BaggageWriter writer) { writer.didOverflowHere(didOverflow); for (ByteBuffer atom : dataAtoms) { ByteBuffer newAtom = writer.newDataAtom(atom.remaining()); ByteBuffers.copyTo(atom, newAtom); } for (BagKey childKey : children.keySet()) { writer.enter(childKey); children.get(childKey).serialize(writer); writer.exit(); } }
@Override public NestedBaggageContext deserialize(byte[] serialized, int offset, int length) { List<ByteBuffer> atoms = AtomLayerSerialization.deserialize(serialized, offset, length); return NestedBaggageContext.parse(BaggageReader.create(atoms)); }
@Override public NestedBaggageContext branch(NestedBaggageContext from) { return from == null ? null : from.branch(); }
@Override public byte[] serialize(NestedBaggageContext baggage, int maximumSerializedSize) { if (baggage == null) { return null; } BaggageWriter writer = BaggageWriter.create(); baggage.serialize(writer); List<ByteBuffer> atoms = writer.atoms(); return AtomLayerSerialization.serialize(atoms, maximumSerializedSize); }
@Override public NestedBaggageContext deserialize(ByteBuffer buf) { List<ByteBuffer> atoms = AtomLayerSerialization.deserialize(buf); return NestedBaggageContext.parse(BaggageReader.create(atoms)); }
@Override public byte[] serialize(NestedBaggageContext baggage) { if (baggage == null) { return null; } BaggageWriter writer = BaggageWriter.create(); baggage.serialize(writer); List<ByteBuffer> atoms = writer.atoms(); return AtomLayerSerialization.serialize(atoms); }
public void mergeWith(NestedBaggageContext right) { if (right == null) { return; } this.didOverflow |= right.didOverflow; if (this.dataAtoms == null) { this.dataAtoms = right.dataAtoms; } else if (right.dataAtoms != null) { this.dataAtoms = Lexicographic.merge(this.dataAtoms, right.dataAtoms); } if (this.children == null) { this.children = right.children; } else if (right.children != null) { for (BagKey childKey : right.children.keySet()) { NestedBaggageContext leftChild = this.children.get(childKey); NestedBaggageContext rightChild = right.children.get(childKey); if (leftChild != null) { leftChild.mergeWith(rightChild); } else { this.children.put(childKey, rightChild); } } } }