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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack
  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. * SVN: $Id$
  23. */
  24. package uk.org.ownage.dmdirc.parser;
  25. import java.util.ArrayList;
  26. /**
  27. * IRC Parser Ignore list.
  28. *
  29. * @author Shane Mc Cormack
  30. * @version $Id$
  31. */
  32. public class RegexStringList {
  33. /** Arraylist storing ignore patterns */
  34. private ArrayList<String> ignoreInfo = new ArrayList<String>();
  35. /**
  36. * Add a new ignore pattern to the ignore list.
  37. *
  38. * @param pattern Regex syntax for the ignore (Pattern is matched case-insensitively as ^pattern$)
  39. */
  40. public void add(String pattern) {
  41. for (int i = 0; i < this.count(); ++i) {
  42. if (pattern.equalsIgnoreCase(this.get(i))) {
  43. return;
  44. }
  45. }
  46. ignoreInfo.add(pattern);
  47. }
  48. /**
  49. * Delete an ignore from the list
  50. *
  51. * @param position Position in the list to remove
  52. */
  53. public void remove(int position) {
  54. if (position < this.count()) {
  55. ignoreInfo.remove(position);
  56. }
  57. }
  58. /**
  59. * Check if a string matches any of the ignores in the list
  60. *
  61. * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
  62. * @return integer showing the position of the first match in the ignore list (-1 if none)
  63. */
  64. public int matches(String check) {
  65. for (int i = 0; i < this.count(); ++i) {
  66. if (check.matches("(?i)"+this.get(i))) {
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72. /**
  73. * Check if a string matches a specific ignore in the list
  74. *
  75. * @param position Position to check
  76. * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
  77. * @return boolean true/false
  78. */
  79. public boolean matches(int position, String check) {
  80. if (position < this.count()) {
  81. return check.matches("(?i)"+this.get(position));
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Get the ignore pattern in a given position in the list.
  88. *
  89. * @param position Position to check
  90. * @return String showing the pattern. ("" if position isn't valid)
  91. */
  92. public String get(int position) {
  93. if (position < this.count()) {
  94. return ignoreInfo.get(position);
  95. } else {
  96. return "";
  97. }
  98. }
  99. /**
  100. * Change the ignore pattern in a given position in the list.
  101. *
  102. * @param position Position to change
  103. * @param pattern New pattern
  104. */
  105. public void set(int position, String pattern) {
  106. if (position < this.count()) {
  107. ignoreInfo.set(position, pattern);
  108. }
  109. }
  110. /**
  111. * Get the amount of ignores in the list.
  112. *
  113. * @return int showing the number of ignores
  114. */
  115. public int count() {
  116. return ignoreInfo.size();
  117. }
  118. /**
  119. * Get SVN Version information.
  120. *
  121. * @return SVN Version String
  122. */
  123. public static String getSvnInfo () { return "$Id$"; }
  124. }