PatternSyntaxException类(java JDK源码记录)

  1  *
  2  *
  3  *
  4  *
  5  *
  6  *
  7  *
  8  *
  9  *
 10  *
 11  *
 12  *
 13  *
 14  *
 15  *
 16  *
 17  */
 18
 19 package java.util.regex;
 20
 21 /**
 22  * Unchecked exception thrown to indicate a syntax error in a
 23  * regular-expression pattern.
 24  *
 25  * @author  unascribed
 26  * @since 1.4
 27  * @spec JSR-51
 28  */
 29
 30 public class PatternSyntaxException
 31     extends IllegalArgumentException
 32 {
 33     private static final long serialVersionUID = -3864639126226059218L;
 34
 35     private final String desc;
 36     private final String pattern;
 37     private final int index;
 38
 39     /**
 40      * Constructs a new instance of this class.
 41      *
 42      * @param  desc
 43      *         A description of the error
 44      *
 45      * @param  regex
 46      *         The erroneous pattern
 47      *
 48      * @param  index
 49      *         The approximate index in the pattern of the error,
 50      *         or {@code -1} if the index is not known
 51      */
 52     public PatternSyntaxException(String desc, String regex, int index) {
 53         this.desc = desc;
 54         this.pattern = regex;
 55         this.index = index;
 56     }
 57
 58     /**
 59      * Retrieves the error index.
 60      *
 61      * @return  The approximate index in the pattern of the error,
 62      *         or {@code -1} if the index is not known
 63      */
 64     public int getIndex() {
 65         return index;
 66     }
 67
 68     /**
 69      * Retrieves the description of the error.
 70      *
 71      * @return  The description of the error
 72      */
 73     public String getDescription() {
 74         return desc;
 75     }
 76
 77     /**
 78      * Retrieves the erroneous regular-expression pattern.
 79      *
 80      * @return  The erroneous pattern
 81      */
 82     public String getPattern() {
 83         return pattern;
 84     }
 85
 86     /**
 87      * Returns a multi-line string containing the description of the syntax
 88      * error and its index, the erroneous regular-expression pattern, and a
 89      * visual indication of the error index within the pattern.
 90      *
 91      * @return  The full detail message
 92      */
 93     public String getMessage() {
 94         StringBuilder sb = new StringBuilder();
 95         sb.append(desc);
 96         if (index >= 0) {
 97             sb.append(" near index ");
 98             sb.append(index);
 99         }
100         sb.append(System.lineSeparator());
101         sb.append(pattern);
102         if (index >= 0 && pattern != null && index < pattern.length()) {
103             sb.append(System.lineSeparator());
104             for (int i = 0; i < index; i++) sb.append(‘ ‘);
105             sb.append(‘^‘);
106         }
107         return sb.toString();
108     }
109
110 }

原文地址:https://www.cnblogs.com/zhangyishu/p/11263329.html

时间: 2024-08-03 13:27:27

PatternSyntaxException类(java JDK源码记录)的相关文章

Pattern类(java JDK源码记录)

1 /* 2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 *

Matcher类(java JDK源码记录)

1 /* 2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 *

java arraylist源码记录

1. ArrayList 实现了RandomAccess接口, RandomAccess接口用于标记是否可以随机访问 2. 继承了AbstractList类, 因此获取了modcount , modcount用于实现快速失败机制, 如果list有修改, 那么modcount自增 3. ArrayList不支持并发, 是非线程安全的 4. 支持存放null元素 5. 扩容, 每次都是原来大小的1.5倍 public void ensureCapacity(int minCapacity) { //

Java中集合框架,Collection接口、Set接口、List接口、Map接口,已经常用的它们的实现类,简单的JDK源码分析底层实现

(一)集合框架: Java语言的设计者对常用的数据结构和算法做了一些规范(接口)和实现(实现接口的类).所有抽象出来的数据结构和操作(算法)统称为集合框架. 程序员在具体应用的时候,不必考虑数据结构和算法实现细节,只需要用这些类创建一些对象,然后直接应用就可以了,这样就大大提高了编程效率. (二)集合框架包含的内容: (三)集合框架的接口(规范)   Collection接口:存储一组不唯一,无序的对象 List接口:存储一组不唯一,有序的对象 Set接口:存储一组唯一,无序的对象 Map接口:

从JDK源码角度看java并发的原子性如何保证

JDK源码中,在研究AQS框架时,会发现很多地方都使用了CAS操作,在并发实现中CAS操作必须具备原子性,而且是硬件级别的原子性,java被隔离在硬件之上,明显力不从心,这时为了能直接操作操作系统层面,肯定要通过用C++编写的native本地方法来扩展实现.JDK提供了一个类来满足CAS的要求,sun.misc.Unsafe,从名字上可以大概知道它用于执行低级别.不安全的操作,AQS就是使用此类完成硬件级别的原子操作. Unsafe是一个很强大的类,它可以分配内存.释放内存.可以定位对象某字段的

jdk源码阅读笔记之java集合框架(二)(ArrayList)

关于ArrayList的分析,会从且仅从其添加(add)与删除(remove)方法入手. ArrayList类定义: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Monaco } span.s1 { color: #931a68 } public class ArrayList<E> extends AbstractList<E> implements List<E> ArrayList基本属性: /** *

JDK源码笔记-java.util.HashMap

HashMap 的存储实现 当程序试图将多个 key-value 放入 HashMap 中时,以如下代码片段为例: Java代码 HashMap<String , Double> map = new HashMap<String , Double>(); map.put("语文" , 80.0); map.put("数学" , 89.0); map.put("英语" , 78.2); HashMap 采用一种所谓的&quo

【JDK源码】JDK的java.util.concurrent包结构

本文从JDK源码包中截取出concurrent包的所有类,对该包整体结构进行一个概述. 在JDK1.5之前,Java中要进行业务并发时,通常需要有程序员独立完成代码实现,当然也有一些开源的框架提供了这些功能,但是这些依然没有JDK自带的功能使用起来方便.而当针对高质量Java多线程并发程序设计时,为防止死蹦等现象的出现,比如使用java之前的wait().notify()和synchronized等,每每需要考虑性能.死锁.公平性.资源管理以及如何避免线程安全性方面带来的危害等诸多因素,往往会采

jdk源码每日一读 (一) java.lang.Object

jdk源码每日一读 (一) java.lang.Object 1. 类说明 Object是java继承体系的根,是每一个类的基类,所有的类都实现了Object类的所有方法. 2.重要方法 public final native Class<?> getClass() public native int hashCode(); public boolean equals(Object obj); protected native Object clone() throws CloneNotSup