|
| 1 | +package com.abilityapi.sequenceapi.origin; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +public class Origin { |
| 9 | + |
| 10 | + public static OriginKey SOURCE = new OriginKey("source", null); |
| 11 | + |
| 12 | + public static OriginKey OWNER = new OriginKey("owner", null); |
| 13 | + |
| 14 | + public static OriginKey STATE = new OriginKey("state", null); |
| 15 | + |
| 16 | + public static Builder builder() { |
| 17 | + return new Builder(); |
| 18 | + } |
| 19 | + |
| 20 | + // This should be immutable. |
| 21 | + private Map<OriginKey, Object> origins = new HashMap<>(); |
| 22 | + |
| 23 | + private Origin(Map<OriginKey, Object> origins) { |
| 24 | + this.origins = origins; |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + public static class Builder { |
| 29 | + |
| 30 | + Map<OriginKey, Object> origins = new HashMap<>(); |
| 31 | + Set<String> keysUsed = new HashSet<>(); |
| 32 | + |
| 33 | + Builder() {} |
| 34 | + |
| 35 | + Builder source(Object value) { |
| 36 | + if (!this.keysUsed.contains(Origin.SOURCE.getId())) { |
| 37 | + this.origins.put(Origin.SOURCE, value); |
| 38 | + this.keysUsed.add(Origin.SOURCE.getId()); |
| 39 | + } |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + Builder owner(Object value) { |
| 44 | + if (!this.keysUsed.contains(Origin.OWNER.getId())) { |
| 45 | + this.origins.put(Origin.OWNER, value); |
| 46 | + this.keysUsed.add(Origin.OWNER.getId()); |
| 47 | + } |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + Builder state(Object value) { |
| 52 | + if (!this.keysUsed.contains(Origin.STATE.getId())) { |
| 53 | + this.origins.put(Origin.STATE, value); |
| 54 | + this.keysUsed.add(Origin.STATE.getId()); |
| 55 | + } |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + Builder custom(OriginKey key, Object value) { |
| 60 | + if (!this.keysUsed.contains(key.getId())) { |
| 61 | + this.origins.put(key, value); |
| 62 | + this.keysUsed.add(key.getId()); |
| 63 | + } |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + Origin build() { |
| 68 | + return new Origin(this.origins); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments