View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.dict.kuromoji;
17  
18  import org.codelibs.fess.dict.DictionaryItem;
19  
20  public class KuromojiItem extends DictionaryItem {
21      private final String token;
22  
23      private final String segmentation;
24  
25      private final String reading;
26  
27      private final String pos;
28  
29      private String newToken;
30  
31      private String newSegmentation;
32  
33      private String newReading;
34  
35      private String newPos;
36  
37      public KuromojiItem(final long id, final String token, final String segmentation, final String reading, final String pos) {
38          this.id = id;
39          this.token = token;
40          this.segmentation = segmentation;
41          this.reading = reading;
42          this.pos = pos;
43  
44          if (id == 0) {
45              // create
46              newToken = token;
47              newSegmentation = segmentation;
48              newReading = reading;
49              newPos = pos;
50          }
51      }
52  
53      public String getNewToken() {
54          return newToken;
55      }
56  
57      public void setNewToken(final String newToken) {
58          this.newToken = newToken;
59      }
60  
61      public String getNewSegmentation() {
62          return newSegmentation;
63      }
64  
65      public void setNewSegmentation(final String newSegmentation) {
66          this.newSegmentation = newSegmentation;
67      }
68  
69      public String getNewReading() {
70          return newReading;
71      }
72  
73      public void setNewReading(final String newReading) {
74          this.newReading = newReading;
75      }
76  
77      public String getNewPos() {
78          return newPos;
79      }
80  
81      public void setNewPos(final String newPos) {
82          this.newPos = newPos;
83      }
84  
85      public String getToken() {
86          return token;
87      }
88  
89      public String getSegmentation() {
90          return segmentation;
91      }
92  
93      public String getReading() {
94          return reading;
95      }
96  
97      public String getPos() {
98          return pos;
99      }
100 
101     public boolean isUpdated() {
102         return newToken != null;
103     }
104 
105     public boolean isDeleted() {
106         return isUpdated() && newToken.length() == 0;
107     }
108 
109     @Override
110     public int hashCode() {
111         final int prime = 31;
112         int result = 1;
113         result = prime * result + (pos == null ? 0 : pos.hashCode());
114         result = prime * result + (reading == null ? 0 : reading.hashCode());
115         result = prime * result + (segmentation == null ? 0 : segmentation.hashCode());
116         result = prime * result + (token == null ? 0 : token.hashCode());
117         return result;
118     }
119 
120     @Override
121     public boolean equals(final Object obj) {
122         if (this == obj) {
123             return true;
124         }
125         if (obj == null) {
126             return false;
127         }
128         if (getClass() != obj.getClass()) {
129             return false;
130         }
131         final KuromojiItem other = (KuromojiItem) obj;
132         if (pos == null) {
133             if (other.pos != null) {
134                 return false;
135             }
136         } else if (!pos.equals(other.pos)) {
137             return false;
138         }
139         if (reading == null) {
140             if (other.reading != null) {
141                 return false;
142             }
143         } else if (!reading.equals(other.reading)) {
144             return false;
145         }
146         if (segmentation == null) {
147             if (other.segmentation != null) {
148                 return false;
149             }
150         } else if (!segmentation.equals(other.segmentation)) {
151             return false;
152         }
153         if (token == null) {
154             if (other.token != null) {
155                 return false;
156             }
157         } else if (!token.equals(other.token)) {
158             return false;
159         }
160         return true;
161     }
162 
163     @Override
164     public String toString() {
165         return "KuromojiItem [token=" + token + ", segmentation=" + segmentation + ", reading=" + reading + ", pos=" + pos + ", newToken="
166                 + newToken + ", newSegmentation=" + newSegmentation + ", newReading=" + newReading + ", newPos=" + newPos + "]";
167     }
168 
169     public String toLineString() {
170         final StringBuilder buf = new StringBuilder(100);
171         if (isUpdated()) {
172             buf.append(quoteEscape(newToken));
173             buf.append(',');
174             buf.append(quoteEscape(newSegmentation));
175             buf.append(',');
176             buf.append(quoteEscape(newReading));
177             buf.append(',');
178             buf.append(quoteEscape(newPos));
179         } else {
180             buf.append(quoteEscape(token));
181             buf.append(',');
182             buf.append(quoteEscape(segmentation));
183             buf.append(',');
184             buf.append(quoteEscape(reading));
185             buf.append(',');
186             buf.append(quoteEscape(pos));
187         }
188         return buf.toString();
189     }
190 
191     private static String quoteEscape(final String original) {
192         String result = original;
193 
194         if (result.indexOf('\"') >= 0) {
195             result = result.replace("\"", "\"\"");
196         }
197         if (result.indexOf(',') >= 0) {
198             return "\"" + result + "\"";
199         }
200         return result;
201     }
202 }