001/* 002 Copyright 2010-2016 Boxfuse GmbH 003 <p/> 004 Licensed under the Apache License, Version 2.0 (the "License"); 005 you may not use this file except in compliance with the License. 006 You may obtain a copy of the License at 007 <p/> 008 http://www.apache.org/licenses/LICENSE-2.0 009 <p/> 010 Unless required by applicable law or agreed to in writing, software 011 distributed under the License is distributed on an "AS IS" BASIS, 012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 See the License for the specific language governing permissions and 014 limitations under the License. 015 */ 016package io.avaje.classpath.scanner.core; 017 018 019/** 020 * A starting location to scan from. 021 */ 022public final class Location { 023 024 /** 025 * The prefix for classpath locations. 026 */ 027 private static final String CLASSPATH_PREFIX = "classpath:"; 028 029 /** 030 * The prefix for filesystem locations. 031 */ 032 private static final String FILESYSTEM_PREFIX = "filesystem:"; 033 034 /** 035 * The prefix part of the location. Can be either classpath: or filesystem:. 036 */ 037 private final String prefix; 038 039 /** 040 * The path part of the location. 041 */ 042 private String path; 043 044 /** 045 * Creates a new location. 046 * 047 * @param descriptor The location descriptor. 048 */ 049 public Location(String descriptor) { 050 String normalizedDescriptor = descriptor.trim().replace("\\", "/"); 051 052 final int colonPos = normalizedDescriptor.indexOf(":"); 053 if (colonPos > -1) { 054 prefix = normalizedDescriptor.substring(0, colonPos + 1); 055 path = normalizedDescriptor.substring(colonPos + 1); 056 } else { 057 prefix = CLASSPATH_PREFIX; 058 path = normalizedDescriptor; 059 } 060 if (isClassPath()) { 061 path = path.replace(".", "/"); 062 if (path.startsWith("/")) { 063 path = path.substring(1); 064 } 065 } else if (!isFileSystem()) { 066 throw new IllegalStateException("Unknown prefix, should be either filesystem: or classpath: " + normalizedDescriptor); 067 } 068 if (path.endsWith("/")) { 069 path = path.substring(0, path.length() - 1); 070 } 071 } 072 073 /** 074 * Return true if this denotes a classpath location. 075 */ 076 public boolean isClassPath() { 077 return CLASSPATH_PREFIX.equals(prefix); 078 } 079 080 /** 081 * Return true if this denotes a filesystem location. 082 */ 083 public boolean isFileSystem() { 084 return FILESYSTEM_PREFIX.equals(prefix); 085 } 086 087 /** 088 * Return the path part of the location. 089 */ 090 public String path() { 091 return path; 092 } 093 094 /** 095 * Return the prefix denoting classpath of filesystem. 096 */ 097 public String prefix() { 098 return prefix; 099 } 100 101 /** 102 * Return the complete location descriptor. 103 */ 104 public String descriptor() { 105 return prefix + path; 106 } 107 108 @Override 109 public boolean equals(Object o) { 110 if (this == o) return true; 111 if (o == null || getClass() != o.getClass()) return false; 112 Location location = (Location) o; 113 return descriptor().equals(location.descriptor()); 114 } 115 116 @Override 117 public int hashCode() { 118 return descriptor().hashCode(); 119 } 120 121 @Override 122 public String toString() { 123 return descriptor(); 124 } 125}