/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.p4java.extension.path.internal;
import java.util.Set;
import com.perforce.p4java.extension.path.PathStringFormat;
import com.perforce.p4java.extension.path.PerforcePath;
import com.perforce.p4java.extension.utility.Validate;
/**
* @since 2012.1
*/
public class PerforcePathImpl implements PerforcePath {
/** path without escape nor unescape */
private final String basePath;
private final String revisionString;
/** file name without escape nor unescape */
private final String fileName;
/**
* @param basePath
* the non-null full depot path to ultimate folder, not including
* any file name or terminal wildcarding.
* @param fileName
* the filename or terminal wildcarding. Ignored if null.
* @param revisionString
* the "#" prefixed revision string or query parameters. Ignored
* if null.
*/
public PerforcePathImpl(String basePath, String fileName,
String revisionString) {
Validate.notNull(basePath,
"basePath must not be null to PerforcePathImpl");
if (basePath.endsWith("/"))
this.basePath = unescape(basePath);
else
this.basePath = unescape(basePath) + "/";
this.fileName = fileName == null ? "" : unescape(fileName);
this.revisionString = revisionString == null ? "" : revisionString;
}
/**
* Construct a new instance based on existing instance
*
* @param path
* @param revision
*/
public PerforcePathImpl(PerforcePath path, String revision) {
this.basePath = path.getBasePath();
this.fileName = path.getFileName();
this.revisionString = revision == null ? "" : revision;
}
/**
* @see com.perforce.p4java.extension.path.PerforcePath#asString(java.util.Set)
*/
public String asString(Set<PathStringFormat> modifiers) {
if (modifiers == null)
return basePath + fileName + revisionString;
String path = basePath + fileName;
boolean containsEnquote = false;
for (PathStringFormat modifier : modifiers) {
if (modifier == PathStringFormat.ENQUOTE) {
containsEnquote = true;
continue;
}
path = modifier.modify(path);
}
path = path + revisionString;
if (containsEnquote)
path = PathStringFormat.ENQUOTE.modify(path);
return path;
}
/**
* @see com.perforce.p4java.extension.path.PerforcePath#asRevisionlessString(java.util.Set)
*/
public String asRevisionlessString(Set<PathStringFormat> modifiers) {
return applyModifiers(getRevisionlessPath(), modifiers);
}
private String getRevisionlessPath() {
return basePath + fileName;
}
private String applyModifiers(String path, Set<PathStringFormat> modifiers) {
if (modifiers == null)
return path;
for (PathStringFormat modifier : modifiers)
path = modifier.modify(path);
return path;
}
/**
* @see com.perforce.p4java.extension.path.PerforcePath#getBasePath()
*/
public String getBasePath() {
return basePath;
}
/**
* @see com.perforce.p4java.extension.path.PerforcePath#getRevisionString()
*/
public String getRevisionString() {
return revisionString;
}
/**
* @see com.perforce.p4java.extension.path.PerforcePath#getFileName()
*/
public String getFileName() {
return fileName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((basePath == null) ? 0 : basePath.hashCode());
result = prime * result
+ ((fileName == null) ? 0 : fileName.hashCode());
result = prime * result
+ ((revisionString == null) ? 0 : revisionString.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PerforcePathImpl other = (PerforcePathImpl) obj;
if (basePath == null) {
if (other.basePath != null)
return false;
} else if (!basePath.equals(other.basePath))
return false;
if (fileName == null) {
if (other.fileName != null)
return false;
} else if (!fileName.equals(other.fileName))
return false;
if (revisionString == null) {
if (other.revisionString != null)
return false;
} else if (!revisionString.equals(other.revisionString))
return false;
return true;
}
private String unescape(String path) {
return PathStringFormat.UNESCAPE.modify(path);
}
@Override
public String toString() {
return asString(null);
}
}