/** * Creates a new field. * * @param name The name of the field * @param dataType The datatype of the field * @param isHeader Whether this is a "header" field or a "content" field * (true = "header"). */ public Field(String name, int id, DataType dataType, boolean isHeader) { super(name); this.fieldId = id; this.fieldIdV6 = id; this.dataType = dataType; this.isHeader = isHeader; this.forcedId = true; validateId(id, null, Document.SERIALIZED_VERSION); }
/** * The field id must be unique within a document type, and also * within a (unknown at this time) hierarchy of document types. * In addition it should be as resilient to doctype content changes * and inheritance hierarchy changes as possible. * All of this is enforced for names, so id's should follow names. * Therefore we hash on name. */ private int calculateIdV6(DocumentType owner) { int newId = BobHash.hash(getName()); // Using a portfriendly hash if (newId < 0) newId = -newId; // Highest bit is reserved to tell 7-bit id's from 31-bit ones validateId(newId, owner, 6); return newId; }
/** * Sets the id of this field. Don't do this unless you know what you are doing * * @param newId the id - if this is less than 100 it will cause document to serialize * using just one byte for this field id. 100-127 are reserved values * @param owner the owning document, this is checked for collisions and notified * of the id change. It can not be null */ public void setId(int newId, DocumentType owner) { if (owner == null) { throw new NullPointerException("Can not assign an id of " + this + " without knowing the owner"); } validateId(newId, owner, Document.SERIALIZED_VERSION); owner.removeField(getName()); this.fieldId = newId; this.fieldIdV6 = newId; this.forcedId = true; owner.addField(this); }
/** * The field id must be unique within a document type, and also * within a (unknown at this time) hierarchy of document types. * In addition it should be as resilient to doctype content changes * and inheritance hierarchy changes as possible. * All of this is enforced for names, so id's should follow names. * Therefore we hash on name. */ protected int calculateIdV7(DocumentType owner) { String combined = getName() + dataType.getId(); int newId = BobHash.hash(combined); // Using a portfriendly hash if (newId < 0) newId = -newId; // Highest bit is reserved to tell 7-bit id's from 31-bit ones validateId(newId, owner, Document.SERIALIZED_VERSION); return newId; }