View Javadoc
1   /*
2    * Copyright 2012-2025 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.stemmeroverride;
17  
18  import java.util.Objects;
19  
20  import org.codelibs.core.lang.StringUtil;
21  import org.codelibs.fess.dict.DictionaryItem;
22  
23  /**
24   * Represents an item in a stemmer override dictionary.
25   * This class stores a mapping from an input word to its corresponding
26   * output stem. It also tracks updated values for the item.
27   */
28  public class StemmerOverrideItem extends DictionaryItem {
29      /** The original input word. */
30      private final String input;
31  
32      /** The original output stem. */
33      private final String output;
34  
35      /** The new input word, if updated. */
36      private String newInput;
37  
38      /** The new output stem, if updated. */
39      private String newOutput;
40  
41      /**
42       * Constructs a new stemmer override item.
43       *
44       * @param id     The unique identifier of the item.
45       * @param input  The input word.
46       * @param output The output stem.
47       */
48      public StemmerOverrideItem(final long id, final String input, final String output) {
49          this.id = id;
50          this.input = input;
51          this.output = output;
52  
53          if (id == 0) {
54              // create
55              newInput = input;
56              newOutput = output;
57          }
58      }
59  
60      /**
61       * Gets the new input word.
62       *
63       * @return The new input word.
64       */
65      public String getNewInput() {
66          return newInput;
67      }
68  
69      /**
70       * Sets the new input word.
71       *
72       * @param newInput The new input word.
73       */
74      public void setNewInput(final String newInput) {
75          this.newInput = newInput;
76      }
77  
78      /**
79       * Gets the new output stem.
80       *
81       * @return The new output stem.
82       */
83      public String getNewOutput() {
84          return newOutput;
85      }
86  
87      /**
88       * Sets the new output stem.
89       *
90       * @param newOutputs The new output stem.
91       */
92      public void setNewOutput(final String newOutputs) {
93          newOutput = newOutputs;
94      }
95  
96      /**
97       * Gets the original input word.
98       *
99       * @return The original input word.
100      */
101     public String getInput() {
102         return input;
103     }
104 
105     /**
106      * Gets the original output stem.
107      *
108      * @return The original output stem.
109      */
110     public String getOutput() {
111         return output;
112     }
113 
114     /**
115      * Checks if the item has been updated.
116      *
117      * @return true if the item has been updated, false otherwise.
118      */
119     public boolean isUpdated() {
120         return newInput != null && newOutput != null;
121     }
122 
123     /**
124      * Checks if the item has been marked for deletion.
125      *
126      * @return true if the item is marked for deletion, false otherwise.
127      */
128     public boolean isDeleted() {
129         return isUpdated() && StringUtil.isBlank(newInput);
130     }
131 
132     @Override
133     public int hashCode() {
134         return Objects.hash(input, output);
135     }
136 
137     @Override
138     public boolean equals(final Object obj) {
139         if (this == obj) {
140             return true;
141         }
142         if (obj == null || getClass() != obj.getClass()) {
143             return false;
144         }
145         final StemmerOverrideItem other = (StemmerOverrideItem) obj;
146         if (!Objects.equals(input, other.input) || !Objects.equals(output, other.output)) {
147             return false;
148         }
149         return true;
150     }
151 
152     @Override
153     public String toString() {
154         return "StemmerOverrideItem [input=" + input + ", output=" + output + ", newInput=" + newInput + ", newOutput=" + newOutput + "]";
155     }
156 
157     /**
158      * Converts the item to a string representation for writing to a file.
159      *
160      * @return A string in the format "input=>output".
161      */
162     public String toLineString() {
163         if (isUpdated()) {
164             return newInput + "=>" + newOutput;
165         }
166         return input + "=>" + output;
167     }
168 
169 }