@Override public Object getId(T item) { Object itemId = idGetter.apply(item); assert itemId != null : "CallbackDataProvider got null as an id for item: " + item; return itemId; } }
@Override protected String getItemKey(T item) { if (keyMapper == null) { return null; } return keyMapper.apply(item); }
@Override public boolean has(V o) { return objectIdKeyMap.containsKey(identifierGetter.apply(o)); }
/** * Removes object from the mapper. * * @param removeobj * the object to be removed. */ @Override public void remove(V removeobj) { final String key = objectIdKeyMap .remove(identifierGetter.apply(removeobj)); if (key != null) { keyObjectMap.remove(key); } }
@Override public void refresh(V dataObject) { Object id = identifierGetter.apply(dataObject); String key = objectIdKeyMap.get(id); if (key != null) { keyObjectMap.put(key, dataObject); } }
@Override public void setIdentifierGetter(ValueProvider<V, Object> identifierGetter) { if (this.identifierGetter != identifierGetter) { this.identifierGetter = identifierGetter; objectIdKeyMap.clear(); for (Map.Entry<String, V> entry : keyObjectMap.entrySet()) { objectIdKeyMap.put(identifierGetter.apply(entry.getValue()), entry.getKey()); } } } }
/** * Creates a predicate that compares equality of the given required value to * the value the given value provider obtains. * * @param valueProvider * the value provider to use * @param requiredValue * the required value * @param <T> * the data provider object type * @param <V> * the provided value type * @return the created predicate */ public static <T, V> SerializablePredicate<T> createEqualsFilter( ValueProvider<T, V> valueProvider, V requiredValue) { Objects.requireNonNull(valueProvider, "Value provider cannot be null"); return item -> Objects.equals(valueProvider.apply(item), requiredValue); } }
/** * Creates a comparator for the return type of the given * {@link ValueProvider}, sorted in the direction specified by the given * {@link SortDirection}. * * @param valueProvider * the value provider to use * @param sortDirection * the sort direction to use * @param <T> * the data provider object type * @param <V> * the provided value type * * @return the created comparator */ public static <V extends Comparable<? super V>, T> SerializableComparator<T> propertyComparator( ValueProvider<T, V> valueProvider, SortDirection sortDirection) { Objects.requireNonNull(valueProvider, "Value provider cannot be null"); Objects.requireNonNull(sortDirection, "Sort direction cannot be null"); Comparator<V> comparator = getNaturalSortComparator(sortDirection); return (a, b) -> comparator.compare(valueProvider.apply(a), valueProvider.apply(b)); }
/** * Stores the state of the given bean. * * @param bean * the bean to store the state of * @param bindings * the bindings to store * * @return map from binding to value */ @SuppressWarnings({ "unchecked", "rawtypes" }) protected Map<Binding<BEAN, ?>, Object> getBeanState(BEAN bean, Collection<Binding<BEAN, ?>> bindings) { Map<Binding<BEAN, ?>, Object> oldValues = new HashMap<>(); bindings.stream().map(binding -> (BindingImpl) binding) .filter(binding -> binding.setter != null) .forEach(binding -> oldValues.put(binding, binding.getter.apply(bean))); return oldValues; }
/** * Creates a new predicate from the given predicate and value provider. This * allows using a predicate of the value providers return type with objects * of the value providers type. * * @param valueProvider * the value provider to use * @param valueFilter * the original predicate * @param <T> * the data provider object type * @param <V> * the provided value type * * @return the created predicate */ public static <T, V> SerializablePredicate<T> createValueProviderFilter( ValueProvider<T, V> valueProvider, SerializablePredicate<V> valueFilter) { return item -> valueFilter.test(valueProvider.apply(item)); }
@Override protected String createKey() { return Optional.ofNullable(uniqueKeyProviderSupplier.get()) .map(provider -> provider.apply(object)) .orElse(super.createKey()); } };
/** * Adds the given items as root items and uses the given value provider to * recursively populate children of the root items. * * @param rootItems * the root items to add * @param childItemProvider * the value provider used to recursively populate this TreeData * from the given root items * @return this */ public TreeData<T> addItems(Stream<T> rootItems, ValueProvider<T, Stream<T>> childItemProvider) { // Must collect to lists since the algorithm iterates multiple times return addItems(rootItems.collect(Collectors.toList()), item -> childItemProvider.apply(item) .collect(Collectors.toList())); }
/** * Gets key for an object. * * @param o * the object. */ @Override public String key(V o) { if (o == null) { return "null"; } // If the object is already mapped, use existing key Object id = identifierGetter.apply(o); String key = objectIdKeyMap.get(id); if (key != null) { return key; } // If the object is not yet mapped, map it key = createKey(); objectIdKeyMap.put(id, key); keyObjectMap.put(key, o); return key; }
private void addItemsRecursively(Collection<T> items, ValueProvider<T, Collection<T>> childItemProvider) { items.forEach(item -> { Collection<T> childItems = childItemProvider.apply(item); addItems(item, childItems); addItemsRecursively(childItems, childItemProvider); }); } }
@Override public ValueProvider<T, V> getGetter() { return bean -> { Method readMethod = getDescriptor().getReadMethod(); Object value = invokeWrapExceptions(readMethod, parent.getGetter().apply(bean)); return getType().cast(value); }; }
@Override public Optional<Setter<T, V>> getSetter() { if (getDescriptor().getWriteMethod() == null) { return Optional.empty(); } Setter<T, V> setter = (bean, value) -> { // Do not "optimize" this getter call, // if its done outside the code block, that will produce // NotSerializableException because of some lambda compilation // magic Method innerSetter = getDescriptor().getWriteMethod(); invokeWrapExceptions(innerSetter, parent.getGetter().apply(bean), value); }; return Optional.of(setter); }
@Override public void read(BEAN bean) { field.setValue(convertToFieldType(getter.apply(bean))); }
private void generateTreeData(T item, JsonObject jsonObject) { Optional.ofNullable(getParentItem(item)) .ifPresent(parent -> jsonObject.put("parentUniqueKey", uniqueKeyProviderSupplier.get().apply(parent))); }
@Override public Component createComponent(SOURCE item) { return new Span(getFormattedValue(valueProvider.apply(item))); }
@Override public String apply(T source) { return StringValuePresenter.getDefault().present(provider.apply(source)); }