首页 > 解决方案 > Geotools:关于 SimpleFeatureTypeBuilder 的 length(int length) 方法

问题描述

我想设置字符串类型字段的长度,但是我看到了源代码:</p>

调用 {@link #add(String, Class)} 后重置值

方法的意义是什么?如何设置字符串类型字段的长度?任何人都可以帮助我

/**
 * 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;
}

标签: javageotools

解决方案


如果您按照代码进行操作,您将看到add(String, Class)调用该代码以将新属性添加到架构中。它要求AttributeTypeBuilder 对属性进行编码。这是setLength存储您设置的长度限制的地方length。一旦生成了属性,resetTypeState就会调用“重置”属性构建器来准备下一个属性(这是将长度重置为空的地方)。

因此,在将属性添加到模式后,JavaDoc 警告是正确的长度会被重置,因此,如果要对属性设置长度限制,则需要执行以下操作:

fBuilder.length(30);
fBuilder.add("myStringAttr",String.class);
schema = fBuilder.buildFeatureType();

推荐阅读