Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | /* |
2 | * This file is part of lanterna (http://code.google.com/p/lanterna/). | |
3 | * | |
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. | |
8 | * | |
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. | |
13 | * | |
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/>. | |
16 | * | |
17 | * Copyright (C) 2010-2015 Martin | |
18 | */ | |
19 | package com.googlecode.lanterna.input; | |
20 | ||
21 | import java.util.Arrays; | |
22 | import java.util.List; | |
23 | ||
24 | /** | |
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. | |
27 | * | |
28 | * @author Martin, Andreas | |
29 | */ | |
30 | public class BasicCharacterPattern implements CharacterPattern { | |
31 | private final KeyStroke result; | |
32 | private final char[] pattern; | |
33 | ||
34 | /** | |
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} | |
38 | */ | |
39 | public BasicCharacterPattern(KeyStroke result, char... pattern) { | |
40 | this.result = result; | |
41 | this.pattern = pattern; | |
42 | } | |
43 | ||
44 | /** | |
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 | |
47 | */ | |
48 | public char[] getPattern() { | |
49 | return Arrays.copyOf(pattern, pattern.length); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Returns the keystroke that this pattern results in | |
54 | * @return The keystoke this pattern will return if it matches | |
55 | */ | |
56 | public KeyStroke getResult() { | |
57 | return result; | |
58 | } | |
59 | ||
60 | @Override | |
61 | public Matching match(List<Character> seq) { | |
62 | int size = seq.size(); | |
63 | ||
64 | if(size > pattern.length) { | |
65 | return null; // nope | |
66 | } | |
67 | for (int i = 0; i < size; i++) { | |
68 | if (pattern[i] != seq.get(i)) { | |
69 | return null; // nope | |
70 | } | |
71 | } | |
72 | if (size == pattern.length) { | |
73 | return new Matching( getResult() ); // yep | |
74 | } else { | |
75 | return Matching.NOT_YET; // maybe later | |
76 | } | |
77 | } | |
78 | ||
79 | @Override | |
80 | public boolean equals(Object obj) { | |
81 | if (!(obj instanceof BasicCharacterPattern)) { | |
82 | return false; | |
83 | } | |
84 | ||
85 | BasicCharacterPattern other = (BasicCharacterPattern) obj; | |
86 | return Arrays.equals(this.pattern, other.pattern); | |
87 | } | |
88 | ||
89 | @Override | |
90 | public int hashCode() { | |
91 | int hash = 3; | |
92 | hash = 53 * hash + Arrays.hashCode(this.pattern); | |
93 | return hash; | |
94 | } | |
95 | } |