2

I want to set the length for a String type field,but i see the Source code:

value is reset after a call to {@link #add(String, Class)}

What is the significance of a method?How can i set the length for a String type field?anyone could help me

/**
 * Sets a restriction on the field length of the next attribute added to the feature type.
 *
 * <p>This method is the same as adding a restriction based on length( value ) < length This
 * value is reset after a call to {@link #add(String, Class)}
 *
 * @return length Used to limit the length of the next attribute created
 */
public SimpleFeatureTypeBuilder length(int length) {
    attributeBuilder.setLength(length);
    return this;
}
Mr.H
  • 59
  • 7

1 Answers1

0

If you follow through the code you will see that add(String, Class) is called to add the new attribute to the schema. It calls out to AttributeTypeBuilder to encode the attribute. This is where setLength stores the length restriction you set in length. Once the attribute is generated resetTypeState is called to "reset" the attribute builder ready for your next attribute (this is where length is reset to null).

So the JavaDoc warning is correct length is reset after you add the attribute to the schema, so if you want to set a length restriction on an attribute you need to do something like:

fBuilder.length(30);
fBuilder.add("myStringAttr",String.class);
schema = fBuilder.buildFeatureType();
Ian Turton
  • 10,018
  • 1
  • 28
  • 47