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.seunjeon;
17  
18  import java.util.Arrays;
19  
20  import org.apache.commons.lang3.StringUtils;
21  import org.codelibs.core.lang.StringUtil;
22  import org.codelibs.fess.dict.DictionaryItem;
23  
24  public class SeunjeonItem extends DictionaryItem {
25      private final String[] inputs;
26  
27      private String[] newInputs;
28  
29      public SeunjeonItem(final long id, final String[] inputs) {
30          this.id = id;
31          this.inputs = inputs;
32  
33          if (id == 0) {
34              // create
35              newInputs = inputs;
36          }
37      }
38  
39      public String[] getNewInputs() {
40          return newInputs;
41      }
42  
43      public void setNewInputs(final String[] newInputs) {
44          this.newInputs = newInputs;
45      }
46  
47      public String[] getInputs() {
48          return inputs;
49      }
50  
51      public String getInputsValue() {
52          if (inputs == null) {
53              return StringUtil.EMPTY;
54          }
55          return String.join(",", inputs);
56      }
57  
58      public boolean isUpdated() {
59          return newInputs != null;
60      }
61  
62      public boolean isDeleted() {
63          return isUpdated() && newInputs.length == 0;
64      }
65  
66      @Override
67      public int hashCode() {
68          final int prime = 31;
69          int result = 1;
70          result = prime * result + Arrays.hashCode(inputs);
71          return result;
72      }
73  
74      @Override
75      public boolean equals(final Object obj) {
76          if (this == obj) {
77              return true;
78          }
79          if (obj == null) {
80              return false;
81          }
82          if (getClass() != obj.getClass()) {
83              return false;
84          }
85          final SeunjeonItem other = (SeunjeonItem) obj;
86          if (!Arrays.equals(inputs, other.inputs)) {
87              return false;
88          }
89          return true;
90      }
91  
92      @Override
93      public String toString() {
94          return "SynonymItem [id=" + id + ", inputs=" + Arrays.toString(inputs) + ", newInputs=" + Arrays.toString(newInputs) + "]";
95      }
96  
97      public String toLineString() {
98          if (isUpdated()) {
99              return StringUtils.join(newInputs, ",");
100         } else {
101             return StringUtils.join(inputs, ",");
102         }
103     }
104 
105 }