01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.controls.runtime.generator;
20:
21: import java.util.HashMap;
22: import java.util.Collection;
23:
24: /**
25: * The AptMethodSet method represents a collection of AptMethod objects. It contains special
26: * support for method overloading, to ensure that overloaded method objects contained within
27: * the set will each have a unique index value.
28: *
29: * @see org.apache.beehive.controls.runtime.generator.AptMethod#setIndex
30: */
31: public class AptMethodSet<T extends AptMethod> {
32: /**
33: * Adds a new method to the list. Also detects overloaded methods and ensures that they
34: * will receive a unique index value.
35: */
36: public void add(T method) {
37: // check for overridden method
38: if (isOverrideMethod(method)) {
39: return;
40: }
41:
42: // Add to the list of managed methods
43: _methods.put(method.getName() + method.getArgTypes(), method);
44:
45: // Ensure that all added methods have a unique index
46: Object nameValue = _nameMap.get(method.getName());
47: if (nameValue == null) {
48: // first method with this name, considered unique (for now)
49: _nameMap.put(method.getName(), method);
50: } else {
51: int nextIndex;
52: if (nameValue instanceof AptMethod) {
53: // 2nd method with this name, add index to original and start indexing
54: ((AptMethod) nameValue).setIndex(0);
55: nextIndex = 1;
56: } else {
57: // 3rd (or later) method with this name, continue indexing
58: nextIndex = ((Integer) nameValue).intValue();
59: }
60:
61: method.setIndex(nextIndex++);
62: _nameMap.put(method.getName(), nextIndex);
63: }
64: }
65:
66: /**
67: * Get the collection of methods in this set.
68: */
69: public Collection<T> getMethods() {
70: return _methods.values();
71: }
72:
73: /**
74: * Get the number of methods in this set.
75: */
76: public int size() {
77: return _methods.size();
78: }
79:
80: /**
81: * Determine of the method overrides a previously added method.
82: * @param method Method to check.
83: * @return true if method is an override to an existing method and does not need to be added
84: * to this method set.
85: */
86: private boolean isOverrideMethod(T method) {
87: return _methods.containsKey(method.getName()
88: + method.getArgTypes());
89: }
90:
91: private HashMap _nameMap = new HashMap(); // method name -> a single (unique) AptMethod or next index
92: private HashMap<String, T> _methods = new HashMap<String, T>(); // method name + arg types -> AptMethod
93: }
|