View Javadoc
1   /*
2    * Copyright 2012-2021 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  import java.util.Objects;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.codelibs.core.collection.ArrayUtil;
23  import org.codelibs.core.lang.StringUtil;
24  import org.codelibs.fess.dict.DictionaryItem;
25  
26  public class SynonymItem extends DictionaryItem {
27      private final String[] inputs;
28  
29      private final String[] outputs;
30  
31      private String[] newInputs;
32  
33      private String[] newOutputs;
34  
35      public SynonymItem(final long id, final String[] inputs, final String[] outputs) {
36          this.id = id;
37          this.inputs = inputs;
38          this.outputs = outputs;
39  
40          if (id == 0) {
41              // create
42              newInputs = inputs;
43              newOutputs = outputs;
44          }
45      }
46  
47      public String[] getNewInputs() {
48          return newInputs;
49      }
50  
51      public void setNewInputs(final String[] newInputs) {
52          this.newInputs = newInputs;
53      }
54  
55      public String[] getNewOutputs() {
56          return newOutputs;
57      }
58  
59      public void setNewOutputs(final String[] newOutputs) {
60          this.newOutputs = newOutputs;
61      }
62  
63      public String[] getInputs() {
64          return inputs;
65      }
66  
67      public String getInputsValue() {
68          if (inputs == null) {
69              return StringUtil.EMPTY;
70          }
71          return String.join("\n", inputs);
72      }
73  
74      public String[] getOutputs() {
75          return outputs;
76      }
77  
78      public String getOutputsValue() {
79          if (outputs == null) {
80              return StringUtil.EMPTY;
81          }
82          return String.join("\n", outputs);
83      }
84  
85      public boolean isUpdated() {
86          return newInputs != null && newOutputs != null;
87      }
88  
89      public boolean isDeleted() {
90          return isUpdated() && newInputs.length == 0;
91      }
92  
93      @Override
94      public int hashCode() {
95          return Objects.hash(Arrays.hashCode(inputs), Arrays.hashCode(outputs));
96      }
97  
98      @Override
99      public boolean equals(final Object obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (obj == null) {
104             return false;
105         }
106         if (getClass() != obj.getClass()) {
107             return false;
108         }
109         final SynonymItem other = (SynonymItem) obj;
110         if (!ArrayUtil.equalsIgnoreSequence(inputs, other.inputs)) {
111             return false;
112         }
113         if (!ArrayUtil.equalsIgnoreSequence(outputs, other.outputs)) {
114             return false;
115         }
116         return true;
117     }
118 
119     @Override
120     public String toString() {
121         return "SynonymItem [id=" + id + ", inputs=" + Arrays.toString(inputs) + ", outputs=" + Arrays.toString(outputs) + ", newInputs="
122                 + Arrays.toString(newInputs) + ", newOutputs=" + Arrays.toString(newOutputs) + "]";
123     }
124 
125     public String toLineString() {
126         if (isUpdated()) {
127             if (Arrays.equals(newInputs, newOutputs)) {
128                 return StringUtils.join(newInputs, ",");
129             }
130             return StringUtils.join(newInputs, ",") + "=>" + StringUtils.join(newOutputs, ",");
131         }
132         if (Arrays.equals(inputs, outputs)) {
133             return StringUtils.join(inputs, ",");
134         }
135         return StringUtils.join(inputs, ",") + "=>" + StringUtils.join(outputs, ",");
136     }
137 
138 }