01: /**********************************************************************
02: Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: ...
18: **********************************************************************/package org.jpox.state;
19:
20: import java.util.ArrayList;
21: import java.util.List;
22: import java.util.ListIterator;
23:
24: /**
25: * Holder for the state control for FetchPlan processing.
26: * Maintains a list of the field names being fetched. The first item in the List will be the root.
27: * When a new branch of the graph of processed the field name is added, and is removed when it is
28: * processed. This provides a means of always knowing the depth in the current graph, and also of
29: * allowing detection of recursion of field names.
30: *
31: * @version $Revision: 1.6 $
32: */
33: public class FetchPlanState {
34: /**
35: * List of field names in the graph. The first is the root of the tree, and fields are added as they are encountered
36: * and removed when they are finished with.
37: */
38: protected List fetchFieldNames = new ArrayList();
39:
40: /**
41: * Method to add an field name to the list since it is being processed
42: * @param field The field to add
43: */
44: public void addFieldName(String field) {
45: fetchFieldNames.add(field);
46: }
47:
48: /**
49: * Method to remove the latest field name from the list since it is now processed
50: */
51: public void removeLatestFieldName() {
52: fetchFieldNames.remove(fetchFieldNames.size() - 1);
53: }
54:
55: /**
56: * Accessor for the object graph depth currently
57: * @return The graph depth
58: */
59: public int getCurrentFetchDepth() {
60: return fetchFieldNames.size();
61: }
62:
63: /**
64: * Accessor for the current depth for the specified field name
65: * @param fieldName The name of the field
66: * @return The depth for this field name
67: */
68: public int getObjectDepthForType(String fieldName) {
69: ListIterator iter = fetchFieldNames
70: .listIterator(fetchFieldNames.size()); // Start at the end
71: int number = 0;
72: while (iter.hasPrevious()) {
73: String field = (String) iter.previous();
74: if (field.equals(fieldName)) {
75: number++;
76: } else {
77: break;
78: }
79: }
80: return number;
81: }
82: }
|