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.stopwords;
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 * Represents an item in a stopwords dictionary.
24 * This class stores a stopword and tracks its updated value.
25 */
26 public class StopwordsItem extends DictionaryItem {
27 /** The original stopword. */
28 private final String input;
29
30 /** The new stopword, if updated. */
31 private String newInput;
32
33 /**
34 * Constructs a new stopword item.
35 *
36 * @param id The unique identifier of the item.
37 * @param input The stopword.
38 */
39 public StopwordsItem(final long id, final String input) {
40 this.id = id;
41 this.input = input;
42
43 if (id == 0) {
44 // create
45 newInput = input;
46 }
47 }
48
49 /**
50 * Gets the new stopword.
51 *
52 * @return The new stopword.
53 */
54 public String getNewInput() {
55 return newInput;
56 }
57
58 /**
59 * Sets the new stopword.
60 *
61 * @param newInput The new stopword.
62 */
63 public void setNewInput(final String newInput) {
64 this.newInput = newInput;
65 }
66
67 /**
68 * Gets the original stopword.
69 *
70 * @return The original stopword.
71 */
72 public String getInput() {
73 return input;
74 }
75
76 /**
77 * Gets the value of the stopword.
78 *
79 * @return The stopword value, or an empty string if null.
80 */
81 public String getInputValue() {
82 if (input == null) {
83 return StringUtil.EMPTY;
84 }
85 return input;
86 }
87
88 /**
89 * Checks if the item has been updated.
90 *
91 * @return true if the item has been updated, false otherwise.
92 */
93 public boolean isUpdated() {
94 return newInput != null;
95 }
96
97 /**
98 * Checks if the item has been marked for deletion.
99 *
100 * @return true if the item is marked for deletion, false otherwise.
101 */
102 public boolean isDeleted() {
103 return isUpdated() && newInput.length() == 0;
104 }
105
106 @Override
107 public int hashCode() {
108 return java.util.Objects.hashCode(input);
109 }
110
111 @Override
112 public boolean equals(final Object obj) {
113 if (this == obj) {
114 return true;
115 }
116 if (obj == null || getClass() != obj.getClass()) {
117 return false;
118 }
119 final StopwordsItem other = (StopwordsItem) obj;
120 return java.util.Objects.equals(input, other.input);
121 }
122
123 @Override
124 public String toString() {
125 return "StopwordsItem [id=" + id + ", inputs=" + input + ", newInputs=" + newInput + "]";
126 }
127
128 /**
129 * Converts the item to a string representation for writing to a file.
130 *
131 * @return The stopword as a string.
132 */
133 public String toLineString() {
134 if (isUpdated()) {
135 return StringUtils.join(newInput);
136 }
137 return StringUtils.join(input);
138 }
139
140 }