Commit 7e455073 authored by Jonathan Mace's avatar Jonathan Mace

Add Fonseca example

parent 7daa7454
......@@ -62,4 +62,8 @@ bag ExampleBag {
map<string, counter> countermap = 35;
}
bag FonsecaBag {
map<string, set<string>> publications_by_conference = 77;
}
\ No newline at end of file
/** Generated by BaggageBuffersCompiler */
package brown.tracingplane.bdl.examples;
import brown.tracingplane.ActiveBaggage;
import brown.tracingplane.BaggageContext;
import brown.tracingplane.baggageprotocol.BagKey;
import brown.tracingplane.baggageprotocol.BaggageReader;
import brown.tracingplane.baggageprotocol.BaggageWriter;
import brown.tracingplane.bdl.BDLUtils;
import brown.tracingplane.bdl.Bag;
import brown.tracingplane.bdl.BaggageHandler;
import brown.tracingplane.bdl.Brancher;
import brown.tracingplane.bdl.Branchers;
import brown.tracingplane.bdl.Joiner;
import brown.tracingplane.bdl.Joiners;
import brown.tracingplane.bdl.Parser;
import brown.tracingplane.bdl.Parsers;
import brown.tracingplane.bdl.ReaderHelpers;
import brown.tracingplane.bdl.Serializer;
import brown.tracingplane.bdl.Serializers;
import brown.tracingplane.bdl.WriterHelpers;
import brown.tracingplane.impl.BDLContextProvider;
import brown.tracingplane.impl.BaggageHandlerRegistry;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FonsecaBag implements Bag {
private static final Logger _log = LoggerFactory.getLogger(FonsecaBag.class);
public Map<String, Set<String>> publicationsByConference = null;
public boolean _overflow = false;
/**
* <p>
* Get the {@link FonsecaBag} set in the active {@link BaggageContext} carried by the current thread. If no baggage is being
* carried by the current thread, or if there is no FonsecaBag in it, then this method returns {@code null}.
* </p>
*
* <p>
* To get FonsecaBag from a specific Baggage instance, use {@link #getFrom(BaggageContext)}.
* </p>
*
* @return the FonsecaBag being carried in the {@link BaggageContext} of the current thread, or {@code null}
* if none is being carried. The returned instance maybe be modified and modifications will be reflected in
* the baggage.
*/
public static FonsecaBag get() {
Bag bag = BDLContextProvider.get(ActiveBaggage.peek(), Handler.registration());
if (bag instanceof FonsecaBag) {
return (FonsecaBag) bag;
} else {
return null;
}
}
/**
* <p>
* Get the {@link FonsecaBag} set in {@code baggage}. If {@code baggage} has no FonsecaBag set then
* this method returns null.
* </p>
*
* <p>
* This method does <b>not</b> affect the Baggage being carried by the current thread. To get FonsecaBag
* from the current thread's Baggage, use {@link #get()}.
* </p>
*
* @param baggage A baggage instance to get the {@link FonsecaBag} from
* @return the {@link FonsecaBag} instance being carried in {@code baggage}, or {@code null} if none is being carried.
* The returned instance can be modified, and modifications will be reflected in the baggage.
*/
public static FonsecaBag getFrom(BaggageContext baggage) {
Bag bag = BDLContextProvider.get(baggage, Handler.registration());
if (bag instanceof FonsecaBag) {
return (FonsecaBag) bag;
} else if (bag != null) {
Handler.checkRegistration();
}
return null;
}
/**
* <p>
* Update the {@link FonsecaBag} set in the current thread's baggage. This method will overwrite any existing
* FonsecaBag set in the current thread's baggage.
* </p>
*
* <p>
* To set the {@link FonsecaBag} in a specific {@link BaggageContext} instance, use
* {@link #setIn(BaggageContext, FonsecaBag)}
* </p>
*
* @param fonsecaBag the new {@link FonsecaBag} to set in the current thread's {@link BaggageContext}. If {@code null}
* then any existing mappings will be removed.
*/
public static void set(FonsecaBag fonsecaBag) {
ActiveBaggage.update(BDLContextProvider.set(ActiveBaggage.peek(), Handler.registration(), fonsecaBag));
}
/**
* <p>
* Update the {@link FonsecaBag} set in {@code baggage}. This method will overwrite any existing
* FonsecaBag set in {@code baggage}.
* </p>
*
* <p>
* This method does <b>not</b> affect the {@link BaggageContext} being carried by the current thread. To set the
* {@link FonsecaBag} for the current thread, use {@link #set(FonsecaBag)}
* </p>
*
* @param baggage A baggage instance to set the {@link FonsecaBag} in
* @param fonsecaBag the new FonsecaBag to set in {@code baggage}. If {@code null}, it will remove any
* mapping present.
* @return a possibly new {@link BaggageContext} instance that contains all previous mappings plus the new mapping.
*/
public static BaggageContext setIn(BaggageContext baggage, FonsecaBag fonsecaBag) {
return BDLContextProvider.set(baggage, Handler.registration(), fonsecaBag);
}
@Override
public BaggageHandler<?> handler() {
return Handler.instance;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("FonsecaBag{\n");
b.append(this.publicationsByConference == null ? "" : BDLUtils.indent(String.format("publicationsByConference = %s\n", BDLUtils.toString(this.publicationsByConference, _v0 -> BDLUtils.toString(_v0)))));
b.append("}");
return b.toString();
}
public static class Handler implements BaggageHandler<FonsecaBag> {
public static final Handler instance = new Handler();
private static BagKey registration = null;
static synchronized BagKey checkRegistration() {
registration = BaggageHandlerRegistry.get(instance);
if (registration == null) {
_log.error("FonsecaBag MUST be registered to a key before it can be propagated. " +
"There is currently no registration for FonsecaBag and it will not be propagated. " +
"To register a bag set the bag.{index} property in your application.conf (eg, for " +
"index 10, bag.10 = \"brown.tracingplane.bdl.examples.FonsecaBag\") or with -Dbag.{index} flag " +
"(eg, for index 10, -Dbag.10=brown.tracingplane.bdl.examples.FonsecaBag)");
}
return registration;
}
static BagKey registration() {
return registration == null ? checkRegistration() : registration;
}
private Handler(){}
private static final BagKey _publicationsByConferenceKey = BagKey.indexed(77);
private static final Parser<Map<String, Set<String>>> _publicationsByConferenceParser = Parsers.<String,Set<String>>mapParser(ReaderHelpers.to_string, Parsers.<String>setParser(Parsers.stringParser()));
private static final Serializer<Map<String, Set<String>>> _publicationsByConferenceSerializer = Serializers.<String,Set<String>>mapSerializer(WriterHelpers.from_string, Serializers.<String>setSerializer(Serializers.stringSerializer()));
private static final Brancher<Map<String, Set<String>>> _publicationsByConferenceBrancher = Branchers.<String, Set<String>>map(Branchers.<String>set());
private static final Joiner<Map<String, Set<String>>> _publicationsByConferenceJoiner = Joiners.<String, Set<String>>mapMerge(Joiners.<String>setUnion());
@Override
public boolean isInstance(Bag bag) {
return bag == null || bag instanceof FonsecaBag;
}
@Override
public FonsecaBag parse(BaggageReader reader) {
FonsecaBag instance = new FonsecaBag();
if (reader.enter(_publicationsByConferenceKey)) {
instance.publicationsByConference = _publicationsByConferenceParser.parse(reader);
reader.exit();
}
instance._overflow = reader.didOverflow();
return instance;
}
@Override
public void serialize(BaggageWriter writer, FonsecaBag instance) {
if (instance == null) {
return;
}
writer.didOverflowHere(instance._overflow);
if (instance.publicationsByConference != null) {
writer.enter(_publicationsByConferenceKey);
_publicationsByConferenceSerializer.serialize(writer, instance.publicationsByConference);
writer.exit();
}
}
@Override
public FonsecaBag branch(FonsecaBag instance) {
if (instance == null) {
return null;
}
FonsecaBag newInstance = new FonsecaBag();
newInstance.publicationsByConference = _publicationsByConferenceBrancher.branch(instance.publicationsByConference);
return newInstance;
}
@Override
public FonsecaBag join(FonsecaBag left, FonsecaBag right) {
if (left == null) {
return right;
} else if (right == null) {
return left;
} else {
left.publicationsByConference = _publicationsByConferenceJoiner.join(left.publicationsByConference, right.publicationsByConference);
return left;
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment