View Javadoc

1   package org.lcsim.detector;
2   
3   import java.util.ArrayList;
4   
5   /**
6    * An ordered list of PhysicalVolume objects
7    * corresponding to a unique path in a 
8    * geometry of LogicalVolumes and
9    * PhysicalVolumes.  The first volume
10   * should be the world volume.
11   * 
12   * @author Jeremy McCormick <jeremym@slac.stanford.edu>
13   *
14   */
15  public class PhysicalVolumePath 
16  extends ArrayList<IPhysicalVolume>
17  implements IPhysicalVolumePath
18  {
19  	/**
20  	 * Get the top or first volume which is the world volume.
21  	 * @return The top volume.
22  	 */
23  	public IPhysicalVolume getTopVolume()
24  	{
25  		if (size()>0)
26  		{
27  			return this.get(0);
28  		}
29  		else {
30  			return null;
31  		}
32  	}	
33  	
34  	/**
35  	 * Get the bottom or last volume which is the leaf in this path.
36  	 * @return The leaf volume.
37  	 */
38  	public IPhysicalVolume getLeafVolume()
39  	{
40  		if (size()>0)
41  		{
42  			return this.get(size()-1);
43  		}
44  		else {
45  			return null;
46  		}
47  	}
48  	
49  	/**
50  	 * True if this IPhysicalVolumePath contains no PhysicalVolumes.
51  	 * @return True if empty.
52  	 */
53  	public boolean isEmpty()
54  	{
55  		return size()==0;
56  	}
57  
58  	/**
59  	 * Compare with another IPhysicalVolumePath.
60  	 * 
61  	 * False if path is null, if path is not the same
62  	 * size as this one, or if any of the PhysicalVolumes
63  	 * are different PhysicalVolume objects.
64  	 * 
65  	 * @param path
66  	 * @return True if the the paths are equal.
67  	 */
68  	public boolean equals(IPhysicalVolumePath path)
69  	{
70  		if (path == null)
71  		{
72  			return false;
73  		}
74  		
75  		if (path.size() != this.size())
76  		{
77  			return false;
78  		}
79  		
80  		for (int i=0; i<this.size(); i++)
81  		{
82  			if (this.get(i) != path.get(i))
83  			{
84  				return false;
85  			}
86  		}
87  		
88  		return true;
89  	}
90  	
91  	/**
92  	 * Compare the smallest prefix set of PhysicalVolumes together. 
93       * 
94       * For instance, these two IDs would return true.
95       * 
96       * <code>
97       * ExpandedIdentifier id1 = ExpandedIdentifier("/1/2/3");
98       * ExpandedIdentifier id2 = ExpandedIdentifier("/1/2/");
99       * </code>
100 	 * 
101 	 * @param path
102 	 * @return True if the set of prefix fields is equal.
103 	 */
104 	public boolean equalsPrefix(IPhysicalVolumePath path)
105 	{		
106 		if (path == null)
107 		{
108 			return false;
109 		}
110 		
111 		if (size() != 0 && path.size() == 0)
112 		{
113 			return false;
114 		}
115 		
116 		for (int i = (size() <= path.size() ? size() : path.size() ); i<size(); i++)
117 		{
118 			if (get(i) != path.get(i))
119 			{
120 				return false;
121 			}
122 		}
123 		return true;
124 	}
125 	
126 	public String toString()
127 	{		
128 		StringBuffer sb = new StringBuffer();
129 
130 		if (getTopVolume() != null)
131 		{			
132 			if (size() == 1)
133 			{
134 				sb.append("/");				
135 			}
136 			else {
137 				for (int i=1; i<size(); i++)
138 				{
139 					sb.append("/"+get(i).getName());
140 				}
141 			}
142 		}
143 		else {
144 			sb.append("");
145 		}				
146 				
147 		return sb.toString();
148 	}
149 }