com.foreach.across.modules.entity.autosuggest.SimpleAutoSuggestDataSet Maven / Gradle / Ivy
/*
* Copyright 2014 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.foreach.across.modules.entity.autosuggest;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* Represents a simple dataset used for auto-suggest data.
* A dataset has a function for suggestions based on a query, and one to prefetch suggestion data.
*
* The return value of the functions is a general object which will usually be converted
* to a specific output format by a controller. Usually the return value is one of the following:
*
* - {@code null} or an empty collection to indicate no results
* - a collection holding the objects that should be returned to the caller; usually converted to JSON
* - a {@link org.springframework.http.ResponseEntity} that can already hold the JSON data
*
*
* @author Arne Vandamme
* @see AutoSuggestDataController
* @since 3.0.0
*/
@Builder
@RequiredArgsConstructor
public class SimpleAutoSuggestDataSet implements AutoSuggestDataSet
{
private final BiFunction suggestionsLoader;
private final Function prefetchLoader;
/**
* Retrieve the suggestions for a particular query.
* The second parameter is an optional control name for which the suggestions are requested.
*
* @param query usually the input of the user
* @param controlName name of the control for which the suggestions are requested
* @return suggestions result
*/
@Override
public Object suggestions( String query, String controlName ) {
return suggestionsLoader != null ? suggestionsLoader.apply( query, controlName ) : null;
}
/**
* Retrieve the prefetch data for this set. Prefetch data is the initial data that
* should be loaded and possibly shown as suggestions, without the user having entered
* a particular query string.
*
* @param controlName name of the control for which the suggestions are requested
* @return suggestions result
*/
@Override
public Object prefetch( String controlName ) {
return prefetchLoader != null ? prefetchLoader.apply( controlName ) : null;
}
@Override
public boolean isPrefetchSupported() {
return prefetchLoader != null;
}
/**
* Represent a single auto-suggest result.
*/
@RequiredArgsConstructor
@Data
public static class Result implements AutoSuggestDataSet.Result
{
private final Object id;
private final String label;
/**
* Create a result instance of an object, where the object is considered to be the
* value of {@link #id} and the {@code toString()} method of the object will be called
* to generate the label.
*
* @param data which represents the result
* @return result instance
*/
public static Result of( @NonNull Object data ) {
return new Result( data, data.toString() );
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy