1 import fi.jyu.mit.ohj2.Mjonot;
3
9 public class JokeriHakija implements SanaHakija {
10 private String ehto = null;
11 private String alku = null;
12 private String loppu = null;
13 private boolean jokeri = false;
14
15
19 public JokeriHakija(String ehto) {
20 alusta(ehto);
21 }
22
23
27 public void alusta(String ehto) {
28 if (ehto!=null){
29 int i = ehto.indexOf('*');
30 if ( (ehto.indexOf(' ')!=-1) || (i!=ehto.lastIndexOf('*')) )
31 throw new IllegalArgumentException("Virheellinen hakuehto!");
32 StringBuffer sb = new StringBuffer(ehto);
33 this.alku = Mjonot.erota(sb,'*');
34 this.loppu = sb.toString();
35 this.jokeri = (i!=-1);
36 }
37 this.ehto = ehto;
38 }
39
40
45 public boolean hae(String sana) {
46 if (this.ehto==null) return true;
47 if (sana.length()<this.alku.length()+this.loppu.length()) return false;
48 if (sana.indexOf(this.alku)!=0) return false;
49 if ((!jokeri) && (sana.length()>this.alku.length())) return false;
50 if (sana.lastIndexOf(this.loppu)!=sana.length()-this.loppu.length()) return false;
51 return true;
52 }
53
54
58 public String toString() {
59 return this.ehto;
60 }
61 }
62