Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegexStringList.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.regex.PatternSyntaxException;
  26. /**
  27. * IRC Parser Ignore list.
  28. *
  29. * @author Shane Mc Cormack
  30. */
  31. public class RegexStringList {
  32. /** Arraylist storing ignore patterns. */
  33. protected final List<String> ignoreInfo = new ArrayList<String>();
  34. /**
  35. * Creates a new instance of RegexStringList.
  36. */
  37. public RegexStringList() {
  38. // Do nothing
  39. }
  40. /**
  41. * Creates a new instance of RegexStringList, with the specified items.
  42. *
  43. * @param items Items to add to this RegexStringList
  44. */
  45. public RegexStringList(final List<String> items) {
  46. addAll(items);
  47. }
  48. /**
  49. * Add a new ignore pattern to the ignore list.
  50. *
  51. * @param pattern Regex syntax for the ignore (Pattern is matched case-insensitively as ^pattern$)
  52. */
  53. public void add(final String pattern) {
  54. for (String target : ignoreInfo) {
  55. if (pattern.equalsIgnoreCase(target)) {
  56. return;
  57. }
  58. }
  59. ignoreInfo.add(pattern);
  60. }
  61. /**
  62. * Adds a set of patterns to the list.
  63. *
  64. * @param patterns A list of patterns to be added
  65. */
  66. public void addAll(final List<String> patterns) {
  67. for (String pattern : patterns) {
  68. add(pattern);
  69. }
  70. }
  71. /**
  72. * Delete an ignore from the list.
  73. *
  74. * @param position Position in the list to remove
  75. */
  76. public void remove(final int position) {
  77. if (position < this.count()) {
  78. ignoreInfo.remove(position);
  79. }
  80. }
  81. /**
  82. * Clear the ignore list.
  83. */
  84. public void clear() {
  85. ignoreInfo.clear();
  86. }
  87. /**
  88. * Check if a string matches any of the ignores in the list.
  89. *
  90. * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
  91. * @return integer showing the position of the first match in the ignore list (-1 if none)
  92. * @throws PatternSyntaxException if one of the items in the list is an invalid regex
  93. */
  94. public int matches(final String check) throws PatternSyntaxException {
  95. for (int i = 0; i < this.count(); ++i) {
  96. if (check.matches("(?i)"+this.get(i))) {
  97. return i;
  98. }
  99. }
  100. return -1;
  101. }
  102. /**
  103. * Check if a string matches a specific ignore in the list.
  104. *
  105. * @param position Position to check
  106. * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
  107. * @return boolean true/false
  108. * @throws PatternSyntaxException if the item is an invalid regex
  109. */
  110. public boolean matches(final int position, final String check) throws PatternSyntaxException {
  111. if (position < this.count()) {
  112. return check.matches("(?i)"+this.get(position));
  113. } else {
  114. return false;
  115. }
  116. }
  117. /**
  118. * Get the ignore pattern in a given position in the list.
  119. *
  120. * @param position Position to check
  121. * @return String showing the pattern. ("" if position isn't valid)
  122. */
  123. public String get(final int position) {
  124. if (position < this.count()) {
  125. return ignoreInfo.get(position);
  126. } else {
  127. return "";
  128. }
  129. }
  130. /**
  131. * Change the ignore pattern in a given position in the list.
  132. *
  133. * @param position Position to change
  134. * @param pattern New pattern
  135. */
  136. public void set(final int position, final String pattern) {
  137. if (position < this.count()) {
  138. ignoreInfo.set(position, pattern);
  139. }
  140. }
  141. /**
  142. * Get the amount of ignores in the list.
  143. *
  144. * @return int showing the number of ignores
  145. */
  146. public int count() {
  147. return ignoreInfo.size();
  148. }
  149. }