|
| 1 | +package com.betfair.cougar.core.api.builder; |
| 2 | + |
| 3 | +import com.betfair.cougar.api.RequestUUID; |
| 4 | +import com.betfair.cougar.api.geolocation.GeoLocationDetails; |
| 5 | + |
| 6 | +import java.util.BitSet; |
| 7 | +import java.util.Date; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/** |
| 11 | + * Base utils for building ExecutionContexts |
| 12 | + */ |
| 13 | +public abstract class BaseExecutionContextBuilder<T extends BaseExecutionContextBuilder> { |
| 14 | + |
| 15 | + private BitSet whatsSet = new BitSet(MAX_BASE_BITS+getNumSpecificComponents()); |
| 16 | + |
| 17 | + protected GeoLocationDetails location; |
| 18 | + protected RequestUUID uuid; |
| 19 | + protected Date receivedTime; |
| 20 | + protected Date requestTime; |
| 21 | + protected boolean traceLoggingEnabled; |
| 22 | + protected int transportSecurityStrengthFactor; |
| 23 | + |
| 24 | + protected abstract int getNumSpecificComponents(); |
| 25 | + protected final static int MAX_BASE_BITS = 6; |
| 26 | + |
| 27 | + public T setLocation(GeoLocationDetails location) { |
| 28 | + this.location = location; |
| 29 | + set(0); |
| 30 | + return (T) this; |
| 31 | + } |
| 32 | + |
| 33 | + public T setRequestUUID(RequestUUID uuid) { |
| 34 | + this.uuid = uuid; |
| 35 | + set(1); |
| 36 | + return (T) this; |
| 37 | + } |
| 38 | + |
| 39 | + public T setReceivedTime(Date receivedTime) { |
| 40 | + this.receivedTime = receivedTime; |
| 41 | + set(2); |
| 42 | + return (T) this; |
| 43 | + } |
| 44 | + |
| 45 | + public T setRequestTime(Date requestTime) { |
| 46 | + this.requestTime = requestTime; |
| 47 | + set(3); |
| 48 | + return (T) this; |
| 49 | + } |
| 50 | + |
| 51 | + public T setTraceLoggingEnabled(boolean traceLoggingEnabled) { |
| 52 | + this.traceLoggingEnabled = traceLoggingEnabled; |
| 53 | + set(4); |
| 54 | + return (T) this; |
| 55 | + } |
| 56 | + |
| 57 | + public T setTransportSecurityStrengthFactor(int factor) { |
| 58 | + transportSecurityStrengthFactor = factor; |
| 59 | + set(5); |
| 60 | + return (T) this; |
| 61 | + } |
| 62 | + |
| 63 | + protected void beenSet(int subComponent) { |
| 64 | + set(MAX_BASE_BITS + subComponent); |
| 65 | + } |
| 66 | + |
| 67 | + private void set(int bit) { |
| 68 | + if (whatsSet.get(bit)) { |
| 69 | + throw new IllegalStateException("Component has already been set"); |
| 70 | + } |
| 71 | + whatsSet.set(bit); |
| 72 | + } |
| 73 | + |
| 74 | + protected void checkReady() { |
| 75 | + if (whatsSet.nextClearBit(0) <= getNumSpecificComponents()) { |
| 76 | + throw new IllegalStateException("Not all components have been set on this execution context"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public Date getRequestTime() { |
| 81 | + return requestTime; |
| 82 | + } |
| 83 | + |
| 84 | + public Object getRequestUUID() { |
| 85 | + return uuid; |
| 86 | + } |
| 87 | +} |
0 commit comments