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