⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 path.java

📁 jawe的最新版本,基于Java的图形化工作流编辑器。图形化工作流编辑器 。使用JAVA语言开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      if (device != null)         length += device.length();      if ((separators & HAS_LEADING) != 0)         length ++;      if ((separators & IS_UNC) != 0)         length++;      //add the segment lengths      int max = segments.length;      if (max > 0) {         for (int i = 0; i < max; i++) {            length += segments[i].length();         }         //add the separator lengths         length += max-1;      }      if ((separators & HAS_TRAILING) != 0)         length++;      return length;   }   /*   * Returns the number of segments in the given path   */   private int computeSegmentCount(String path) {      int len = path.length();      if (len == 0 || (len == 1 && path.charAt(0) == SEPARATOR)) {         return 0;      }      int count = 1;      int prev = -1;      int i;      while ((i = path.indexOf(SEPARATOR, prev + 1)) != -1) {         if (i != prev + 1 && i != len) {            ++count;         }         prev = i;      }      if (path.charAt(len - 1) == SEPARATOR) {         --count;      }      return count;   }   /**    * Computes the segment array for the given canonicalized path.    */   private String[] computeSegments(String path) {      // performance sensitive --- avoid creating garbage      int segmentCount = computeSegmentCount(path);      if (segmentCount == 0)         return NO_SEGMENTS;      String[] newSegments = new String[segmentCount];      int len = path.length();      // check for initial slash      int firstPosition = (path.charAt(0) == SEPARATOR) ? 1 : 0;      // check for UNC      if (firstPosition == 1 && len > 1 && (path.charAt(1) == SEPARATOR))         firstPosition = 2;      int lastPosition = (path.charAt(len - 1) != SEPARATOR) ? len - 1 : len - 2;      // for non-empty paths, the number of segments is      // the number of slashes plus 1, ignoring any leading      // and trailing slashes      int next = firstPosition;      for (int i = 0; i < segmentCount; i++) {         int start = next;         int end = path.indexOf(SEPARATOR, next);         if (end == -1) {            newSegments[i] = path.substring(start, lastPosition + 1);         } else {            newSegments[i] = path.substring(start, end);         }         next = end + 1;      }      return newSegments;   }   /*   * Initialize the current path with the given string.   */   private void initialize(String pDevice, String fullPath) {      if (fullPath==null) throw new RuntimeException();      this.device = pDevice;      //indexOf is much faster than replace      String path = fullPath.indexOf('\\') == -1 ? fullPath : fullPath.replace('\\', SEPARATOR);      int i = path.indexOf(DEVICE_SEPARATOR);      if (i != -1) {         // if the specified device is null then set it to         // be whatever is defined in the path string         if (pDevice == null)            this.device = path.substring(0, i + 1);         path = path.substring(i + 1, path.length());      }      path = collapseSlashes(path);      int len = path.length();      //compute the separators array      if (len < 2) {         if (len == 1 && path.charAt(0) == SEPARATOR) {            this.separators = HAS_LEADING;         } else {            this.separators = 0;         }      } else {         boolean hasLeading = path.charAt(0) == SEPARATOR;         boolean isUNC = hasLeading && path.charAt(1) == SEPARATOR;         //UNC path of length two has no trailing separator         boolean hasTrailing = !(isUNC && len == 2) && path.charAt(len-1) == SEPARATOR;         separators = hasLeading ? HAS_LEADING : 0;         if (isUNC) separators |= IS_UNC;         if (hasTrailing) separators |= HAS_TRAILING;      }      //compute segments and ensure canonical form      segments = computeSegments(path);      if (!canonicalize()) {         //compute hash now because canonicalize didn't need to do it         separators = (separators & ALL_SEPARATORS) | (computeHashCode() << 3);      }   }   public boolean isAbsolute() {      //it's absolute if it has a leading separator      return (separators & HAS_LEADING) != 0;   }   public int matchingFirstSegments(Path anotherPath) {      if (anotherPath==null) throw new RuntimeException();      int anotherPathLen = anotherPath.segmentCount();      int max = Math.min(segments.length, anotherPathLen);      int count = 0;      for (int i = 0; i < max; i++) {         if (!segments[i].equals(anotherPath.segment(i))) {            return count;         }         count++;      }      return count;   }   public String segment(int index) {      if (index >= segments.length)         return null;      return segments[index];   }   public int segmentCount() {      return segments.length;   }   public String[] segments() {      String[] segmentCopy = new String[segments.length];      System.arraycopy(segments, 0, segmentCopy, 0, segments.length);      return segmentCopy;   }   public Path setDevice(String value) {      if (value != null) {         //Assert.isTrue(value.indexOf(Path.DEVICE_SEPARATOR) == (value.length() - 1), "Last character should be the device separator"); //$NON-NLS-1$         if (value.indexOf(Path.DEVICE_SEPARATOR) != (value.length() - 1)) throw new RuntimeException("Last character should be the device separator");      }      //return the reciever if the device is the same      if (value == device || (value != null && value.equals(device)))         return this;      return new Path(value, segments, separators);   }   public String toOSString() {      //Note that this method is identical to toString except      //it uses the OS file separator instead of the path separator      int resultSize = computeLength();      if (resultSize <= 0)         return EMPTY_STRING;      char FILE_SEPARATOR = File.separatorChar;      char[] result = new char[resultSize];      int offset = 0;      if (device != null) {         int size = device.length();         device.getChars(0, size, result, offset);         offset += size;      }      if ((separators & HAS_LEADING) != 0)         result[offset++] = FILE_SEPARATOR;      if ((separators & IS_UNC) != 0)         result[offset++] = FILE_SEPARATOR;      int len = segments.length-1;      if (len>=0) {         //append all but the last segment, with separators         for (int i = 0; i < len; i++) {            int size = segments[i].length();            segments[i].getChars(0, size, result, offset);            offset += size;            result[offset++] = FILE_SEPARATOR;         }         //append the last segment         int size = segments[len].length();         segments[len].getChars(0, size, result, offset);         offset += size;      }      if ((separators & HAS_TRAILING) != 0)         result[offset++] = FILE_SEPARATOR;      return new String(result);   }   public static String getRelativePath(Path fullPath,Path fBasePath) {      if (fBasePath == null || !hasSameDevice(fullPath, fBasePath)) {         return fullPath.toOSString();      }      int matchingSegments= fBasePath.matchingFirstSegments(fullPath);      StringBuffer res= new StringBuffer();      int backSegments= fBasePath.segmentCount() - matchingSegments;      while (backSegments > 0) {         res.append(".."); //$NON-NLS-1$         res.append(File.separatorChar);         backSegments--;      }      int segCount= fullPath.segmentCount();      for (int i= matchingSegments; i < segCount; i++) {         if (i > matchingSegments) {            res.append(File.separatorChar);         }         res.append(fullPath.segment(i));      }      return res.toString();   }   private static boolean hasSameDevice(Path p1, Path p2) {      String dev= p1.device;      if (dev == null) {         return p2.device == null;      }      return dev.equals(p2.device);   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -