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.synonym;
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 SynonymItem extends DictionaryItem {
25      private final String[] inputs;
26  
27      private final String[] outputs;
28  
29      private String[] newInputs;
30  
31      private String[] newOutputs;
32  
33      public SynonymItem(final long id, final String[] inputs, final String[] outputs) {
34          this.id = id;
35          this.inputs = inputs;
36          this.outputs = outputs;
37          Arrays.sort(inputs);
38          if (inputs != outputs) {
39              Arrays.sort(outputs);
40          }
41  
42          if (id == 0) {
43              // create
44              newInputs = inputs;
45              newOutputs = outputs;
46          }
47      }
48  
49      public String[] getNewInputs() {
50          return newInputs;
51      }
52  
53      public void setNewInputs(final String[] newInputs) {
54          this.newInputs = newInputs;
55      }
56  
57      public String[] getNewOutputs() {
58          return newOutputs;
59      }
60  
61      public void setNewOutputs(final String[] newOutputs) {
62          this.newOutputs = newOutputs;
63      }
64  
65      public String[] getInputs() {
66          return inputs;
67      }
68  
69      public String getInputsValue() {
70          if (inputs == null) {
71              return StringUtil.EMPTY;
72          }
73          return String.join("\n", inputs);
74      }
75  
76      public String[] getOutputs() {
77          return outputs;
78      }
79  
80      public String getOutputsValue() {
81          if (outputs == null) {
82              return StringUtil.EMPTY;
83          }
84          return String.join("\n", outputs);
85      }
86  
87      public boolean isUpdated() {
88          return newInputs != null && newOutputs != null;
89      }
90  
91      public boolean isDeleted() {
92          return isUpdated() && newInputs.length == 0;
93      }
94  
95      public void sort() {
96          if (inputs != null) {
97              Arrays.sort(inputs);
98          }
99          if (outputs != null) {
100             Arrays.sort(outputs);
101         }
102         if (newInputs != null) {
103             Arrays.sort(newInputs);
104         }
105         if (newOutputs != null) {
106             Arrays.sort(newOutputs);
107         }
108     }
109 
110     @Override
111     public int hashCode() {
112         final int prime = 31;
113         int result = 1;
114         result = prime * result + Arrays.hashCode(inputs);
115         result = prime * result + Arrays.hashCode(outputs);
116         return result;
117     }
118 
119     @Override
120     public boolean equals(final Object obj) {
121         if (this == obj) {
122             return true;
123         }
124         if (obj == null) {
125             return false;
126         }
127         if (getClass() != obj.getClass()) {
128             return false;
129         }
130         final SynonymItem other = (SynonymItem) obj;
131         sort();
132         other.sort();
133         if (!Arrays.equals(inputs, other.inputs)) {
134             return false;
135         }
136         if (!Arrays.equals(outputs, other.outputs)) {
137             return false;
138         }
139         return true;
140     }
141 
142     @Override
143     public String toString() {
144         return "SynonymItem [id=" + id + ", inputs=" + Arrays.toString(inputs) + ", outputs=" + Arrays.toString(outputs) + ", newInputs="
145                 + Arrays.toString(newInputs) + ", newOutputs=" + Arrays.toString(newOutputs) + "]";
146     }
147 
148     public String toLineString() {
149         if (isUpdated()) {
150             if (Arrays.equals(newInputs, newOutputs)) {
151                 return StringUtils.join(newInputs, ",");
152             } else {
153                 return StringUtils.join(newInputs, ",") + "=>" + StringUtils.join(newOutputs, ",");
154             }
155         } else {
156             if (Arrays.equals(inputs, outputs)) {
157                 return StringUtils.join(inputs, ",");
158             } else {
159                 return StringUtils.join(inputs, ",") + "=>" + StringUtils.join(outputs, ",");
160             }
161         }
162     }
163 
164 }