Skip to content

Commit

Permalink
ItemBinding can be contravariant in adapters (#165)
Browse files Browse the repository at this point in the history
ItemBinding is used to map items of type T to their layout and
binding variable, among others. It might well be the case that
the passed ItemBinding can not only map instances of type T, but
any instances of some supertype of T, as well as T itself. This
relaxation is in fact already made for the ItemIds callback.
  • Loading branch information
villesinisalo authored and evant committed Nov 26, 2019
1 parent a78aa93 commit 7dfbeb4
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PagedBindingRecyclerViewAdapters {
@SuppressWarnings("unchecked")
@BindingAdapter(value = {"itemBinding", "items", "adapter", "itemIds", "viewHolder", "diffConfig"}, requireAll = false)
public static <T> void setAdapter(RecyclerView recyclerView,
ItemBinding<T> itemBinding,
ItemBinding<? super T> itemBinding,
PagedList<T> items,
BindingRecyclerViewAdapter<T> adapter,
BindingRecyclerViewAdapter.ItemIds<? super T> itemIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class BindingRecyclerViewAdapter<T> extends RecyclerView.Adapter<ViewHolder> implements BindingCollectionAdapter<T> {
private static final Object DATA_INVALIDATION = new Object();

private ItemBinding<T> itemBinding;
private ItemBinding<? super T> itemBinding;
private WeakReferenceOnListChangedCallback<T> callback;
private List<T> items;
private LayoutInflater inflater;
Expand All @@ -44,7 +44,7 @@ public class BindingRecyclerViewAdapter<T> extends RecyclerView.Adapter<ViewHold
private LifecycleOwner lifecycleOwner;

@Override
public void setItemBinding(@NonNull ItemBinding<T> itemBinding) {
public void setItemBinding(@NonNull ItemBinding<? super T> itemBinding) {
this.itemBinding = itemBinding;
}

Expand All @@ -68,7 +68,7 @@ public void setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {

@NonNull
@Override
public ItemBinding<T> getItemBinding() {
public ItemBinding<? super T> getItemBinding() {
if (itemBinding == null) {
throw new NullPointerException("itemBinding == null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BindingRecyclerViewAdapters {
@SuppressWarnings("unchecked")
@BindingAdapter(value = {"itemBinding", "items", "adapter", "itemIds", "viewHolder", "diffConfig"}, requireAll = false)
public static <T> void setAdapter(RecyclerView recyclerView,
ItemBinding<T> itemBinding,
ItemBinding<? super T> itemBinding,
List<T> items,
BindingRecyclerViewAdapter<T> adapter,
BindingRecyclerViewAdapter.ItemIds<? super T> itemIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public interface BindingCollectionAdapter<T> {
/**
* Sets the item biding for the adapter.
*/
void setItemBinding(@NonNull ItemBinding<T> itemBinding);
void setItemBinding(@NonNull ItemBinding<? super T> itemBinding);

/**
* Returns the {@link ItemBinding} that the adapter that was set.
*
* @throws NullPointerException if the item binding was not set.
*/
@NonNull
ItemBinding<T> getItemBinding();
ItemBinding<? super T> getItemBinding();

/**
* Sets the adapter's items. These items will be displayed based on the {@link ItemBinding}. If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import androidx.annotation.LayoutRes;
import androidx.databinding.BindingAdapter;
import androidx.databinding.BindingConversion;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.viewpager.widget.ViewPager;

/**
Expand All @@ -20,7 +18,7 @@ public class BindingCollectionAdapters {
// AdapterView
@SuppressWarnings("unchecked")
@BindingAdapter(value = {"itemBinding", "itemTypeCount", "items", "adapter", "itemDropDownLayout", "itemIds", "itemIsEnabled"}, requireAll = false)
public static <T> void setAdapter(AdapterView adapterView, ItemBinding<T> itemBinding, Integer itemTypeCount, List items, BindingListViewAdapter<T> adapter, @LayoutRes int itemDropDownLayout, BindingListViewAdapter.ItemIds<? super T> itemIds, BindingListViewAdapter.ItemIsEnabled<? super T> itemIsEnabled) {
public static <T> void setAdapter(AdapterView adapterView, ItemBinding<? super T> itemBinding, Integer itemTypeCount, List items, BindingListViewAdapter<T> adapter, @LayoutRes int itemDropDownLayout, BindingListViewAdapter.ItemIds<? super T> itemIds, BindingListViewAdapter.ItemIsEnabled<? super T> itemIsEnabled) {
if (itemBinding == null) {
throw new IllegalArgumentException("onItemBind must not be null");
}
Expand Down Expand Up @@ -57,7 +55,7 @@ private static Adapter unwrapAdapter(Adapter adapter) {
// ViewPager
@SuppressWarnings("unchecked")
@BindingAdapter(value = {"itemBinding", "items", "adapter", "pageTitles"}, requireAll = false)
public static <T> void setAdapter(ViewPager viewPager, ItemBinding<T> itemBinding, List items, BindingViewPagerAdapter<T> adapter, BindingViewPagerAdapter.PageTitles<T> pageTitles) {
public static <T> void setAdapter(ViewPager viewPager, ItemBinding<? super T> itemBinding, List items, BindingViewPagerAdapter<T> adapter, BindingViewPagerAdapter.PageTitles<T> pageTitles) {
if (itemBinding == null) {
throw new IllegalArgumentException("onItemBind must not be null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class BindingListViewAdapter<T> extends BaseAdapter implements BindingCollectionAdapter<T> {
private final int itemTypeCount;
private ItemBinding<T> itemBinding;
private ItemBinding<? super T> itemBinding;
@LayoutRes
private int dropDownItemLayout;
private WeakReferenceOnListChangedCallback<T> callback;
Expand All @@ -45,7 +45,7 @@ public BindingListViewAdapter(int itemTypeCount) {
}

@Override
public void setItemBinding(@NonNull ItemBinding<T> itemBinding) {
public void setItemBinding(@NonNull ItemBinding<? super T> itemBinding) {
this.itemBinding = itemBinding;
}

Expand All @@ -61,7 +61,7 @@ public void setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {

@NonNull
@Override
public ItemBinding<T> getItemBinding() {
public ItemBinding<? super T> getItemBinding() {
if (itemBinding == null) {
throw new NullPointerException("itemBinding == null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* changes to that list.
*/
public class BindingViewPagerAdapter<T> extends PagerAdapter implements BindingCollectionAdapter<T> {
private ItemBinding<T> itemBinding;
private ItemBinding<? super T> itemBinding;
private WeakReferenceOnListChangedCallback<T> callback;
private List<T> items;
private LayoutInflater inflater;
Expand All @@ -35,7 +35,7 @@ public class BindingViewPagerAdapter<T> extends PagerAdapter implements BindingC
private List<View> views = new ArrayList<>();

@Override
public void setItemBinding(@NonNull ItemBinding<T> itemBinding) {
public void setItemBinding(@NonNull ItemBinding<? super T> itemBinding) {
this.itemBinding = itemBinding;
}

Expand All @@ -56,7 +56,7 @@ public void setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {

@NonNull
@Override
public ItemBinding<T> getItemBinding() {
public ItemBinding<? super T> getItemBinding() {
if (itemBinding == null) {
throw new NullPointerException("itemBinding == null");
}
Expand Down

0 comments on commit 7dfbeb4

Please sign in to comment.