DependencyManager.java

biz/hammurapi/config/DependencyManager.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 72:27
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 101:55
Java Inspector 089 Parameter slave is not documented 2 50:9
Java Inspector 089 Parameter master is not documented 2 50:9
Java Inspector 089 Parameter slave is not documented 2 67:9
Java Inspector 089 Parameter master is not documented 2 67:9
Java Inspector 089 Method return value is not properly documented 2 67:9
Java Inspector 089 Method is not properly documented 2 94:9
Java Inspector 089 Undocumented method 2 100:41
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 73:62

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.config;
24
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.Comparator;
28import java.util.HashMap;
29import java.util.HashSet;
30import java.util.Iterator;
31import java.util.List;
32import java.util.Map;
33import java.util.Set;
34
35import biz.hammurapi.eval.CircularReferenceException;
36
37/**
38 * Manages dependencies between objects.
39 * @author Pavel Vlasov
40 * @revision $Revision$
41 */
42public class DependencyManager {
43 private Map dependencyMap=new HashMap();
44
45 /**
46 * Adds dependency. Master can be null for independent objects.
47 * @param slave
48 * @param master
49 */
50 public void addDependency(Object slave, Object master) {
51 Set masters=(Set) dependencyMap.get(slave);
52 if (masters==null) {
53 masters=new HashSet();
54 dependencyMap.put(slave, masters);
55 }
56 if (master!=null) {
57 masters.add(master);
58 }
59 }
60
61 /**
62 * Finds out whether slave depends on master directly or indirectly
63 * @param slave
64 * @param master
65 * @return
66 */
67 public boolean isDependent(Object slave, Object master) {
68 return isDependent(slave, master, slave);
69 }
70
71 private boolean isDependent(Object slave, Object master, Object originalSlave) {
72 if (master==originalSlave) {
73 throw new CircularReferenceException("Circular reference : "+originalSlave);
74 }
75
76 Set masters=(Set) dependencyMap.get(slave);
77 if (masters!=null && masters.contains(master)) {
78 return true;
79 }
80
81 Iterator it=masters.iterator();
82 while (it.hasNext()) {
83 if (isDependent(it.next(), master, originalSlave)) {
84 return true;
85 }
86 }
87
88 return false;
89 }
90
91 /**
92 * @return List of items ordered by dependency - independent items first.
93 */
94 public List getOrdered() {
95 ArrayList ret=new ArrayList(dependencyMap.keySet());
96 Collections.sort(
97 ret,
98 new Comparator() {
99
100 public int compare(Object o1, Object o2) {
101 if (o1==o2) {
102 return 0;
103 }
104
105 if (isDependent(o1, o2)) {
106 return 1;
107 }
108
109 if (isDependent(o2,o1)) {
110 return -1;
111 }
112
113 return o1.hashCode()-o2.hashCode();
114 }
115
116 });
117 return ret;
118 }
119
120}
121