001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.io.File;
020    import java.io.Serializable;
021    
022    import java.nio.file.Path;
023    import java.nio.file.Paths;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class PathHolder implements Serializable {
029    
030            public PathHolder(Path path) {
031                    this(path.toString());
032            }
033    
034            public PathHolder(String pathString) {
035                    _pathString = pathString;
036            }
037    
038            @Override
039            public boolean equals(Object obj) {
040                    if (this == obj) {
041                            return true;
042                    }
043    
044                    if (!(obj instanceof PathHolder)) {
045                            return false;
046                    }
047    
048                    PathHolder pathHolder = (PathHolder)obj;
049    
050                    if (Validator.equals(toString(), pathHolder.toString())) {
051                            return true;
052                    }
053    
054                    return false;
055            }
056    
057            public Path getPath() {
058                    return Paths.get(toString());
059            }
060    
061            @Override
062            public int hashCode() {
063                    String toString = toString();
064    
065                    return toString.hashCode();
066            }
067    
068            @Override
069            public String toString() {
070                    if (_toString != null) {
071                            return _toString;
072                    }
073    
074                    if (_separatorChar == File.separatorChar) {
075                            _toString = _pathString;
076                    }
077                    else {
078                            _toString = _pathString.replace(_separatorChar, File.separatorChar);
079                    }
080    
081                    return _toString;
082            }
083    
084            private static final long serialVersionUID = 1L;
085    
086            private final String _pathString;
087            private final char _separatorChar = File.separatorChar;
088            private transient String _toString;
089    
090    }