1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
17 package net.sf.ehcache.config.generator;
18
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24
25 import net.sf.ehcache.config.generator.model.AbstractDepthFirstVisitor;
26 import net.sf.ehcache.config.generator.model.NodeAttribute;
27 import net.sf.ehcache.config.generator.model.NodeElement;
28
29 public class RememberingVisitor extends AbstractDepthFirstVisitor {
30 private final Set<NodeElement> visitedElements = new HashSet<NodeElement>();
31 private final ConcurrentMap<NodeElement, Set<NodeAttribute>> visitedAttributes = new ConcurrentHashMap<NodeElement, Set<NodeAttribute>>();
32
33 public Set<NodeElement> getVisitedElements() {
34 return visitedElements;
35 }
36
37 public ConcurrentMap<NodeElement, Set<NodeAttribute>> getVisitedAttributes() {
38 return visitedAttributes;
39 }
40
41 @Override
42 protected void visitElement(NodeElement element) {
43 visitedElements.add(element);
44 }
45
46 @Override
47 protected void visitAttributes(NodeElement element, List<NodeAttribute> attributes) {
48 getVisitedAttributesForElement(element).addAll(attributes);
49 }
50
51 private Set<NodeAttribute> getVisitedAttributesForElement(NodeElement element) {
52 Set<NodeAttribute> set = visitedAttributes.get(element);
53 if (set == null) {
54 set = new HashSet<NodeAttribute>();
55 Set<NodeAttribute> prev = visitedAttributes.putIfAbsent(element, set);
56 if (prev != null) {
57 set = prev;
58 }
59 }
60 return set;
61 }
62 }