1 package com.atlassian.pageobjects;
2
3 import com.atlassian.annotations.PublicApi;
4 import com.google.common.base.Function;
5 import com.google.common.collect.Iterables;
6 import com.google.common.collect.ObjectArrays;
7
8 import javax.annotation.Nonnull;
9 import javax.annotation.Nullable;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 /**
14 * Predicates, functions and other utilities for use with {@link PageBinder} and other page-object-related APIs.
15 *
16 * @since 2.3
17 */
18 @PublicApi
19 public final class PageObjects
20 {
21 private PageObjects()
22 {
23 throw new AssertionError("Do not instantiate " + getClass().getSimpleName());
24 }
25
26 /**
27 * Version of {@link #bindTo(PageBinder, Class, Class, Object...)} for where there's no explicit input type.
28 *
29 * @param binder page binder
30 * @param pageObjectClass target page object class
31 * @param extraArguments extra arguments to use when binding each page object
32 * @param <E> input type
33 * @param <PO> wrapping object type
34 *
35 * @return page binding function
36 *
37 * @see #bindTo(PageBinder, Class, Object...)
38 */
39 @Nonnull
40 public static <E, PO> Function<E, PO> bindTo(@Nonnull final PageBinder binder,
41 @Nonnull final Class<PO> pageObjectClass,
42 @Nonnull final Object... extraArguments)
43 {
44 return bindTo(binder, null, pageObjectClass, extraArguments);
45 }
46
47 /**
48 * Binds 'wrapper' page objects that take one constructor parameter (and optionally any number of "fixed"
49 * {@code extraArguments}. Most useful for simple page objects wrapping page elements, e.g. table rows.
50 *
51 * <p/>
52 * Note: the wrapping type needs to have a constructor that accepts a single instance of the input type and all
53 * the extra parameters as provided by {@code extraArguments}.
54 *
55 * @param binder page binder
56 * @param inputType source class to use this method more conveniently
57 * @param pageObjectClass target page object class
58 * @param extraArguments extra arguments to use when binding each page object
59 * @param <E> input type
60 * @param <PO> wrapping object type
61 *
62 * @return page binding function
63 *
64 * @see PageBinder#bind(Class, Object...)
65 */
66 @Nonnull
67 public static <E, PO> Function<E, PO> bindTo(@Nonnull final PageBinder binder,
68 @Nullable final Class<E> inputType,
69 @Nonnull final Class<PO> pageObjectClass,
70 @Nonnull final Object... extraArguments)
71 {
72 checkNotNull(binder, "binder");
73 checkNotNull(pageObjectClass, "pageObjectClass");
74 checkNotNull(extraArguments, "extraArguments");
75
76 return new Function<E, PO>()
77 {
78 @Override
79 public PO apply(E element)
80 {
81 return binder.bind(pageObjectClass, ObjectArrays.concat(element, extraArguments));
82 }
83 };
84 }
85
86 /**
87 * Transforms a list of objects into a list of page objects wrapping those objects. Most commonly the input list
88 * will contain page elements of some sort.
89 *
90 * @param binder page binder
91 * @param pageElements a list of page elements to transform
92 * @param pageObjectClass target page object class
93 * @param extraArguments extra arguments to use when binding each page object
94 * @param <E> input object type
95 * @param <EE> wrapping object type
96 *
97 * @return a list of page objects wrapping the inputs
98 *
99 * @see #bindTo(PageBinder, Class, Object...)
100 */
101 @Nonnull
102 public static <E, EE> Iterable<EE> bind(@Nonnull PageBinder binder,
103 @Nonnull Iterable<E> pageElements,
104 @Nonnull Class<EE> pageObjectClass,
105 @Nonnull final Object... extraArguments)
106 {
107 return Iterables.transform(pageElements, bindTo(binder, pageObjectClass, extraArguments));
108 }
109 }