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.mapping;
17  
18  import java.util.Arrays;
19  import java.util.Objects;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.dict.DictionaryItem;
24  
25  public class CharMappingItem extends DictionaryItem {
26      private final String[] inputs;
27  
28      private final String output;
29  
30      private String[] newInputs;
31  
32      private String newOutput;
33  
34      public CharMappingItem(final long id, final String[] inputs, final String output) {
35          this.id = id;
36          this.inputs = inputs;
37          this.output = output == null ? null : output.replace("\n", " ");
38          Arrays.sort(inputs);
39  
40          if (id == 0) {
41              // create
42              newInputs = inputs;
43              newOutput = output;
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 getNewOutput() {
56          return newOutput;
57      }
58  
59      public void setNewOutput(final String newOutput) {
60          this.newOutput = newOutput == null ? null : newOutput.replace("\n", " ");
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 getOutput() {
75          return output;
76      }
77  
78      public boolean isUpdated() {
79          return newInputs != null && newOutput != null;
80      }
81  
82      public boolean isDeleted() {
83          return isUpdated() && newInputs.length == 0;
84      }
85  
86      public void sort() {
87          if (inputs != null) {
88              Arrays.sort(inputs);
89          }
90          if (newInputs != null) {
91              Arrays.sort(newInputs);
92          }
93      }
94  
95      @Override
96      public int hashCode() {
97          return Objects.hash(Arrays.hashCode(inputs), output);
98      }
99  
100     @Override
101     public boolean equals(final Object obj) {
102         if (this == obj) {
103             return true;
104         }
105         if (obj == null) {
106             return false;
107         }
108         if (getClass() != obj.getClass()) {
109             return false;
110         }
111         final CharMappingItem other = (CharMappingItem) obj;
112         sort();
113         other.sort();
114         if (!Arrays.equals(inputs, other.inputs)) {
115             return false;
116         }
117         if (!output.equals(other.getOutput())) {
118             return false;
119         }
120         return true;
121     }
122 
123     @Override
124     public String toString() {
125         return "MappingItem [id=" + id + ", inputs=" + Arrays.toString(inputs) + ", output=" + output + ", newInputs="
126                 + Arrays.toString(newInputs) + ", newOutput=" + newOutput + "]";
127     }
128 
129     public String toLineString() {
130         if (isUpdated()) {
131             return StringUtils.join(newInputs, ",") + "=>" + newOutput;
132         }
133         return StringUtils.join(inputs, ",") + "=>" + output;
134     }
135 
136 }