Skip to content

Method Grouping and Triggering

Ben Fagin edited this page Oct 19, 2013 · 7 revisions

Method Groups

For methods in your API which are not required (those declared with any() and atMost(..)) it is possible to assign to them a group number. Grouping methods allows for more complicated (A|B) scenarios, where the user might pick one method over another. The first time a method with a group is called, all other methods in the group are removed from play.

In the following example, assume we are writing a file uploader service. The developer could select either to upload from a file or from a stream, but not both. (For the moment, assume that we can't just wrap a file in a FileInputStream). After they make their selection, we want the other option to go away. To accomplish this, we can use method grouping.

int FILE_OR_STREAM = 1;

Descriptor descriptor = Flapi.builder()
    .setPackage("unquietcode.tools.flapi.examples")
    .setDescriptorName("FileUploader")

    .addMethod("fromFile(File file)").atMost(1, FILE_OR_STREAM)
    .addMethod("fromStream(InputStream stream, int length)").atMost(1, FILE_OR_STREAM)
.build();

descriptor.writeToStream(System.out);

In the resulting builder, after fromFile(..) is called the fromStream(..) method will disappear, and vice versa.

Method Triggering

Clone this wiki locally