| 1 | /** |
| 2 | * Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.net |
| 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 | package org.deduced.viewer.web.server; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.deduced.AbstractPropertyCollection; |
| 22 | import org.deduced.PropertyCollection; |
| 23 | import org.deduced.RecursiveModelListener; |
| 24 | |
| 25 | /** |
| 26 | * PropertyCollectionIndex is used to monitor an object tree and index all the |
| 27 | * property collection it contains to find them for future reference. |
| 28 | * |
| 29 | * @author Steve McDuff |
| 30 | * |
| 31 | */ |
| 32 | public class PropertyCollectionIndex extends RecursiveModelListener |
| 33 | { |
| 34 | |
| 35 | /** |
| 36 | * index of the property collections where the key is the unique ID of the |
| 37 | * collection. |
| 38 | */ |
| 39 | private Map<Object, PropertyCollection<?, ?>> index = |
| 40 | new HashMap<Object, PropertyCollection<?, ?>>(); |
| 41 | |
| 42 | /** |
| 43 | * PropertyCollectionIndex constructor |
| 44 | */ |
| 45 | public PropertyCollectionIndex() |
| 46 | { |
| 47 | |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * get a Collection based on it's unique ID |
| 52 | * |
| 53 | * @param key the collection unique ID |
| 54 | * @return the matching collection. Null if this ID is unknown |
| 55 | */ |
| 56 | public PropertyCollection<?, ?> getCollection( |
| 57 | Object key) |
| 58 | { |
| 59 | return index.get(key); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * (non-JSDoc) |
| 64 | * |
| 65 | * @see org.deduced.RecursiveModelListener#addAllListeners(org.deduced.PropertyCollection) |
| 66 | */ |
| 67 | @Override |
| 68 | protected void addAllListeners( |
| 69 | PropertyCollection<?, ?> currentCollection) |
| 70 | { |
| 71 | Object key = |
| 72 | AbstractPropertyCollection |
| 73 | .getInstanceIdentityKey(currentCollection); |
| 74 | index.put(key, currentCollection); |
| 75 | super.addAllListeners(currentCollection); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * (non-JSDoc) |
| 80 | * |
| 81 | * @see org.deduced.RecursiveModelListener#removeAllListeners(org.deduced.PropertyCollection) |
| 82 | */ |
| 83 | @Override |
| 84 | protected void removeAllListeners( |
| 85 | PropertyCollection<?, ?> currentCollection) |
| 86 | { |
| 87 | Object key = |
| 88 | AbstractPropertyCollection |
| 89 | .getInstanceIdentityKey(currentCollection); |
| 90 | index.remove(key); |
| 91 | super.removeAllListeners(currentCollection); |
| 92 | } |
| 93 | |
| 94 | } |