/** * Returns whether this type defines the given field name * * @param name The name of the field to check if it has * @return True if there is a field with the given name. */ public boolean hasField(String name) { return getField(name) != null; }
public FieldUpdate removeFieldUpdate(String fieldName) { Field field = documentType.getField(fieldName); return field != null ? removeFieldUpdate(field) : null; }
/** * Returns the update for a field name * * @param fieldName the field name to return the update of * @return the update for the field, or null if that field has no update in this */ public FieldUpdate getFieldUpdate(String fieldName) { Field field = documentType.getField(fieldName); return field != null ? getFieldUpdate(field) : null; } private FieldUpdate getFieldUpdateById(Integer fieldId) {
FieldSet parseFieldCollection(DocumentTypeManager docMan, String docType, String fieldNames) { DocumentType type = docMan.getDocumentType(docType); if (type == null) { throw new IllegalArgumentException("Unknown document type " + docType); } StringTokenizer tokenizer = new StringTokenizer(fieldNames, ","); FieldCollection collection = new FieldCollection(type); for (; tokenizer.hasMoreTokens(); ) { String token = tokenizer.nextToken(); Field f = type.getField(token); if (f == null) { throw new IllegalArgumentException("No such field " + token); } collection.add(f); } return collection; }
private void validateId(int newId, DocumentType owner, int version) { if (newId >= 100 && newId <= 127) { throw new IllegalArgumentException("Attempt to set the id of " + this + " to " + newId + " failed, values from 100 to 127 " + "are reserved for internal use"); } if ((newId & 0x80000000) != 0) // Highest bit must not be set { throw new IllegalArgumentException("Attempt to set the id of " + this + " to " + newId + " failed, negative id values " + " are illegal"); } if (owner == null) return; { Field existing = owner.getField(newId, version); if (existing != null && !existing.getName().equals(getName())) { throw new IllegalArgumentException("Couldn't set id of " + this + " to " + newId + ", " + existing + " already has this id in " + owner); } } }
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."); } }
FieldUpdate readAlter(DocumentUpdate update) throws XMLStreamException { Field f = null; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("field".equals(reader.getAttributeName(i).toString())) { f = update.getDocumentType().getField(reader.getAttributeValue(i)); } } if (f == null) { throw newDeserializeException("Alter update without \"field\" attribute"); } FieldUpdate fu = FieldUpdate.create(f); while (reader.hasNext()) { int type = reader.next(); if (type == XMLStreamReader.START_ELEMENT) { if ("increment".equals(reader.getName().toString()) || "decrement".equals(reader.getName().toString()) || "multiply".equals(reader.getName().toString()) || "divide".equals(reader.getName().toString())) { update.addFieldUpdate(readArithmetic(update, reader.getName().toString(), f, fu)); skipToEnd(reader.getName().toString()); } else { throw newDeserializeException("Element \"" + reader.getName() + "\" not appropriate within alter element"); } } else if (type == XMLStreamReader.END_ELEMENT) { break; } } return fu; }
FieldUpdate readArithmeticField(DocumentUpdate update, String type) throws XMLStreamException { Field f = null; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("field".equals(reader.getAttributeName(i).toString())) { f = update.getDocumentType().getField(reader.getAttributeValue(i)); } } if (f == null) { throw newDeserializeException("Assignment update without \"field\" attribute"); } FieldUpdate fu = FieldUpdate.create(f); readArithmetic(update, type, f, fu); return fu; }
@Override public Field getField(int id) { Field field = headerType.getField(id); if (field == null) { field = bodyType.getField(id); } if (field == null && !isRegistered()) { for (DocumentType inheritedType : inherits) { field = inheritedType.getField(id); if (field != null) break; } } return field; }
FieldUpdate readAssign(DocumentUpdate update) throws XMLStreamException { for (int i = 0; i < reader.getAttributeCount(); i++) { if ("field".equals(reader.getAttributeName(i).toString())) { Field f = update.getDocumentType().getField(reader.getAttributeValue(i)); if (f == null) { throw newDeserializeException("Field " + reader.getAttributeValue(i) + " not found."); } FieldValue value = f.getDataType().createFieldValue(); value.deserialize(f, this); return FieldUpdate.createAssign(f, value); } } throw newDeserializeException("Assignment update without field attribute"); }
/** * Gets the field matching a given name. * * @param name The name of a field. * @return Returns the matching field, or null if not found. */ public Field getField(String name) { Field field = headerType.getField(name); if (field == null) { field = bodyType.getField(name); } if (field == null && !isRegistered()) { for (DocumentType inheritedType : inherits) { field = inheritedType.getField(name); if (field != null) break; } } return field; }
/** * Gets the field matching a given ID. * * @param id The ID of a field. * @param version The serialization version of the document. * @return Returns the matching field, or null if not found. */ public Field getField(Integer id, int version) { Field field = headerType.getField(id, version); if (field == null) { field = bodyType.getField(id, version); } if (field == null && !isRegistered()) { for (DocumentType inheritedType : inherits) { field = inheritedType.getField(id, version); if (field != null) break; } } return field; }
@Override public Field getField(String fieldName) { Field field = header.getField(fieldName); if (field == null) { field = body.getField(fieldName); } if (field == null) { for(DocumentType parent : getDataType().getInheritedTypes()) { field = parent.getField(fieldName); if (field != null) { break; } } } return field; }
FieldUpdate readRemove(DocumentUpdate update) throws XMLStreamException { for (int i = 0; i < reader.getAttributeCount(); i++) { if ("field".equals(reader.getAttributeName(i).toString())) { Field f = update.getDocumentType().getField(reader.getAttributeValue(i)); FieldValue value = f.getDataType().createFieldValue(); value.deserialize(f, this); if (value instanceof Array) { List<FieldValue> l = ((Array)value).getValues(); return FieldUpdate.createRemoveAll(f, l); } else if (value instanceof WeightedSet) { return FieldUpdate.createRemoveAll(f, ((WeightedSet)value)); } else { throw newDeserializeException("Remove operation only applicable to multivalue lists"); } } } throw newDeserializeException("Remove update without field attribute"); }
FieldUpdate readAdd(DocumentUpdate update) throws XMLStreamException { for (int i = 0; i < reader.getAttributeCount(); i++) { if ("field".equals(reader.getAttributeName(i).toString())) { Field f = update.getDocumentType().getField(reader.getAttributeValue(i)); FieldValue value = f.getDataType().createFieldValue(); value.deserialize(f, this); if (value instanceof Array) { List<FieldValue> l = ((Array)value).getValues(); return FieldUpdate.createAddAll(f, l); } else if (value instanceof WeightedSet) { return FieldUpdate.createAddAll(f, ((WeightedSet) value)); } else { throw newDeserializeException("Add operation only applicable to multivalue lists"); } } } throw newDeserializeException("Add update without field attribute"); }
/** * Adds the given {@link FieldUpdate} to this DocumentUpdate. If this DocumentUpdate already contains a FieldUpdate * for the named field, the content of the given FieldUpdate is added to the existing one. * * @param update The FieldUpdate to add to this DocumentUpdate. * @return This, to allow chaining. * @throws IllegalArgumentException If the {@link DocumentType} of this DocumentUpdate does not have a corresponding * field. */ public DocumentUpdate addFieldUpdate(FieldUpdate update) { int fieldId = update.getField().getId(); if (documentType.getField(fieldId) == null) { throw new IllegalArgumentException("Document type '" + documentType.getName() + "' does not have field '" + update.getField().getName() + "'."); } FieldUpdate prevUpdate = getFieldUpdateById(fieldId); if (prevUpdate != update) { if (prevUpdate != null) { prevUpdate.addAll(update); } else { id2FieldUpdates.put(fieldId, update); } } return this; }
@Override public void tryOutputType(Expression exp, String fieldName, DataType valueType) { Field field = output.getDataType().getField(fieldName); if (field == null) { throw new VerificationException(exp, "Field '" + fieldName + "' not found."); } DataType fieldType = field.getDataType(); if (!fieldType.isAssignableFrom(valueType)) { throw new VerificationException(exp, "Can not assign " + valueType.getName() + " to field '" + fieldName + "' which is " + fieldType.getName() + "."); } }
/** * Fail if the subtype changes the type of any equally named field. * * @param superType The supertype to verify against * TODO Add strict type checking no duplicate fields are allowed */ private void verifyTypeConsistency(DocumentType superType) { for (Field f : fieldSet()) { Field supField = superType.getField(f.getName()); if (supField != null) { if (!f.getDataType().equals(supField.getDataType())) { throw new IllegalArgumentException("Inheritance type mismatch: field \"" + f.getName() + "\" in datatype \"" + getName() + "\"" + " must have same datatype as in parent document type \"" + superType.getName() + "\""); } } } }
public void read(FieldUpdate fieldUpdate) { int fieldId = getInt(null); Field field = fieldUpdate.getDocumentType().getField(fieldId, fieldUpdate.getSerializationVersion()); if (field == null) { throw new DeserializationException( "Cannot deserialize FieldUpdate, field fieldId " + fieldId + " not found in " + fieldUpdate.getDocumentType()); } fieldUpdate.setField(field); int size = getInt(null); for (int i = 0; i < size; i++) { if (field.getDataType() instanceof CollectionDataType) { CollectionDataType collType = (CollectionDataType) field.getDataType(); fieldUpdate.addValueUpdate(getValueUpdate(collType, collType.getNestedType())); } else { fieldUpdate.addValueUpdate(getValueUpdate(field.getDataType(), null)); } } }
public void read(FieldUpdate fieldUpdate) { int fieldId = getInt(null); Field field = fieldUpdate.getDocumentType().getField(fieldId, fieldUpdate.getSerializationVersion()); if (field == null) { throw new DeserializationException( "Cannot deserialize FieldUpdate, field fieldId " + fieldId + " not found in " + fieldUpdate.getDocumentType()); } fieldUpdate.setField(field); int size = getInt(null); for (int i = 0; i < size; i++) { if (field.getDataType() instanceof CollectionDataType) { CollectionDataType collType = (CollectionDataType) field.getDataType(); fieldUpdate.addValueUpdate(getValueUpdate(collType, collType.getNestedType())); } else { fieldUpdate.addValueUpdate(getValueUpdate(field.getDataType(), null)); } } }