1/*
2 * Copyright (C) 2007 The Android Open Source Project
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 tests.api.org.xml.sax.support;
18
19import java.util.ArrayList;
20import java.util.List;
21
22/**
23 * A simple helper class that logs method calls by storing method names and
24 * parameter lists. Used as a foundation for various simple SAX handlers.
25 */
26public class MethodLogger {
27
28    /**
29     * The names of the invoked methods, in order.
30     */
31    private List<String> methods = new ArrayList<String>();
32
33    /**
34     * The parameter lists of the invoked methods, in order.
35     */
36    private List<Object[]> argLists = new ArrayList<Object[]>();
37
38    /**
39     * Adds a method call with a variable list of arguments.
40     */
41    public void add(String method, Object ... args) {
42        Object[] argsCopy = new Object[args.length];
43        System.arraycopy(args, 0, argsCopy, 0, args.length);
44
45        methods.add(method);
46        argLists.add(argsCopy);
47    }
48
49    /**
50     * Returns the number of method invoked so far.
51     */
52    public int size() {
53        return methods.size();
54    }
55
56    /**
57     * Returns the method name stored at the given index.
58     */
59    public String getMethod(int index) {
60        return methods.get(index);
61    }
62
63    /**
64     * Returns the name of the last method that was invoked. Returns null if no
65     * method calls have been logged so far.
66     */
67    public String getMethod() {
68        return (size() == 0 ? null : getMethod(size() - 1));
69    }
70
71    /**
72     * Returns the argument array stored at the given index. May be empty, but
73     * not null.
74     */
75    public Object[] getArgs(int index) {
76        return argLists.get(index);
77    }
78
79    /**
80     * Returns the argument array of the last method that was invoked. Returns
81     * null if no method has been invoked so far.
82     */
83    public Object[] getArgs() {
84        return (size() == 0 ? null : getArgs(size() - 1));
85    }
86
87    /**
88     * Clears the log.
89     */
90    public void clear() {
91        methods.clear();
92        argLists.clear();
93    }
94
95}
96