| /*=============================================================================# |
| # Copyright (c) 2018, 2020 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.rj.server.util; |
| |
| import java.net.URISyntaxException; |
| import java.nio.file.Files; |
| import java.nio.file.Path; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| import org.eclipse.statet.jcommons.runtime.UriUtils; |
| |
| |
| @NonNullByDefault |
| public class PathEntry implements Comparable<PathEntry> { |
| |
| |
| public static class Jar extends PathEntry { |
| |
| |
| public Jar(final String bundleId, final Path path) { |
| super(bundleId, path); |
| } |
| |
| |
| @Override |
| public String getUrlString() { |
| try { |
| return UriUtils.toJarUrlString(super.getUrlString()); |
| } |
| catch (final URISyntaxException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| |
| @Override |
| public @Nullable Path getResourcePath(final String resource) { |
| return null; |
| } |
| |
| } |
| |
| |
| protected final String bundleId; |
| |
| private final Path path; |
| |
| |
| public PathEntry(final String bundleId, final Path path) { |
| this.bundleId= bundleId; |
| this.path= path; |
| } |
| |
| |
| public String getBundleId() { |
| return this.bundleId; |
| } |
| |
| @Override |
| public int compareTo(final PathEntry o) { |
| return this.bundleId.compareTo((o).bundleId); |
| } |
| |
| protected Path getPath() { |
| return this.path; |
| } |
| |
| public String getUrlString() { |
| return this.path.toUri().toString(); |
| } |
| |
| public String getJClassPathString() { |
| return this.path.toString(); |
| } |
| |
| public String getCodebaseString() { |
| return this.path.toUri().toString(); |
| } |
| |
| |
| public @Nullable Path getResourcePath(final String resource) { |
| final Path path= this.path.resolve(resource); |
| if (Files.exists(path)) { |
| return path; |
| } |
| return null; |
| } |
| |
| |
| @Override |
| public int hashCode() { |
| return this.bundleId.hashCode() + getPath().hashCode(); |
| } |
| |
| @Override |
| public boolean equals(final @Nullable Object obj) { |
| if (this == obj) { |
| return true; |
| } |
| if (obj instanceof PathEntry) { |
| final PathEntry other= (PathEntry) obj; |
| return (this.bundleId.equals(other.bundleId) |
| && getPath().equals(other.getPath()) ); |
| } |
| return super.equals(obj); |
| } |
| |
| @Override |
| public String toString() { |
| return String.format("'%1$s' -> '%2$s'", this.bundleId, getUrlString()); //$NON-NLS-1$ |
| } |
| |
| } |