Capture.java revision 47d431f63a66505a645f282416659a9758a91f1c
1/*
2 * Copyright 2003-2009 OFFIS, Henri Tremblay
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 */
16package org.easymock;
17
18import java.io.Serializable;
19import java.util.ArrayList;
20import java.util.List;
21
22/**
23 * Will contain what was captured by the <code>capture()</code> matcher. Knows
24 * if something was captured or not (allows to capture a null value).
25 *
26 * @param <T>
27 *            Type of the captured element
28 */
29public class Capture<T> implements Serializable {
30
31    private static final long serialVersionUID = -4214363692271370781L;
32
33    private CaptureType type;
34
35    private final List<T> values = new ArrayList<T>(2);
36
37    /**
38     * Default constructor. Only the last element will be captured
39     */
40    public Capture() {
41        this(CaptureType.LAST);
42    }
43
44    /**
45     * Constructor allowing to select the capture type
46     *
47     * @param type
48     *            capture type
49     */
50    public Capture(CaptureType type) {
51        this.type = type;
52    }
53
54    /**
55     * Will reset capture to a "nothing captured yet" state
56     */
57    public void reset() {
58        values.clear();
59    }
60
61    /**
62     * @return true if something was captured
63     */
64    public boolean hasCaptured() {
65        return !values.isEmpty();
66    }
67
68    /**
69     * Return captured value
70     *
71     * @throws AssertionError
72     *             if nothing was captured yet or if more than one value was
73     *             captured
74     * @return The last captured value
75     */
76    public T getValue() {
77        if (values.isEmpty()) {
78            throw new AssertionError("Nothing captured yet");
79        }
80        if (values.size() > 1) {
81            throw new AssertionError("More than one value captured: "
82                    + getValues());
83        }
84        return values.get(values.size() - 1);
85    }
86
87    /**
88     * Return all captured values. It returns the actual list so you can modify
89     * it's content if needed
90     *
91     * @return The currently captured values
92     */
93    public List<T> getValues() {
94        return values;
95    }
96
97    /**
98     * Used internally by the EasyMock framework to add a new captured value
99     *
100     * @param value
101     *            Value captured
102     */
103    public void setValue(T value) {
104        switch (type) {
105        case NONE:
106            break;
107        case ALL:
108            values.add(value);
109            break;
110        case FIRST:
111            if (!hasCaptured()) {
112                values.add(value);
113            }
114            break;
115        case LAST:
116            if (hasCaptured()) {
117                reset();
118            }
119            values.add(value);
120            break;
121        // ///CLOVER:OFF
122        default:
123            throw new IllegalArgumentException("Unknown capture type: " + type);
124        // ///CLOVER:ON
125        }
126    }
127
128    @Override
129    public String toString() {
130        if (values.isEmpty()) {
131            return "Nothing captured yet";
132        }
133        if (values.size() == 1) {
134            return String.valueOf(values.get(0));
135        }
136        return values.toString();
137    }
138}
139