public Array(DataType type, List<T> values) { this(type); for (T v : values) { if (!((ArrayDataType)type).getNestedType().isValueCompatible(v)) { throw new IllegalArgumentException("FieldValue " + v + " is not compatible with " + type + "."); } } this.values.addAll(values); }
private static boolean isArrayOfStructType(DataType type) { if (type instanceof ArrayDataType) { ArrayDataType arrayType = (ArrayDataType)type; return isStructType(arrayType.getNestedType()); } else { return false; } }
private static boolean isSupportedPositionType(DataType dataType) { if (dataType instanceof ArrayDataType) { dataType = ((ArrayDataType)dataType).getNestedType(); } return dataType.equals(PositionDataType.INSTANCE); }
@Override public FieldPath buildFieldPath(String remainFieldName) { if (remainFieldName.length() > 0 && remainFieldName.charAt(0) == '[') { int endPos = remainFieldName.indexOf(']'); if (endPos == -1) { throw new IllegalArgumentException("Array subscript must be closed with ]"); } else { FieldPath path = getNestedType().buildFieldPath(skipDotInString(remainFieldName, endPos)); List<FieldPathEntry> tmpPath = new ArrayList<FieldPathEntry>(path.getList()); if (remainFieldName.charAt(1) == '$') { tmpPath.add(0, FieldPathEntry.newVariableLookupEntry(remainFieldName.substring(2, endPos), getNestedType())); } else { tmpPath.add(0, FieldPathEntry.newArrayLookupEntry(Integer.parseInt(remainFieldName.substring(1, endPos)), getNestedType())); } return new FieldPath(tmpPath); } } return getNestedType().buildFieldPath(remainFieldName); }
public static boolean isArrayOfSimpleStruct(ImmutableSDField field) { if (field.getDataType() instanceof ArrayDataType) { ArrayDataType arrayType = (ArrayDataType)field.getDataType(); return isStructWithPrimitiveStructFieldAttributes(arrayType.getNestedType(), field); } else { return false; } }
public <T extends FieldValue> void read(FieldBase field, Array<T> value) { try { while (reader.hasNext()) { int type = reader.next(); if (type == XMLStreamReader.START_ELEMENT) { if ("item".equals(reader.getName().toString())) { FieldValue fv = (value.getDataType()).getNestedType().createFieldValue(); deserializeFieldValue(field, fv); // noinspection unchecked value.add((T)fv); skipToEnd("item"); } } else if (type == XMLStreamReader.END_ELEMENT) { return; } } } catch (XMLStreamException e) { throw newException(field, e); } }
@Override protected void checkCompatibility(DataType fieldType) { if (fieldType instanceof ArrayDataType) { if (!(value instanceof IntegerFieldValue)) { throw new IllegalArgumentException("Expected integer, got " + value.getClass().getName() + "."); } update.checkCompatibility(((ArrayDataType)fieldType).getNestedType()); } else if (fieldType instanceof WeightedSetDataType) { ((WeightedSetDataType)fieldType).getNestedType().createFieldValue().assign(value); update.checkCompatibility(DataType.INT); } else if (fieldType instanceof StructuredDataType) { if (!(value instanceof StringFieldValue)) { throw new IllegalArgumentException("Expected string, got " + value.getClass().getName() + "."); } Field field = ((StructuredDataType)fieldType).getField(((StringFieldValue)value).getString()); if (field == null) { throw new IllegalArgumentException("Field '" + value + "' not found."); } update.checkCompatibility(field.getDataType()); } else { throw new UnsupportedOperationException("Field type " + fieldType.getName() + " not supported."); } }
private void buildConfig(ArrayDataType type, DocumenttypesConfig.Documenttype.Datatype.Builder dataTypeBuilder, DocumenttypesConfig.Documenttype.Builder documentBuilder, Set<Integer> built) { dataTypeBuilder. type(DocumenttypesConfig.Documenttype.Datatype.Type.Enum.ARRAY). array(new DocumenttypesConfig.Documenttype.Datatype.Array.Builder(). element(new DocumenttypesConfig.Documenttype.Datatype.Array.Element.Builder().id(type.getNestedType().getId()))); buildConfig(type.getNestedType(), documentBuilder, built); }
static void validate(DocumentTypeManager manager, String documentTypeName, String arrayFieldName) { DocumentType docType = manager.getDocumentType(documentTypeName); if (docType == null) { //the document type does not exist, return throw new IllegalStateException("The document type " + documentTypeName + " is not deployed."); } if (docType.getField(arrayFieldName) == null) { //the document type does not have the field, return throw new IllegalStateException("The document type " + documentTypeName + " does not have a field named " + arrayFieldName + "."); } if (!(docType.getField(arrayFieldName).getDataType() instanceof ArrayDataType)) { //the data type of the field is wrong, return throw new IllegalStateException("The data type of the field named " + arrayFieldName + " in document type " + documentTypeName + " is not an array type"); } ArrayDataType fieldDataType = (ArrayDataType) docType.getField(arrayFieldName).getDataType(); if (!(fieldDataType.getNestedType() instanceof DocumentType)) { //the subtype of tye array data type of the field is wrong, return throw new IllegalStateException("The data type of the field named " + arrayFieldName + " in document type " + documentTypeName + " is not an array of Document."); } }
public boolean hasFieldForStructFieldAttribute(Attribute structFieldAttribute) { StringTokenizer fieldNames = new StringTokenizer(structFieldAttribute.getName(), "."); if (!fieldNames.nextToken().equals(field.getName())) { return false; } if (isArrayOfStructType(dataType())) { StructDataType nestedType = (StructDataType)((ArrayDataType)dataType()).getNestedType(); if (structTypeContainsLastFieldNameComponent(nestedType, fieldNames)) { return true; } } else if (isMapOfStructType(dataType())) { MapDataType mapType = (MapDataType)dataType(); StructDataType valueType = (StructDataType)mapType.getValueType(); String subFieldName = fieldNames.nextToken(); if (subFieldName.equals("key") && !fieldNames.hasMoreTokens()) { return true; } else if (subFieldName.equals("value") && structTypeContainsLastFieldNameComponent(valueType, fieldNames)) { return true; } } else if (isMapOfPrimitiveType(dataType())) { String subFieldName = fieldNames.nextToken(); if ((subFieldName.equals("key") || subFieldName.equals("value")) && !fieldNames.hasMoreTokens()) { return true; } } return false; }
public <T extends FieldValue> void read(FieldBase field, Array<T> array) { int numElements = getNumCollectionElems(); ArrayList<T> list = new ArrayList<T>(numElements); ArrayDataType type = array.getDataType(); for (int i = 0; i < numElements; i++) { if (version < 7) { getInt(null); // We don't need size for anything } FieldValue fv = type.getNestedType().createFieldValue(); fv.deserialize(null, this); list.add((T) fv); } array.clear(); array.addAll(list); }
public <T extends FieldValue> void read(FieldBase field, Array<T> array) { int numElements = getNumCollectionElems(); ArrayList<T> list = new ArrayList<T>(numElements); ArrayDataType type = array.getDataType(); for (int i = 0; i < numElements; i++) { if (version < 7) { getInt(null); // We don't need size for anything } FieldValue fv = type.getNestedType().createFieldValue(); fv.deserialize(null, this); list.add((T) fv); } array.clear(); array.addAll(list); }
@SuppressWarnings("deprecation") static List<Field> flattenField(Field field) { DataType fieldType = field.getDataType(); if (fieldType.getPrimitiveType() != null){ return Collections.singletonList(field); } if (fieldType instanceof ArrayDataType) { boolean header = field.isHeader(); List<Field> ret = new LinkedList<>(); Field innerField = new Field(field.getName(), ((ArrayDataType)fieldType).getNestedType(), header); for (Field flatField : flattenField(innerField)) { ret.add(new Field(flatField.getName(), DataType.getArray(flatField.getDataType()), header)); } return ret; } if (fieldType instanceof StructuredDataType) { List<Field> ret = new LinkedList<>(); String fieldName = field.getName(); for (Field childField : ((StructuredDataType)fieldType).getFields()) { for (Field flatField : flattenField(childField)) { ret.add(new Field(fieldName + "." + flatField.getName(), flatField)); } } return ret; } throw new UnsupportedOperationException(fieldType.getName()); }
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; } } }