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.protwords;
17  
18  import org.apache.commons.lang3.StringUtils;
19  import org.codelibs.core.lang.StringUtil;
20  import org.codelibs.fess.dict.DictionaryItem;
21  
22  /**
23   * Dictionary item for protected words.
24   * This class represents a single protected word entry in the dictionary.
25   */
26  public class ProtwordsItem extends DictionaryItem {
27      private final String input;
28  
29      private String newInput;
30  
31      /**
32       * Constructor for ProtwordsItem.
33       * @param id the unique identifier for this item
34       * @param input the protected word input
35       */
36      public ProtwordsItem(final long id, final String input) {
37          this.id = id;
38          this.input = input;
39  
40          if (id == 0) {
41              // create
42              newInput = input;
43          }
44      }
45  
46      /**
47       * Gets the new input value for this item.
48       * @return the new input value
49       */
50      public String getNewInput() {
51          return newInput;
52      }
53  
54      /**
55       * Sets the new input value for this item.
56       * @param newInput the new input value
57       */
58      public void setNewInput(final String newInput) {
59          this.newInput = newInput;
60      }
61  
62      /**
63       * Gets the input value for this item.
64       * @return the input value
65       */
66      public String getInput() {
67          return input;
68      }
69  
70      /**
71       * Gets the input value or empty string if null.
72       * @return the input value or empty string
73       */
74      public String getInputValue() {
75          if (input == null) {
76              return StringUtil.EMPTY;
77          }
78          return input;
79      }
80  
81      /**
82       * Checks if this item has been updated.
83       * @return true if updated, false otherwise
84       */
85      public boolean isUpdated() {
86          return newInput != null;
87      }
88  
89      /**
90       * Checks if this item has been deleted.
91       * @return true if deleted, false otherwise
92       */
93      public boolean isDeleted() {
94          return isUpdated() && newInput.length() == 0;
95      }
96  
97      @Override
98      public int hashCode() {
99          return java.util.Objects.hashCode(input);
100     }
101 
102     @Override
103     public boolean equals(final Object obj) {
104         if (this == obj) {
105             return true;
106         }
107         if (obj == null || getClass() != obj.getClass()) {
108             return false;
109         }
110         final ProtwordsItem other = (ProtwordsItem) obj;
111         return java.util.Objects.equals(input, other.input);
112     }
113 
114     @Override
115     public String toString() {
116         return "ProtwordsItem [id=" + id + ", inputs=" + input + ", newInputs=" + newInput + "]";
117     }
118 
119     /**
120      * Converts this item to a string representation for writing to file.
121      * @return the string representation of this item
122      */
123     public String toLineString() {
124         if (isUpdated()) {
125             return StringUtils.join(newInput);
126         }
127         return StringUtils.join(input);
128     }
129 
130 }