private static DataType valueTypeForMapUpdate(DataType parentType) { if (parentType instanceof WeightedSetDataType) { return DataType.INT; } else if (parentType instanceof CollectionDataType) { return ((CollectionDataType) parentType).getNestedType(); } else if (parentType instanceof MapDataType) { return ((MapDataType) parentType).getValueType(); } else { throw new UnsupportedOperationException("Unexpected parent type: " + parentType); } }
@Override protected void register(DocumentTypeManager manager, List<DataType> seenTypes) { seenTypes.add(this); if (!seenTypes.contains(getKeyType())) { getKeyType().register(manager, seenTypes); } if (!seenTypes.contains(getValueType())) { getValueType().register(manager, seenTypes); } super.register(manager, seenTypes); }
@Override public FieldPath buildFieldPath(String remainFieldName) { return buildFieldPath(remainFieldName, getKeyType(), getValueType()); }
public StructDataType getFirstStructRecursive() { DataType dataType = getDataType(); while (true) { // Currently no nesting of collections if (dataType instanceof CollectionDataType) { dataType = ((CollectionDataType)dataType).getNestedType(); } else if (dataType instanceof MapDataType) { dataType = ((MapDataType)dataType).getValueType(); } else { break; } } return (dataType instanceof StructDataType) ? (StructDataType)dataType : null; }
public void putAll(java.util.Map<? extends K, ? extends V> m) { for (K key : m.keySet()) { validateCompatibleTypes(getDataType().getKeyType(), key); } for (V value : m.values()) { validateCompatibleTypes(getDataType().getValueType(), value); } values.putAll(m); }
private static boolean isMapOfStructType(DataType type) { if (type instanceof MapDataType) { MapDataType mapType = (MapDataType)type; return ComplexAttributeFieldUtils.isPrimitiveType(mapType.getKeyType()) && isStructType(mapType.getValueType()); } else { return false; } }
public static boolean isMapOfPrimitiveType(DataType type) { if (type instanceof MapDataType) { MapDataType mapType = (MapDataType)type; return ComplexAttributeFieldUtils.isPrimitiveType(mapType.getKeyType()) && ComplexAttributeFieldUtils.isPrimitiveType(mapType.getValueType()); } else { return false; } }
void readKeyAndValue(FieldBase field, KeyAndValue val, MapDataType dt) throws XMLStreamException { while (reader.hasNext()) { int type = reader.next(); if (type == XMLStreamReader.START_ELEMENT) { if ("key".equals(reader.getName().toString())) { val.key = dt.getKeyType().createFieldValue(); deserializeFieldValue(field, val.key); skipToEnd("key"); } else if ("value".equals(reader.getName().toString())) { val.value = dt.getValueType().createFieldValue(); deserializeFieldValue(field, val.value); skipToEnd("value"); } else { throw newDeserializeException("Illegal element inside map item: " + reader.getName()); } } else if (type == XMLStreamReader.END_ELEMENT) { return; } } }
private void replaceTemporaryTypesInMap(MapDataType mapDataType, List<DataType> seenStructs) { if (mapDataType.getValueType() instanceof TemporaryDataType) { mapDataType.setValueType(getDataType(mapDataType.getValueType().getCode(), "")); } else { replaceTemporaryTypes(mapDataType.getValueType(), seenStructs); } if (mapDataType.getKeyType() instanceof TemporaryDataType) { mapDataType.setKeyType(getDataType(mapDataType.getKeyType().getCode(), "")); } else { replaceTemporaryTypes(mapDataType.getKeyType(), seenStructs); } }
public V put(K key, V value) { validateCompatibleTypes(getDataType().getKeyType(), key); validateCompatibleTypes(getDataType().getValueType(), value); return values.put(key, value); }
public static boolean isMapOfPrimitiveType(ImmutableSDField field) { if (field.getDataType() instanceof MapDataType) { MapDataType mapType = (MapDataType)field.getDataType(); return isPrimitiveType(mapType.getKeyType()) && isPrimitiveType(mapType.getValueType()); } else { return false; } }
public static boolean isMapOfSimpleStruct(ImmutableSDField field) { if (field.getDataType() instanceof MapDataType) { MapDataType mapType = (MapDataType)field.getDataType(); return isPrimitiveType(mapType.getKeyType()) && isStructWithPrimitiveStructFieldAttributes(mapType.getValueType(), field.getStructField("value")); } else { return false; } }
public <K extends FieldValue, V extends FieldValue> void read(FieldBase field, MapFieldValue<K, V> map) { int numElements = getNumCollectionElems(); Map<K,V> hash = new HashMap<>(); MapDataType type = map.getDataType(); for (int i = 0; i < numElements; i++) { if (version < 7) { getInt(null); // We don't need size for anything } K key = (K) type.getKeyType().createFieldValue(); V val = (V) type.getValueType().createFieldValue(); key.deserialize(null, this); val.deserialize(null, this); hash.put(key, val); } map.clear(); map.putAll(hash); }
private static void extractNestedTypes(NewDocumentType dt, DataType type) { if (type instanceof StructDataType) { StructDataType tmp = (StructDataType) type; extractDataTypesFromFields(dt, tmp.getFieldsThisTypeOnly()); } else if (type instanceof DocumentType) { throw new IllegalArgumentException("Can not handle nested document definitions. In document type '" + dt.getName().toString() + "', we can not define document type '" + type.toString()); } else if (type instanceof CollectionDataType) { CollectionDataType tmp = (CollectionDataType) type; extractNestedTypes(dt, tmp.getNestedType()); addType(dt, tmp.getNestedType()); } else if (type instanceof MapDataType) { MapDataType tmp = (MapDataType) type; extractNestedTypes(dt, tmp.getKeyType()); extractNestedTypes(dt, tmp.getValueType()); addType(dt, tmp.getKeyType()); addType(dt, tmp.getValueType()); } else if (type instanceof TemporaryAnnotationReferenceDataType) { throw new IllegalArgumentException(type.toString()); } } private static boolean testAddType(NewDocumentType dt, DataType type) { return internalAddType(dt, type, true); }
public <K extends FieldValue, V extends FieldValue> void read(FieldBase field, MapFieldValue<K, V> map) { int numElements = getNumCollectionElems(); Map<K,V> hash = new HashMap<>(); MapDataType type = map.getDataType(); for (int i = 0; i < numElements; i++) { if (version < 7) { getInt(null); // We don't need size for anything } K key = (K) type.getKeyType().createFieldValue(); V val = (V) type.getValueType().createFieldValue(); key.deserialize(null, this); val.deserialize(null, this); hash.put(key, val); } map.clear(); map.putAll(hash); }
structFields.put("key", keyField); SDField valueField = new SDField(sdoc, name.concat(".value"), mdt.getValueType(), isHeader, getOwnerDocType(), new Matching(), true, recursion + 1); structFields.put("value", valueField);
private void buildConfig(MapDataType type, DocumenttypesConfig.Documenttype.Datatype.Builder dataTypeBuilder, DocumenttypesConfig.Documenttype.Builder documentBuilder, Set<Integer> built) { dataTypeBuilder. type(DocumenttypesConfig.Documenttype.Datatype.Type.Enum.MAP). map(new DocumenttypesConfig.Documenttype.Datatype.Map.Builder(). key(new DocumenttypesConfig.Documenttype.Datatype.Map.Key.Builder(). id(type.getKeyType().getId())). value(new DocumenttypesConfig.Documenttype.Datatype.Map.Value.Builder(). id(type.getValueType().getId()))); buildConfig(type.getKeyType(), documentBuilder, built); buildConfig(type.getValueType(), documentBuilder, built); }
private void checkFieldType(SDField field, DataType dataType) { if (dataType instanceof ArrayDataType) { DataType nestedType = ((ArrayDataType) dataType).getNestedType(); checkFieldType(field, nestedType); } else if (dataType instanceof WeightedSetDataType) { DataType nestedType = ((WeightedSetDataType) dataType).getNestedType(); if ( ! (nestedType instanceof PrimitiveDataType)) { fail(search, field, "Weighted set must have a primitive key type."); } } else if (dataType instanceof MapDataType) { DataType keyType = ((MapDataType) dataType).getKeyType(); if ( ! (keyType instanceof PrimitiveDataType)) { fail(search, field, "Map key type must be a primitive type."); } checkFieldType(field, ((MapDataType) dataType).getValueType()); } }
private static DataType createCompatType(DataType origType) { if (origType instanceof ArrayDataType) { return DataType.getArray(createCompatType(((ArrayDataType)origType).getNestedType())); } else if (origType instanceof MapDataType) { MapDataType mapType = (MapDataType)origType; return DataType.getMap(createCompatType(mapType.getKeyType()), createCompatType(mapType.getValueType())); } else if (origType instanceof WeightedSetDataType) { return DataType.getWeightedSet(createCompatType(((WeightedSetDataType)origType).getNestedType())); } else if (origType == PositionDataType.INSTANCE) { return DataType.LONG; } else { return origType; } } }
@SuppressWarnings({ "rawtypes", "cast", "unchecked" }) public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent) { JsonToken token = buffer.currentToken(); int initNesting = buffer.nesting(); expectObjectStart(token); token = buffer.next(); DataType keyType = parent.getDataType().getKeyType(); DataType valueType = parent.getDataType().getValueType(); while (buffer.nesting() >= initNesting) { FieldValue key = readAtomic(buffer.currentName(), keyType); FieldValue value = readSingleValue(buffer, valueType); Preconditions.checkState(key != null && value != null, "Missing key or value for map entry."); parent.put(key, value); token = buffer.next(); } expectObjectEnd(token); }