2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.input
;
21 import java
.util
.Arrays
;
22 import java
.util
.List
;
25 * Very simple pattern that matches the input stream against a pre-defined list of characters. For the pattern to match,
26 * the list of characters must match exactly what's coming in on the input stream.
28 * @author Martin, Andreas
30 public class BasicCharacterPattern
implements CharacterPattern
{
31 private final KeyStroke result
;
32 private final char[] pattern
;
35 * Creates a new BasicCharacterPattern that matches a particular sequence of characters into a {@code KeyStroke}
36 * @param result {@code KeyStroke} that this pattern will translate to
37 * @param pattern Sequence of characters that translates into the {@code KeyStroke}
39 public BasicCharacterPattern(KeyStroke result
, char... pattern
) {
41 this.pattern
= pattern
;
45 * Returns the characters that makes up this pattern, as an array that is a copy of the array used internally
46 * @return Array of characters that defines this pattern
48 public char[] getPattern() {
49 return Arrays
.copyOf(pattern
, pattern
.length
);
53 * Returns the keystroke that this pattern results in
54 * @return The keystoke this pattern will return if it matches
56 public KeyStroke
getResult() {
61 public Matching
match(List
<Character
> seq
) {
62 int size
= seq
.size();
64 if(size
> pattern
.length
) {
67 for (int i
= 0; i
< size
; i
++) {
68 if (pattern
[i
] != seq
.get(i
)) {
72 if (size
== pattern
.length
) {
73 return new Matching( getResult() ); // yep
75 return Matching
.NOT_YET
; // maybe later
80 public boolean equals(Object obj
) {
81 if (!(obj
instanceof BasicCharacterPattern
)) {
85 BasicCharacterPattern other
= (BasicCharacterPattern
) obj
;
86 return Arrays
.equals(this.pattern
, other
.pattern
);
90 public int hashCode() {
92 hash
= 53 * hash
+ Arrays
.hashCode(this.pattern
);