View Javadoc

1   // Copyright (C) 2007 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.engine.common;
23  
24  import java.io.File;
25  import java.io.Serializable;
26  
27  import net.grinder.util.Directory;
28  
29  
30  /**
31   * Pairing of a script file and its root directory. The directory is not
32   * necessarily the immediate parent of the file.
33   *
34   * @author Philip Aston
35   */
36  public final class ScriptLocation implements Serializable {
37    private static final long serialVersionUID = 771173195260716872L;
38  
39    private final Directory m_directory;
40    private final File m_shortFile;
41    private final File m_absoluteFile;
42  
43    /**
44     * Constructor.
45     *
46     * @param directory
47     *            Script working directory. May be relative (to the CWD).
48     * @param file
49     *            The script file. May be relative (to {@code directory}).
50     *            If absolute, it needn't be below the root directory.
51     */
52    public ScriptLocation(final Directory directory, final File file) {
53  
54      m_directory = directory;
55  
56      // Try to shorten the name.
57      final File relativeFile = directory.relativeFile(file, false);
58  
59      if (relativeFile != null) {
60        m_shortFile = relativeFile;
61      }
62      else {
63        m_shortFile = file;
64      }
65  
66      if (file.isAbsolute()) {
67        m_absoluteFile = file;
68      }
69      else {
70        m_absoluteFile = directory.getFile(file);
71      }
72    }
73  
74    /**
75     * Constructor, based on the current working directory.
76     *
77     * @param file
78     *            The script file.
79     * @throws EngineException
80     *    If a file operation failed.
81     */
82    public ScriptLocation(final File file) throws EngineException {
83      this(new Directory(), file);
84    }
85  
86    /**
87     * Accessor for the script working directory.
88     *
89     * @return The directory.
90     */
91    public Directory getDirectory() {
92      return m_directory;
93    }
94  
95    /**
96     * Accessor for the script file. The returned <code>File</code> always
97     * represents an absolute path.
98     *
99     * @return The file.
100    */
101   public File getFile() {
102     return m_absoluteFile;
103   }
104 
105   /**
106    * String representation.
107    *
108    * @return The string.
109    */
110   @Override
111   public String toString() {
112     return m_shortFile.getPath();
113   }
114 
115   /**
116    * Hash code.
117    *
118    * @return The hash code.
119    */
120   @Override
121   public int hashCode() {
122     return getDirectory().hashCode() ^ getFile().hashCode();
123   }
124 
125   /**
126    * Equality.
127    *
128    * @param other Object to compare.
129    * @return {@code true} if and only if we're equal to {@code other}.
130    */
131   @Override
132   public boolean equals(final Object other) {
133     if (this == other) {
134       return true;
135     }
136 
137     if (other == null || other.getClass() != ScriptLocation.class) {
138       return false;
139     }
140 
141     final ScriptLocation otherScriptLocation = (ScriptLocation)other;
142 
143     return getDirectory().equals(otherScriptLocation.getDirectory()) &&
144            getFile().equals(otherScriptLocation.getFile());
145   }
146 }