@Override @SuppressWarnings("unchecked") public Builder merge(CUnion from) { if (unionField == null || unionField != from.unionField()) { set(from.unionField.getKey(), from.get(from.unionField.getKey())); } else { int key = unionField.getKey(); switch (unionField.getType()) { case MESSAGE: { PMessage src = (PMessage) currentValue; PMessage toMerge = (PMessage) from.get(key); currentValue = src.mutate().merge(toMerge).build(); break; } case SET: ((PSet.Builder<Object>) currentValue).addAll((Set<Object>) from.get(key)); break; case MAP: ((PMap.Builder<Object, Object>) currentValue) .putAll((Map<Object, Object>) from.get(key)); break; default: set(key, from.get(key)); break; } } return this; }
@Override public Builder clear(int key) { if (unionField != null && unionField.getKey() == key) { this.unionField = null; this.currentValue = null; } return this; }
private Map<Integer, Object> getValueMap() { if (unionField == null) { return ImmutableMap.of(); } else if (currentValue == null) { throw new IllegalStateException("Union field set, but value is null."); } else { switch (unionField.getType()) { case LIST: return ImmutableMap.of( unionField.getKey(), ((PList.Builder) this.currentValue).build()); case SET: return ImmutableMap.of(unionField.getKey(), ((PSet.Builder) this.currentValue).build()); case MAP: return ImmutableMap.of(unionField.getKey(), ((PMap.Builder) this.currentValue).build()); case MESSAGE: if (currentValue instanceof PMessageBuilder) { return ImmutableMap.of(unionField.getKey(), ((PMessageBuilder) currentValue).build()); } default: return ImmutableMap.of(unionField.getKey(), this.currentValue); } } }