Java Code Examples for com.vaadin.flow.function.SerializableFunction#apply()

The following examples show how to use com.vaadin.flow.function.SerializableFunction#apply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResultTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void map_norError_mapperIsApplied() {
    Result<String> result = new SimpleResult<String>("foo", null) {

        @Override
        public <S> Result<S> flatMap(
                SerializableFunction<String, Result<S>> mapper) {
            return mapper.apply("foo");
        }
    };
    Result<String> mapResult = result.map(value -> {
        Assert.assertEquals("foo", value);
        return "bar";
    });
    Assert.assertTrue(mapResult instanceof SimpleResult);
    Assert.assertFalse(mapResult.isError());
    mapResult.ifOk(v -> Assert.assertEquals("bar", v));
}
 
Example 2
Source File: SimpleResult.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> Result<S> flatMap(SerializableFunction<R, Result<S>> mapper) {
    Objects.requireNonNull(mapper, "mapper cannot be null");

    if (isError()) {
        // Safe cast; valueless
        return (Result<S>) this;
    } else {
        return mapper.apply(value);
    }
}
 
Example 3
Source File: SimpleResult.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public <X extends Throwable> R getOrThrow(
        SerializableFunction<String, ? extends X> exceptionSupplier)
                throws X {
    Objects.requireNonNull(exceptionSupplier,
            "Exception supplier cannot be null");
    if (isError()) {
        throw exceptionSupplier.apply(message);
    } else {
        return value;
    }
}
 
Example 4
Source File: MultiselectComboBox.java    From multiselect-combo-box-flow with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * MultiselectComboBox triggers filtering queries based on the strings users
 * type into the field. For this reason you need to provide the second
 * parameter, a function which converts the filter-string typed by the user
 * into filter-type used by your data provider. If your data provider
 * already supports String as the filter-type, it can be used without a
 * converter function via {@link #setDataProvider(DataProvider)}.
 * <p>
 * Using this method provides the same result as using a data provider
 * wrapped with
 * {@link DataProvider#withConvertedFilter(SerializableFunction)}.
 * <p>
 * Changing the multiselect combo box's data provider resets its current
 * value to {@code null}.
 */
@Override
public <C> void setDataProvider(DataProvider<T, C> dataProvider,
        SerializableFunction<String, C> filterConverter) {

    Objects.requireNonNull(dataProvider,
            "The data provider can not be null");
    Objects.requireNonNull(filterConverter,
            "filterConverter cannot be null");

    if (userProvidedFilter == UserProvidedFilter.UNDECIDED) {
        userProvidedFilter = UserProvidedFilter.YES;
    }

    if (dataCommunicator == null) {
        dataCommunicator = new MultiselectComboBoxDataCommunicator<>(dataGenerator,
                arrayUpdater, data -> getElement()
                        .callJsFunction("$connector.updateData", data),
                getElement().getNode());
    }

    scheduleRender();
    setValue(null);

    SerializableFunction<String, C> convertOrNull = filterText -> {
        if (filterText == null) {
            return null;
        }

        return filterConverter.apply(filterText);
    };

    SerializableConsumer<C> providerFilterSlot = dataCommunicator
            .setDataProvider(dataProvider, convertOrNull.apply(null));

    filterSlot = filter -> {
        if (!Objects.equals(filter, lastFilter)) {
            providerFilterSlot.accept(convertOrNull.apply(filter));
            lastFilter = filter;
        }
    };

    boolean shouldForceServerSideFiltering = userProvidedFilter == UserProvidedFilter.YES;

    dataProvider.addDataProviderListener(e -> {
        if (e instanceof DataChangeEvent.DataRefreshEvent) {
            dataCommunicator.refresh(
                    ((DataChangeEvent.DataRefreshEvent<T>) e).getItem());
        } else {
            refreshAllData(shouldForceServerSideFiltering);
        }
    });
    refreshAllData(shouldForceServerSideFiltering);

    userProvidedFilter = UserProvidedFilter.UNDECIDED;
}
 
Example 5
Source File: TaskUpdateImports.java    From flow with Apache License 2.0 3 votes vote down vote up
/**
 * Create an instance of the updater given all configurable parameters.
 *
 * @param finder
 *            a reusable class finder
 * @param frontendDepScanner
 *            a reusable frontend dependencies scanner
 * @param fallBackScannerProvider
 *            fallback scanner provider, not {@code null}
 * @param npmFolder
 *            folder with the `package.json` file
 * @param generatedPath
 *            folder where flow generated files will be placed.
 * @param frontendDirectory
 *            a directory with project's frontend files
 * @param tokenFile
 *            the token (flow-build-info.json) path, may be {@code null}
 * @param tokenFileData
 *            object to fill with token file data, may be {@code null}
 * @param disablePnpm
 *            if {@code true} then npm is used instead of pnpm, otherwise
 *            pnpm is used
 */
TaskUpdateImports(ClassFinder finder,
        FrontendDependenciesScanner frontendDepScanner,
        SerializableFunction<ClassFinder, FrontendDependenciesScanner> fallBackScannerProvider,
        File npmFolder, File generatedPath, File frontendDirectory,
        File tokenFile, JsonObject tokenFileData, boolean disablePnpm) {
    super(finder, frontendDepScanner, npmFolder, generatedPath, null);
    this.frontendDirectory = frontendDirectory;
    fallbackScanner = fallBackScannerProvider.apply(finder);
    this.tokenFile = tokenFile;
    this.tokenFileData = tokenFileData;
    this.disablePnpm = disablePnpm;
}
 
Example 6
Source File: AbstractSinglePropertyField.java    From flow with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new field that uses a property value with the given stateless
 * converters for producing a model value.
 * <p>
 * The property type must be one of the types that can be written as an
 * element property value: String, Integer, Double or Boolean.
 *
 * @param propertyName
 *            the name of the element property to use
 * @param defaultValue
 *            the default value to use if the property isn't defined
 * @param elementPropertyType
 *            the type of the element property
 * @param presentationToModel
 *            a function that converts a property value to a model value
 * @param modelToPresentation
 *            a function that converts a model value to a property value
 * @param <P>
 *            the property type
 */
public <P> AbstractSinglePropertyField(String propertyName, T defaultValue,
        Class<P> elementPropertyType,
        SerializableFunction<P, T> presentationToModel,
        SerializableFunction<T, P> modelToPresentation) {
    this(propertyName, defaultValue, elementPropertyType,
            (ignore, value) -> presentationToModel.apply(value),
            (ignore, value) -> modelToPresentation.apply(value));
}