1/*
2 * Copyright (C) 2013 DroidDriver committers
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.android.droiddriver.finders;
18
19import com.google.android.droiddriver.UiElement;
20import com.google.android.droiddriver.util.Preconditions;
21
22/**
23 * Finds UiElement by applying Finders in turn: using the UiElement returned by
24 * first Finder as context for the second Finder. It is conceptually similar to
25 * <a href="http://en.wikipedia.org/wiki/Functional_composition">Function
26 * composition</a>. The returned UiElement can be thought of as the result of
27 * second(first(context)).
28 * <p>
29 * Note typically first Finder finds the ancestor, then second Finder finds the
30 * target UiElement, which is a descendant. ChainFinder can be chained with
31 * additional Finders to make a "chain".
32 */
33public class ChainFinder implements Finder {
34  private final Finder first;
35  private final Finder second;
36
37  protected ChainFinder(Finder first, Finder second) {
38    this.first = Preconditions.checkNotNull(first);
39    this.second = Preconditions.checkNotNull(second);
40  }
41
42  @Override
43  public String toString() {
44    return String.format("Chain{%s, %s}", first, second);
45  }
46
47  @Override
48  public UiElement find(UiElement context) {
49    return second.find(first.find(context));
50  }
51}
52