of features we would like to build ( assume schema = (geom:Point,name:String) ) SimpleFeatureType featureType = ... //create the builder SimpleFeatureBuilder builder = new SimpleFeatureBuilder(); //set the type of created features builder.setType( featureType ); //add the attributes builder.add( new Point( 0 , 0 ) ); builder.add( "theName" ); //build the feature SimpleFeature feature = builder.buildFeature( "fid" );
This builder builds a feature by maintaining state. Each call to {@link #add(Object)}creates a new attribute for the feature and stores it locally. When using the add method to add attributes to the feature, values added must be added in the same order as the attributes as defined by the feature type. The methods {@link #set(String,Object)} and {@link #set(int,Object)} are used to add attributes out of order.
Each time the builder builds a feature with a call to {@link #buildFeature(String)}the internal state is reset.
This builder can be used to copy features as well. The following code sample demonstrates: //original feature SimpleFeature original = ...; //create and initialize the builder SimpleFeatureBuilder builder = new SimpleFeatureBuilder(); builder.init(original); //create the new feature SimpleFeature copy = builder.buildFeature( original.getID() );
The builder also provides a number of static "short-hand" methods which can be used when its not ideal to instantiate a new builder, thought this will trigger some extra object allocations. In time critical code sections it's better to instantiate the builder once and use it to build all the required features. SimpleFeatureType type = ..; Object[] values = ...; //build a new feature SimpleFeature feature = SimpleFeatureBuilder.build( type, values, "fid" ); ... SimpleFeature original = ...; //copy the feature SimpleFeature feature = SimpleFeatureBuilder.copy( original );
This class is not thread safe nor should instances be shared across multiple threads.
@author Justin Deoliveira
@author Jody Garnett
@source $URL$